Hey, Scripting Guy! How can I insert a Wingding symbol into a Word document?
— RC
Hey, RC. In case you’re wondering, there were two reasons why we decided to answer your question. First, we just liked the phrase insert a Wingding symbol. (No, we don’t know why we liked it, we just did.) Second – and by far most important – this is an easy one, one we can answer with just a few lines of code.
As we’ll see, the code for inserting a symbol into a Word document is trivial; what’s tricky is figuring out which symbol you want to insert (as well as the character code for that symbol). Perhaps the easiest way to do this (although we’ll show you another method at the end of today’s column) is to open up the Symbol dialog box, select the desired font, and then click on the symbol you want to insert:
As you can see, we’ve selected the smiley face. When we do so, the character code for the smiley face is listed in the Character code box. Now that we know the font we want to use (Wingdings) and the character code for our symbol (74), we’re ready to write a script that inserts the smiley face into a Word document:
Set objWord = CreateObject(“Word.Application”) objWord.Visible = True Set objDoc = objWord.Documents.Add() Set objSelection = objWord.SelectionobjSelection.InsertSymbol 74, “Wingdings”
We told you the code was trivial. We begin by creating an instance of the Word.Application object, and then set the Visible property to True. We call the Add() method to create a new document, then create an instance of the Word Selection object.
At that point all we need is one line of code in order to insert the smiley face symbol:
objSelection.InsertSymbol 74, “Wingdings”
That’s it: we call the InsertSymbol method followed by the desired character code and the desired font. Run the script, and you should see a smiley face in your document. (It is a miracle, isn’t it?)
If you like using symbols it might be worth your while to create a catalog of symbols and their character codes. Here’s a script that creates a Word document containing all the symbols and their character codes found in the Wingdings font (character codes 32 through 255). You can easily modify the script to give yourself a catalog of symbols for Wingdings2, Wingdings3, Webdings, Bookdings, and any other ding-dang font you want.
Here’s the code:
Set objWord = CreateObject(“Word.Application”) objWord.Visible = True Set objDoc = objWord.Documents.Add() Set objSelection = objWord.SelectionFor i = 32 to 255 objSelection.TypeText i & ” — ” objSelection.InsertSymbol i, “Wingdings” objSelection.TypeParagraph() Next
And here’s what the resulting document looks like:
It’s not quite the IKEA catalog, but it’s close.
0 comments