Office Space: Tips and Tricks for Scripting Microsoft Office Applications
Welcome to Office Space, the new column that offers tips and tricks for scripting Microsoft® Office applications. We’ll post new tips every Tuesday and Thursday; to see an archive of previous tips, visit the Office Space Archive. And if you have particular questions about Microsoft Office scripting, feel free to send them to scripter@microsoft.com (in English, if possible). We can’t promise to answer all the questions we receive, but we’ll do our best.
Inserting AutoText Using a Script
Let’s be honest: one of the main reasons scripting appeals to so many of us is because we’re lazy. Scripts don’t do anything we couldn’t do ourselves; it’s just that we’re too lazy or too busy to want to do them ourselves. And so we let the script do the work for us. The work still gets done, but we don’t have to do it. If we could just find a way to get the lawn mowed in automated fashion life would be perfect.
Well, almost perfect. After all, you still have a bit of work to do any time you write a script. For example, you might be using Microsoft Word scripts to automatically create reports. That’s great, but suppose all your reports must include a fancy corporate logo and a legal disclaimer. That’s not a huge deal, but you will have to write the code to insert and format these items. And, like it or not, there’s just no way around that.
Well, unless you use an AutoText entry, of course.
What’s AutoText? Well, AutoText provides a way to quickly and easily insert boilerplate text; that is, text that you find yourself using over and over again. What’s cool about AutoText is that it’s not limited to plain text; you can also insert fancy formatted text. We won’t spend a lot of time talking about AutoText itself; after all, this is a scripting column. But here’s an example of some fancy text; note both the font formatting and the accompanying graphic:
To turn this into an AutoText entry all we have to do is highlight the text and the graphic, then click Tools, AutoCorrect Options. In the AutoCorrect dialog box, on the AutoText tab, type a name for your new AutoText entry (say, Dr. Scripto) and then click Add. After that, any time you’re working in Word you can insert this cool snippet of text by bringing up the AutoCorrect dialog box, selecting the AutoText entry named Dr. Scripto, and then clicking Insert.
Pretty slick, huh? But what if you aren’t actually working in Word, what if you’re using a script to create a Word document? Is there any way to insert an AutoText entry using a script?
You bet there is. First, we need to create an instance of the Word Range object; that’s simply so we can specify a location in the document where the AutoText entry should be inserted. Here’s code that creates an instance of the Word.Application object (setting the Visible property to True, so we can see what’s going on), adds a new document, and then creates a new range object at the beginning of that document:
On Error Resume Next
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add()
Set objRange = objDoc.Range
Second, we need to specify the AutoText entry to be inserted. AutoText entries are actually part of the document template; consequently, we need to use the AttachedTemplate property to create an instance of the Template object:
Set objTemplate = objDoc.AttachedTemplate
After we have a Template object (one based on the AttachedTemplate property) we indicate the desired AutoText entry using syntax like this, with the entry name shown in parenthesis:
objTemplate.AutoTextEntries("Dr. Scripto")
To this we add the Insert method, specifying the Range object we created earlier as the insertion point. Again, all this takes only a single line of code:
objTemplate.AutoTextEntries("Dr. Scripto").Insert objRange
Here’s a complete script that creates a new document and then inserts an AutoText entry named Dr. Scripto:
On Error Resume Next
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add()
Set objRange = objDoc.Range
Set objTemplate = objDoc.AttachedTemplate
objTemplate.AutoTextEntries("Dr. Scripto").Insert objRange