none
Hyperlink Formatting in Outlook VBScript Signature RRS feed

  • Question

  • Hi all,

    I've created a VBScript for Word/Outlook to generate Active Directory based user signatures. The script runs fine and all of the information is correctly attributed, however I'm having trouble formatting the hyperlinks.

    The signature block is in font size 10 and colour black, however the hyperlinks are taking the default style of font size 11 and colour blue. I want to be able to format the hyperlinks with the same font size and colour as the remainder of the text block, however retain the underline to make it apparent that they are hyperlinks.

    I know I could do this by using the Selection object, extending the selection backwards to select the string value I've just entered and update the formatting after the string has been entered, but I'd rather not mess with the caret position and have the formatting applied in-line.

    The hyperlinks need to remain as clickable links, so using the NoAutoFormat property isn't an option for me.

    Here is my code thus far:

    objSelection.Font.Bold = True
    objSelection.TypeText strName
    objSelection.TypeText Chr(11)
    
    objSelection.Font.Size = 10
    objSelection.Font.Bold = False
    objSelection.TypeText strJobTitle & ", " & strDepartment
    objSelection.TypeText Chr(11)
    objSelection.TypeText strCompany
    objSelection.TypeParagraph()
    
    objSelection.TypeText "Email: "
    objDoc.Hyperlinks.Add objSelection.Range, "mailto:" & strEmail,,,strEmail
    objSelection.TypeText " | " & "Web: "
    objDoc.Hyperlinks.Add objSelection.Range, strWebAddress,,,strWebAddress
    objSelection.TypeParagraph()
    
    Set objSelection = objDoc.Range()

    Thanks for any help.
    Richard


    Richard Green, MCSE Windows Server 2003

    Tuesday, February 21, 2012 9:17 PM

Answers

  • Just for an example below is some sample code that might help explain some things you can do:

    Sub InsertHyperlink()
        Dim doc As Word.Document
        Set doc = Word.ActiveDocument
        Dim strEmail As String: strEmail = "myEmail@myCompany.com"
        Dim strWebAddress As String: strWebAddress = "http://www.myCompany.com"
        Word.Selection.TypeText "Email: "
        doc.Hyperlinks.Add Word.Selection.Range, "mailto:" & strEmail, , , strEmail
        Word.Selection.StartOf unit:=wdWord, Extend:=wdExtend
        Word.Selection.Font.Size = 14
        Word.Selection.Collapse wdCollapseEnd
        Word.Selection.TypeText " | " & "Web: "
        doc.Hyperlinks.Add Word.Selection.Range, strWebAddress, , , strWebAddress
    '    Word.Selection.StartOf unit:=wdParagraph, Extend:=wdExtend
    '    Word.Selection.Font.Bold = True
    '    Word.Selection.Collapse wdCollapseEnd
        Word.Selection.TypeParagraph
    End Sub

    After you have added your hyperlink to the the email string the insertion point is at the end of the string.  You can use the Selection.StartOf method and move back to the start of the "word" which is actually the email address and the extend property keeps it all selected.  At that point you then apply any special formatting to email address.

    The next important command that you have to issue before continuing with the web address hyperlink, is the Selection.Collapse.  This places the insertion point back where it started, which was at the end of the email address. Now you can go ahead and add the web address to the paragraph.  I left commented out code to show you another way that you could select and format the entire paragraph.

    Again, it's vital that you always collapse the selection before continuing with your next command.

    The Developers Reference is a valuable resource for you as you develop your script. This link takes you to the Selection object

    I hope this helps


    Kind Regards, Rich ... http://greatcirclelearning.com

    • Marked as answer by richardjgreen Friday, February 24, 2012 10:05 PM
    Friday, February 24, 2012 8:30 PM

All replies

  • Hi Richard,

    Thank you for posting.

    I did some test about the problem, I recorded the macro and think that we can use MoveLeft method to get the Selection for the hyperlink. Then, we can change the font size of the hyperlink, here is the code snippet for you to reference:

        Selection.MoveLeft Unit:=wdCharacter, Count:=3, Extend:=wdExtend
        Selection.Font.Size = 14
    Hope this can help you.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, February 23, 2012 9:04 AM
  • Hi Richard,

    Thank you for posting.

    I did some test about the problem, I recorded the macro and think that we can use MoveLeft method to get the Selection for the hyperlink. Then, we can change the font size of the hyperlink, here is the code snippet for you to reference:

        Selection.MoveLeft Unit:=wdCharacter, Count:=3, Extend:=wdExtend
        Selection.Font.Size = 14
    Hope this can help you.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us

    Bruce,

    Thanks for the reply. When using wdCharacter with a Count of 3, I assume this means it would move the caret back three character positions only, so I would need to actually move the caret backward based on the length of the actual strEmail string. I assume I could get the length using Len() and then change the Count:=3 to Count:=intEmailLength?

    Second question I have, is once I've moved the caret back and changed the formatting of the hyperlink, what Selection.MoveRight syntax would move the caret o the end of the currnet line so that I can proceed to the next part of the script?

    Many Thanks
    Richard


    Richard Green MCSE Windows Server 2003, MCTS Desktop Virtualization

    Friday, February 24, 2012 6:20 PM
  • Just for an example below is some sample code that might help explain some things you can do:

    Sub InsertHyperlink()
        Dim doc As Word.Document
        Set doc = Word.ActiveDocument
        Dim strEmail As String: strEmail = "myEmail@myCompany.com"
        Dim strWebAddress As String: strWebAddress = "http://www.myCompany.com"
        Word.Selection.TypeText "Email: "
        doc.Hyperlinks.Add Word.Selection.Range, "mailto:" & strEmail, , , strEmail
        Word.Selection.StartOf unit:=wdWord, Extend:=wdExtend
        Word.Selection.Font.Size = 14
        Word.Selection.Collapse wdCollapseEnd
        Word.Selection.TypeText " | " & "Web: "
        doc.Hyperlinks.Add Word.Selection.Range, strWebAddress, , , strWebAddress
    '    Word.Selection.StartOf unit:=wdParagraph, Extend:=wdExtend
    '    Word.Selection.Font.Bold = True
    '    Word.Selection.Collapse wdCollapseEnd
        Word.Selection.TypeParagraph
    End Sub

    After you have added your hyperlink to the the email string the insertion point is at the end of the string.  You can use the Selection.StartOf method and move back to the start of the "word" which is actually the email address and the extend property keeps it all selected.  At that point you then apply any special formatting to email address.

    The next important command that you have to issue before continuing with the web address hyperlink, is the Selection.Collapse.  This places the insertion point back where it started, which was at the end of the email address. Now you can go ahead and add the web address to the paragraph.  I left commented out code to show you another way that you could select and format the entire paragraph.

    Again, it's vital that you always collapse the selection before continuing with your next command.

    The Developers Reference is a valuable resource for you as you develop your script. This link takes you to the Selection object

    I hope this helps


    Kind Regards, Rich ... http://greatcirclelearning.com

    • Marked as answer by richardjgreen Friday, February 24, 2012 10:05 PM
    Friday, February 24, 2012 8:30 PM
  • Rich,

    Thanks for that. As your code was in VBA and my code is VBScript it took me a while to get it working as I'm not very good (probably an understatement) at cnverting VBA to VBScript, however I managed to get it to work as I'd hoped:

    Const wdParagraph = 4
    Const wdExtend = 1
    Const wdCollapseEnd = 0
    
    objSelection.TypeText "Email: "
    objDoc.Hyperlinks.Add objSelection.Range, "mailto:" & strEmail,,,strEmail
    objSelection.TypeText " | " & "Web: "
    objDoc.Hyperlinks.Add objSelection.Range, strWebAddress,,,strWebAddress
    
    objSelection.StartOf wdParagraph, wdExtend
    objSelection.Font.Color = RGB(128,128,128)
    objSelection.Font.Size = 10
    objSelection.Collapse wdCollapseEnd

    Definng the Constants for the Word properties was the hardest part as I basically had to trial and error my way through the numerical values - It would have been nice is the Developers Reference listed the order that each of the options exists within the array, but sadly it doesn't.

    As I'm not sure what each value I've defined means, I've somehow ended up in a position where I only have to define the caret movements once as opposed to once for the Email element and then a second time for the Web Address element.

    Many Thanks for guiding me to this.
    Richard


    Richard Green MCSE Windows Server 2003, MCTS Desktop Virtualization

    Friday, February 24, 2012 10:05 PM