Hey, Scripting Guy! How Can I Add a Hyperlink to a Microsoft Office Outlook Signature?
Hey, Scripting Guy! I’m trying to use a script to create a Microsoft Office Outlook signature, but I can’t figure out how to add a live hyperlink to that signature. Can you help?
Hey, BK. You know, it’s interesting that you mention signatures. During his last two trips to TechEd Orlando, the Scripting Guy who writes the column has actually been asked for his autograph! (To be honest, we don’t know why, either, although he suspects people just wanted to see if he could spell his own name correctly. And he can … most of the time, anyway.) This Scripting Guy didn’t pay much attention to that until just recently, when he learned that William Shakespeare’s autograph is worth an estimated $5 million. Five million dollars for the autograph of some playwright who’s been dead for almost 400 years?!? Holy smokes; can you imagine how much this same Scripting Guy’s autograph must be worth?!?
Actually, that’s what the folks at Christie’s auction house said, too.
Well, once they stopped laughing, that is.
Note. Good point: maybe we should have asked them about the Scripting Editor’s autograph instead. After all, if she’s not the William Shakespeare of system administration, well, we don’t know who is.
In fact, now that we think about it, she really has contributed as much to system administration and the Script Center as William Shakespeare has, hasn’t she?
Oh, well. We suppose it’s possible that the autograph of the Scripting Guy who writes this column isn’t worth $5 million. On the other hand, what about a script – created by this very same Scripting Guy – that can not only build a new Outlook email signature, but include a hyperlink within that signature? How much would you be willing to pay for that?
Well, to be perfectly blunt, if you said anything but “Nothing” then you got ripped off. After all, we’re giving that script away, absolutely free:
Set objWord = CreateObject("Word.Application") objWord.Visible = True Set objDoc = objWord.Documents.Add() Set objSelection = objWord.Selection objSelection.TypeText "Ken Myer" objSelection.TypeParagraph() Set objRange = objDoc.Paragraphs(2).Range Set objLink = objDoc.Hyperlinks.Add _ (objRange, " http://www.microsoft.com/technet/scriptcenter ", , , "Fabrikam Corporation") Set objSelection = objDoc.Range() objSelection.Font.Name = "Arial" objSelection.Font.Size = 10 Set objEmailOptions = objWord.EmailOptions Set objSignatureObject = objEmailOptions.EmailSignature Set objSignatureEntries = objSignatureObject.EmailSignatureEntries objSignatureEntries.Add "My Signature", objSelection
OK, let’s see what we have here. We kick things off in truly festive fashion, creating an instance of the Word.Application object and then setting the Visible property to True; that gives us a running instance of Microsoft Word that we can see on screen. That also leads to an obvious question: if we’re trying to create a new signature for Microsoft Outlook, then why in the world are loading up Microsoft Word? Shouldn’t we be loading up Outlook instead?
Well, you’d think so, wouldn’t you? As it turns out, however, Outlook’s object model doesn’t include any options for creating new email signatures; for some reason, however, Word’s object model does provide a way to create a new email signature. Why? Who knows; you might as well ask “What is the meaning of life?”
Note. What’s that? What is the meaning of life? Let’s put it this way: the Scripting Guys should be the last people you want answering a question like that.
Once we have an instance of Word up and running we use the Add method to create a new, blank document. We then use this line of code to create an instance of the Word Selection object:
Set objSelection = objWord.Selection
We’ve decided to create a very simple signature here: it’s going to be the user’s name (Ken Myer) followed by a link to the Fabrikam Corporation Web site:
Note. That’s odd. If you look at the URL, it appears as though the Fabrikam Corporation is trying to claim the Script Center as their very own! Oh, well, what the heck; after all, these days pretty much everyone is trying to claim the Script Center as their very own.
Don’t ask. As we’ve noted before, the past few months have been … interesting … ones here at Scripting Guys World Headquarters.
Adding the user name to a signature block is easy; that’s what these two lines of code are for:
objSelection.TypeText "Ken Myer"
As we implied, there’s nothing very complicated about this. In the first line, we’re using the TypeText method to type the user name (Ken Myer) into our document; in line 2, we’re using the TypeParagraph method to replicate the ENTER key. (In other words, having typed in the user name, we now want to press ENTER and start a new paragraph.)
This is where it gets just a tiny bit tricky (but not much). Here’s the code that adds a hyperlink to our email signature:
Set objRange = objDoc.Paragraphs(2).Range
Set objLink = objDoc.Hyperlinks.Add _
(objRange, " http://www.microsoft.com/technet/scriptcenter ", , , "Fabrikam Corporation")
So what’s going on here? Well, we want paragraph 2 to be our hyperlink. Because hyperlinks are child objects of the Range object, we can’t insert a hyperlink without first having a Range object. (If your parents failed to explain this to you: in much the same way that storks are required in order to bring a baby, ranges are needed in order to bring a hyperlink.) In order to insert the hyperlink in paragraph 2 we simply create a Range object that encompasses paragraph 2:
Set objRange = objDoc.Paragraphs(2).Range
Once we’ve done that we can then call the Add method to add a new hyperlink to the Hyperlinks collection:
Set objLink = objDoc.Hyperlinks.Add _
(objRange, " http://www.microsoft.com/technet/scriptcenter", , , "Fabrikam Corporation")
Let’s quickly go over the parameters passed to the Add method:
objRange. The location in the document where we want the hyperlink inserted.
http://www.microsoft.com/technet/scriptcenter. The target of the hyperlink.
Blank. This parameter enables you to specify a bookmark or other named range to help pinpoint a specific location on the hyperlink target. We didn’t have any use for this, so we left it blank.
Blank. An optional tooltip that appears any time the mouse hovers over the hyperlink. If left blank (as we left it here) the hyperlink target will be used as a tooltip.
Fabrikam Corporation. The actual text of the hyperlink.
That brings us to this block of code:
Set objSelection = objDoc.Range()
objSelection.Font.Name = "Arial"
objSelection.Font.Size = 10
In the first line we’re using the syntax objDoc.Range() to select all the text in our little Word document. That’s important; only the selected text will be included in our email signature. Lines 2 and 3 are optional. The way we have Word configured (for better or worse) our default paragraph text is different from the text used when we programmatically add in a hyperlink. To take care of that problem, we simply changed the font Name of the selected text to Arial, and the font Size to 10.
Besides, this gives you a hint or two as to how you can modify the appearance of your email signature.
At this point we’re ready to create our new email signature; up until now all we’ve done is create a Word document, add some text to it, then selected and formatted all that text. (Hmmm, now that you mention it that is quite a bit of work in and of itself, isn’t it?) In order to create the new signature we first need to execute these three lines of code:
Set objEmailOptions = objWord.EmailOptions
Set objSignatureObject = objEmailOptions.EmailSignature
Set objSignatureEntries = objSignatureObject.EmailSignatureEntries
We won’t bother going through this in exhaustive detail today; after all, we do have a TechNet Magazine article that discusses email signatures and how to create them. (In that article we also show you how to programmatically make your new signature the default signature for all your email.) What we will do is note that we can’t add a new signature until we bind to the EmailSignatureEntries collection; in order to do that we first bind to the EmailOptions object, connect to the EmailSignature object and then – at long last – hook up with the EmailSignatureEntries collection. Once we’ve done that we can use this line of code to create the new signature:
objSignatureEntries.Add "My Signature", objSelection
All we’re doing here is calling the Add method and passing this method two parameters: the name of the new signature (My Signature) and the object reference for the selected text (objSelection). The minute we do that we’ll have ourselves a new email signature. (To verify that, go into Outlook, click Tools, then click Options. In the Options dialog box, on the Mail Format tab, click Signatures. You should see your new signature listed, in all its glory.)
That’s about all the time we have for today, BK; if you have additional questions either drop us a line or take a peek at our TechNet Magazine article. (Which you may have already done.) Incidentally, we checked eBay and didn’t see any Scripting Guy autographs currently up for auction. However, you can short-circuit the auction process and buy a Ray Charles autograph for a mere $750,000. (Supposedly, a Ray Charles autograph is the “Holy Grail” of autograph collectors.) Granted, that’s a lot of money, but at least the shipping is free. By contrast, if you decide to buy a Ulysses S. Grant/Robert E. Lee dual autograph for $19,999.99 you’ll also be asked to pay $50 in shipping charges.
Which seems only fair.
Note. In case you’re wondering, at the moment the high bid for several autographs – including football player Jordan Palmer and baseball player Gary Matthews – is 1 penny. That’s the same bid for any one of 40 unauthenticated signatures of “the great John R. Dowell.” And no, we have no idea who the great John R. Dowell is. But that’s OK; after all, we doubt that the great John R. Dowell has any idea who the Scripting Guys are, either.