Dieser Inhalt ist in Ihrer Sprache leider nicht verfügbar. Im Folgenden finden Sie die englische Version.
Diese Dokumentation wurde archiviert und wird nicht länger gepflegt.
Hey, Scripting Guy! Setting Up Outlook E-Mail Signatures
The Microsoft Scripting Guys work for—well, are employed by—Microsoft. When not playing/coaching/watching baseball (and various other activities) they run the TechNet Script Center. Check it out at www.scriptingguys.com.
© 2008 Microsoft Corporation and CMP Media, LLC. All rights reserved; reproduction in part or in whole without permission is prohibited.
The Microsoft Scripting Guys
Download the code for this article: ScriptingGuys2006_10.exe (151KB)
You know, some things are hard to resist, even when we know better. Take carnival games. Everyone knows that these games are often rigged (pac-c.org/Carnies.htm) and yet you just have to try them. After all, rigged or not, how hard can it be to break a balloon with a dart? Land a dime inside a goblet? Toss a basketball into a peach basket? Come on, it's easy! Well, maybe if we put a little more backspin on the ball...
In some ways, Microsoft® Outlook® is the ring-toss of the scripting world. Ring-toss looks ridiculously easy: you just throw a big ring over a relatively tiny little peg. As it turns out, though, some of the pegs are angled just right, making it nearly impossible for a ring to land on them. Likewise, there are lots of things you'd like to script with Outlook that look so easy. Create a new Outlook signature and then make that your default signature for all outgoing e-mail? Come on, how hard could that be? Well, maybe if we put a little more backspin into our script.
As many of you have discovered, this isn't as easy as it seems. Not that people don't try anyway: we Scripting Guys have received scores of sample scripts in which people have tried everything from manipulating the Outlook object model to modifying values in the registry to creating some weird sort of .INI file, all in a futile attempt to create and assign an Outlook signature. Apparently, Outlook signatures are rigged like the carnival games, and you just can't win.
Or can you? Although some carnival games are designed so that you can't win, others actually can be beaten; you just need to know the secret. For example, when throwing a basketball into a peach basket, just remember that the angle of incidence equals the angle of refraction. The same is true of Outlook: when trying to create and assign a new Outlook e-mail signature, just remember that the angle of incidence equals the angle of refraction. Oh, and use Microsoft Word to create and assign the signature.
That's right, Microsoft Word. Don't bother asking us why; you might as well ask us why the angle of incidence equals the angle of refraction. (No, we don't know the answer to that, either.) To tell you the truth, we aren't sure why the Microsoft Word object model includes methods for creating and assigning e-mail signatures while the Microsoft Outlook object model doesn't. But we Scripting Guys learned long ago that it's often better to just accept things as they are and not ask too many questions.
Here's the answer to why you couldn't knock a stuffed cat over by throwing a baseball at it: in some cases, at least, the shelves are too wide to cause the cat to fall off. (In the carnival game it's usually not enough to knock the cat over, you have to knock it clear off the shelf to win.) And there are times when the space between the back wall and the shelf might be too small to allow the cat to fall through. And, of course, you do have to hit the cat with your throw to begin with. For some of us, that's easier said than done (certainly not for us, mind you).
Let's forget stuffed cats and see what we can do with e-mail signatures. To begin with, let's try to create a new e-mail signature. As with the balloon dart toss (hint: surreptitiously heat the tip of the dart with a cigarette lighter before throwing) there's a trick involved here. To create a new e-mail signature you actually need to create a Word document, type in the signature (along with any fancy formatting you might want to add), select the text, and then create the signature. We know, that sounds complicated. But as you're about to see, it's really not all that bad. And much easier than heating the end of a dart with a cigarette lighter without the carny seeing you.
Figure 1 shows the code required for creating a new e-mail signature. Let's see if we can figure out what's going on here. We begin by creating an instance of the Word.Application object, and then setting the Visible property to True. (We don't really need to set the Visible property to True; we do that just so you can watch the excitement unfold onscreen.) We use the Add method to add a new, blank document to our instance of Word, then use this line of code to create an instance of the Word Selection object:
Set objSelection = objWord.Selection
Set objWord = CreateObject("Word.Application") objWord.Visible = True Set objDoc = objWord.Documents.Add() Set objSelection = objWord.Selection objSelection.TypeText "Ken Myer" objSelection.TypeParagraph() objSelection.TypeText "Fabrikam Corporation" Set objSelection = objDoc.Range() Set objEmailOptions = objWord.EmailOptions Set objSignatureObject = objEmailOptions.EmailSignature Set objSignatureEntries = objSignatureObject.EmailSignatureEntries objSignatureEntries.Add "Scripted Entry", objSelection
In this case, the Selection object merely positions the cursor at the beginning of the document, giving us a place to start typing. We can then use these three lines of code to enter the new e-mail signature:In other words, we want a signature that looks like this:
objSelection.TypeText "Ken Myer" objSelection.TypeParagraph() objSelection.TypeText "Fabrikam Corporation"
Granted, it's not very fancy, but it will do for now. And, needless to say, you can change fonts, put things in boldface, add images, add borders—basically do just about anything else you can do to a Word document. For educational purposes we just wanted to keep our example as simple as possible. (Yes, simple, just like the throw-a-ping-pong-ball-in-a-bowl-and-win-a-goldfish game, one of the few carnival games you really can win, mainly because you pay a dollar for the chance to win a goldfish that would cost you 50 cents at the pet store.)
Once the signature is complete, we use the following line of code to create a range encompassing all the text in the document:Think of this as being equivalent to pressing Ctrl+A or choosing Select All from the Edit menu.
Set objSelection = objDoc.Range()
That brings us to the next few lines of code that create a series of object references:Why all this code? Well, the EmailOptions object is a child object of the Word.Application object; in the first line we're simply creating an instance of the EmailOptions object. In turn, the EmailSignature object is a child of EmailOptions, and the EmailSignatureEntries collection is a child of EmailSignature. We actually need to add the new signature to the EmailSignatureEntries collection; it just takes us awhile to work our way down to that collection.
Set objEmailOptions = objWord.EmailOptions Set objSignatureObject = _ objEmailOptions.EmailSignature Set objSignatureEntries = _ objSignatureObject.EmailSignatureEntries
Once we get there, however, it takes only a single line of code in order to create a new e-mail signature named Scripted Entry:
objSignatureEntries.Add _ "Scripted Entry", objSelection
As you can see, all we do is call the Add method and pass two parameters: the name of the new signature (Scripted Entry) and the object reference for the selected text (objSelection). Just like that you'll have a new e-mail signature within Word and Outlook. We tried this first with Word configured as the default e-mail editor and then without. The results were the same: you get the new signature regardless.
Of course, while that creates a new e-mail signature it doesn't actually assign the new signature to all outgoing messages. To do that you need to modify one (or both) of the following EmailSignature object properties: NewMessageSignature indicates the signature to be tacked on to the end of any new e-mail messages, and ReplyMessageSignature indicates the signature to be tacked on to the end of any e-mail replies.
Here's a sample script that assigns the new signature (Scripted Entry) to both the NewMessageSignature and ReplyMessageSignature properties:It's that easy.
Set objWord = CreateObject("Word.Application") objWord.Visible = True Set objEmailOptions = objWord.EmailOptions Set objSignatureObject = _ objEmailOptions.EmailSignature objSignatureObject.NewMessageSignature = _ "Scripted Entry" objSignatureObject.ReplyMessageSignature = _ "Scripted Entry"
If Outlook is already open these changes are applied to your default profile; this will also be the case if you have only a single Outlook profile. If you have multiple Outlook profiles, however, you'll be prompted to select a profile when the script runs. To bypass that prompt, just have your script open Outlook with the desired profile. For more information go to—where else?—the TechNet Script Center: microsoft.com/technet/scriptcenter/resources/officetips/jun05/tips0614.mspx.
What if you don't want an e-mail signature? Is it possible to set the signature options to None? Of course, just set the value of the two properties to an empty string (""):As nice as this is, it's still a little like paying a dollar to win a 50-cent goldfish: after all, having to edit a script each time you want to create a signature for a different user isn't that much better than creating that signature by hand. Now that we know how to create and assign an e-mail signature, let's see if we can come up with a script that's a bit more practical. For example, here's a script that:
Set objWord = CreateObject("Word.Application") objWord.Visible = True Set objEmailOptions = objWord.EmailOptions Set objSignatureObject = _ objEmailOptions.EmailSignature objSignatureObject.NewMessageSignature = "" objSignatureObject.ReplyMessageSignature = ""
- Binds to the Active Directory® user account for the logged-on user, retrieving the user's name, job title, department, company, and phone number.
- Creates a new instance of Word and a blank Word document. (Note that this time we don't set the Visible property to True—that way Word never appears onscreen while the script runs.)
- Creates a new e-mail signature (named AD Signature) based on property values retrieved from Active Directory.
- Assigns the new e-mail signature to both the NewMessageSignature and ReplyMessageSignature properties.
- Exits Word.
Is that more like it? Figure 2 shows the script. Figure 3 shows what one of these Active Directory-derived signatures might look like. If you're looking for a way to create a corporate-wide e-mail signature, well, this might be the very thing you've been looking for.
On Error Resume Next Set objSysInfo = CreateObject("ADSystemInfo") strUser = objSysInfo.UserName Set objUser = GetObject("LDAP://" & strUser) strName = objUser.FullName strTitle = objUser.Title strDepartment = objUser.Department strCompany = objUser.Company strPhone = objUser.telephoneNumber Set objWord = CreateObject("Word.Application") Set objDoc = objWord.Documents.Add() Set objSelection = objWord.Selection Set objEmailOptions = objWord.EmailOptions Set objSignatureObject = objEmailOptions.EmailSignature Set objSignatureEntries = objSignatureObject.EmailSignatureEntries objSelection.TypeText strName & ", " & strTitle objSelection.TypeParagraph() objSelection.TypeText strDepartment objSelection.TypeParagraph() objSelection.TypeText strCompany objSelection.TypeParagraph() objSelection.TypeText strPhone Set objSelection = objDoc.Range() objSignatureEntries.Add "AD Signature", objSelection objSignatureObject.NewMessageSignature = "AD Signature" objSignatureObject.ReplyMessageSignature = "AD Signature" objDoc.Saved = True objWord.Quit
Figure 3 Deriving the Signature from Active Directory (Click the image for a larger view)
That was easy, wasn't it? Now, you look pretty strong and healthy to us—we bet it would be a cinch for you to knock down these empty milk cans with a baseball. What do you say? Just one dollar a throw.