TweetFollow Us on Twitter

Introduction to Scripting Microsoft Word

Volume Number: 23 (2007)
Issue Number: 01
Column Tag: Applescript Essentials

Introduction to Scripting Microsoft Word

by Benjamin S. Waldie

Lately, there has been a lot of talk in the Macintosh community about Microsoft, and the forthcoming Office 12. One of Microsoft's big announcements is that they will be doing away with support for creating and running Visual Basic macros in the next version of Office. This leaves many people wondering how they will go about automating their Office applications. AppleScript to the rescue. I'm pleased to say that Entourage, Excel, PowerPoint, and Word are all AppleScriptable.

Office has supported AppleScript for some time now, and with the release of Office 11 in 2004, Microsoft actually completely re-implemented much of their AppleScript support, and also added AppleScript support for PowerPoint. Due to these changes, much of the AppleScript terminology in Word and Excel changed from previous versions. If you are currently using Office AppleScripts with a pre-2004 version of Office, then please be aware that you will probably need to make some modifications to your scripts when you decide to upgrade your Office suite. Of course, this should go without saying when upgrading any scriptable application. Any time any application is updated, AppleScript terminology changes may be introduced. This is why it is always a good idea to test your existing scripts with any new application version before implementing it into your live workflow.

This month, we're going to take a look at scripting Microsoft Word. We'll walk through some basic techniques for interacting with Word documents, and the content within those documents. Please note that all code in this month's column was written for and tested with Office 11 (2004). Therefore, if you're using a different version of Office, please be aware that the terminology you need to use may differ from that which I have used.

Working with Documents

Making a Document

Making a new Word document is relatively straightforward. To make a new Word document, simply use the make command, as demonstrated here.

tell application "Microsoft Word"
   make new document
end tell
--> document "Document1" of application "Microsoft Word"

Notice that the result of the make command is a reference to the newly created document. This reference may be placed into a variable, if desired, for future reference in your script.

Closing a Document

Closing a Word document is also pretty straightforward. To close a Word document, use the close command. Optionally, you may specify a constant value (yes, no, or ask) for the close command's saving parameter, to indicate whether the document being closed should be saved. The following example code will close a document without saving it.

tell application "Microsoft Word"
   close document 1 saving no
end tell

Opening a Document

To open a Word document, use the open command, followed by a reference to the document file you wish to open. The following example code will prompt the user to select a Word document file. It will then open that file.

set theDocFile to choose file with prompt "Please select a Word document file:"
tell application "Microsoft Word"
   open theDocFile
end tell

Please note that, in the example above, no result is returned by the open command. Since you will typically want your script to perform additional tasks on the newly opened document, you will need a way of referencing the document. There are a number of ways that this can be done. Assuming you know the name of the document, one way to do this is to retrieve a reference to the document using its name. For example:

set theDocFile to choose file with prompt "Please select a Word document file:"
set theDocName to name of (info for theDocFile)
tell application "Microsoft Word"
   open theDocFile
   set theDocument to document theDocName
end tell
--> document "My Document.doc" of application "Microsoft Word"

If you're sure that the target document will be in the front, then another way you can reference it is by referencing the active document application property. For example:

set theDocFile to choose file with prompt "Please select a Word document file:"
tell application "Microsoft Word"
   open theDocFile
   set theDocument to active document
end tell
--> active document of application "Microsoft Word"

Take note that the result of the code above is a reference to the active document. This may work fine in most cases. However, be aware that, since it is not referencing a specific document, if another document is brought to the front, then the incorrect document may be targeted.

It's also important to note is that, in Word, documents have an index number. However, this number does not indicate the front to back position of the document, as it often does in many other applications. Instead, it indicates the document's position, in the order that the documents were opened. In other words, if a document in the back was the last document to be opened, then the front document will not be document 1. For this reason, when you want to target the front document in Word, it's always good practice to reference the active document.

We have discussed a couple of ways to retrieve a reference to a newly opened document. There are others, some of which are more robust, and I would encourage you to see if you can come up with some ways of doing this on your own.

Saving a Document

Saving a previously saved document into its original location is as simple as using the save command, as demonstrated here.

tell application "Microsoft Word"
   save active document
end tell

However, suppose you want to save a document into a specific location, or in a different format? This is done using Word's save as command. This command has several optional parameters, which will allow you to specify the output location, format, and more. To save a document into a specific location, make use of the file name parameter, as follows:

set theOutputPath to (path to desktop folder as string) & "My Saved Doc.doc"
tell application "Microsoft Word"
   save as active document file name theOutputPath
end tell

To specify a format for the saved document, use the file format parameter. For example, the following code will save a document in RTF format:

set theOutputPath to (path to desktop folder as string) & "My Saved Doc.rtf"
tell application "Microsoft Word"
   save as active document file name theOutputPath file format format rtf
end tell

There are numerous other formats in which you can save a Word document, including Word template, HTML, web archive, and more. A complete list of supported formats can be found in Word's AppleScript dictionary, under the save as command.

Working with Document Text

Working with Text Ranges

In a Word document, text is typically referenced using text ranges. A text range is a class of object that refers to a specific area of text, such as a single character, word, or paragraph, or all of the text within the document. Each text range has a starting and ending position. For example, a text range might represent character 1 through character 5 of the document. The position 0 represents the absolute beginning of a document, just before the first character, so if you wanted to reference the first 5 characters of a document, the text range would actually begin at position 0, and end at position 5.

To reference text within a Word document, you must first create a text range. This is done by using the create range command. This command simply creates a reference for you, to the specified text content. Here's an example:

tell application "Microsoft Word"
   tell active document
      set theRange to create range start 0 end 5
   end tell
end tell
--> text range id «data iWrg0000000000000005» ¬
of active document of application "Microsoft Word"

The result of the code above is a reference to a range of text within my document, in this case, characters 1 through 5. I can now access attributes of this text by referencing the range. For example, the following code will get the text content within the range:

tell application "Microsoft Word"
   tell active document
      set theRange to create range start 0 end 5
      content of theRange
   end tell
end tell
--> "APPLE"

Getting the Entire Content of a Document

In Word, the document class possesses a text object property. This property references a text range representing the entire content of the document. For example:

tell application "Microsoft Word"
   tell active document
      content of text object
   end tell
end tell
--> "APPLESCRIPT ESSENTIALS
      Introduction to Scripting Microsoft Word
      Copyright 2007 by Ben Waldie...

Adding Text to a Document

To replace text in a Word document, create a text range representing the text to be replaced. Then set the content property of that range to the desired text. For example:

tell application "Microsoft Word"
   tell active document
      set theRange to create range start 0 end 5
      set content of theRange to "TEST"
   end tell
end tell

Another way that text can be added to a document is by inserting it. This is done by using the insert command. This command has a required parameter, text, which indicates the text to be inserted. It also has an optional parameter, at, which can be used to indicate the location before which the text should be inserted. For example, the following code will insert the text TEST at the beginning of the active document.

tell application "Microsoft Word"
   tell active document
      set theRange to create range start 0 end 0
      insert text "TEST" at theRange
   end tell
end tell

This same task could also have been accomplished by setting the content property of a range representing the beginning of the document to the specified text. For example:

tell application "Microsoft Word"
   tell active document
      set theRange to create range start 0 end 0
      set content of theRange to "TEST"
   end tell
end tell

Working with Other Content

It's also possible to add other types of elements besides text into Word documents via scripting. For example, you might want to insert a hyperlink, or an image.

Adding a Hyperlink to a Document

To add a hyperlink to a document, use the make command to create a hyperlink object. In doing so, you may specify property values for the hyperlink, including the text to be displayed the link URL, and the text range representing the location where the hyperlink should be created. For example, the following code will create a hyperlink to my company's website at the beginning of the active document.

tell application "Microsoft Word"
   tell active document
      set theRange to create range start 0 end 0
      make new hyperlink object at end with properties ¬
{text to display:"Automated Workflows, LLC", hyperlink ¬
address:"http://www.automatedworkflows.com", ¬
text object:theRange} end tell end tell --> hyperlink object "http://www.automatedworkflows.com" ¬
of active document of application "Microsoft Word"

Adding a Picture to a Document

Adding an inline picture to a document is similar to adding a hyperlink. Again, use the make command, but this time create an inline picture, rather than a hyperlink object. When using this command, you may specify property values for the picture, including the file name property, which should include the path of the picture file to be inserted (as a string). For example, the following code will create a new inline picture at the beginning of the active document.

set thePicturePath to choose file with prompt "Please select a picture to insert:"
tell application "Microsoft Word"
   tell active document
      set theRange to create range start 0 end 0
      make new inline picture at theRange with properties ¬ 
{file name:thePicturePath as string} end tell end tell --> inline picture 1 of text range id ¬
«data iWrg0000000000000000» of document "Document1" ¬
of application "Microsoft Word"

In Closing

Now that we have discussed some basic interaction with Word, where do you go from here? We have really only scratched the surface, and there's a lot more that can be done. Take some time to explore modifying attributes of text, such as the font, point size, and color. Explore modifying document properties, adjusting header and footer content, inserting a table of contents, and much more. Take some time to explore Word's AppleScript dictionary. You will find that it contains quite a lot of terminology, so it may actually take some time to become familiar and comfortable with it.

For additional help getting started with Word, be sure to review the Microsoft Word 2004 AppleScript Reference documentation. This documentation can be obtained in the Resources > Development Center > AppleScript Resources for Office 2004 section of Microsoft's Mactopia website at http://www.microsoft.com/mac/. You may also want to explore MacScripter.net's ScriptBuilders, at http://scriptbuilders.net/, where you will find some AppleScripts for Microsoft Word, some of which may even be editable!

Until next time, keep scripting!


Ben Waldie is the author of the best selling books "AppleScripting the Finder" and the "Mac OS X Technology Guide to Automator", available from http://www.spiderworks.com, as well as an AppleScript Training CD, available from http://www.vtc.com. Ben is also president of Automated Workflows, LLC, a company specializing in AppleScript and workflow automation consulting. For years, Ben has developed professional AppleScript-based solutions for businesses including Adobe, Apple, NASA, PC World, and TV Guide. For more information about Ben, please visit http://www.automatedworkflows.com, or email Ben at ben@automatedworkflows.com.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

1Password 7.9.5 - Powerful password mana...
1Password is a password manager that uniquely brings you both security and convenience. It is the only program that provides anti-phishing protection and goes beyond password management by adding Web... Read more
Alfred 4.6.6 - Quick launcher for apps a...
Alfred is an award-winning productivity application for OS X. Alfred saves you time when you search for files online or on your Mac. Be more productive with hotkeys, keywords, and file actions at... Read more
PDFKey Pro 4.5.0 - Edit and print passwo...
PDFKey Pro can unlock PDF documents protected for printing and copying when you've forgotten your password. It can now also protect your PDF files with a password to prevent unauthorized access and/... Read more
Default Folder X 5.6.5 - Enhances Open a...
Default Folder X attaches a toolbar to the right side of the Open and Save dialogs in any OS X-native application. The toolbar gives you fast access to various folders and commands. You just click on... Read more
TeamViewer 15.30.3 - Establish remote co...
TeamViewer gives you remote control of any computer or Mac over the Internet within seconds, or can be used for online meetings. Find out why more than 200 million users trust TeamViewer! Free for... Read more
Adobe Animate 22.0.6 - Animation authori...
Adobe Animate is available as part of Adobe Creative Cloud for as little as $20.99/month (or $9.99/month if you're a previous Flash Professional customer). Adobe Animate (was Flash CC) lets you share... Read more
Carbon Copy Cloner 6.1.2 - Easy-to-use b...
Carbon Copy Cloner backups are better than ordinary backups. Suppose the unthinkable happens while you're under deadline to finish a project: your Mac is unresponsive and all you hear is an ominous,... Read more
Maintenance 2.9.0 - System maintenance u...
Maintenance is a system maintenance and cleaning utility. It allows you to run miscellaneous tasks of system maintenance: Check the the structure of the disk Repair permissions Run periodic scripts... Read more
OnyX 4.1.9 - Maintenance and optimizatio...
OnyX is a multifunction utility that you can use to verify the startup disk and the structure of its system files, to run miscellaneous maintenance and cleaning tasks, to configure parameters in the... Read more
Notion 2.0.22 - A unified workspace for...
Notion is the unified workspace for modern teams. Notion Features: Integration with Slack Documents Wikis Tasks Release notes were unavailable when this listing was updated. Download Now]]> Read more

Latest Forum Discussions

See All

Sony Reveals Aggressive Mobile Growth Pl...
Last year, Sony revealed that it is exploring the mobile market with “wonderful PlayStation franchises". Fast forward to today, we haven’t seen much of the results of that, but the company revealed in its corporate strategy meeting under the Game... | Read more »
Out Now: ‘Streets of Rage 4’, ‘Catalyst...
Each and every day new mobile games are hitting the App Store, and so each week we put together a big old list of all the best new releases of the past seven days. Back in the day the App Store would showcase the same games for a week, and then... | Read more »
SwitchArcade Round-Up: ‘Amazing Superher...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for May 25th, 2022. As many Wednesdays of late have been, it’s a little quiet today. We have a handful of new releases to check out, with summaries for each. Beyond that, it’s just the... | Read more »
An Interview With Playdigious on Bringin...
Playdigious have brought over many great console and PC games to mobile over the years. These range from smaller-scale projects to ones that get regular updates and paid DLC. | Read more »
‘The Battle of Polytopia’ Gets Huge Dipl...
The Battle of Polytopia (Free) from Midjiwan is an excellent showcase for a game being designed to play great on touchscreens. | Read more »
Side-scrolling action game ‘Blue’s Journ...
This week’s new ACA NeoGeo classic from SNK and Hamster is the side-scrolling action game Blue’s Journey ($3.99) on both iOS and Android. Blue’s Journey is a side-scrolling action game that debuted back in 1991. Your aim is to save the planet Raguy... | Read more »
‘Genshin Impact’ Gets a New Character Tr...
The major Genshin Impact (Free) 2.7 releases next week across all platforms. Developer HoYoverse today revealed a new character trailer for Yelan from next week’s update. This update will also include a new Archon Quest in the Chasm unveiling Liyue... | Read more »
The 10 Best Strategy RPGs on Nintendo Sw...
We’ve covered many genres in our SwitchArcade Special Editions so far, but there’s one that I adore and play whenever I need to relax. The strategy RPG genre. Now before getting into the games, I want to clarify that by Strategy RPGs, I’m going to... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for May 24th, 2022. In today’s article, we’ve first got a little news to check out. After that, I have a couple of reviews for you to enjoy. Both games are quite good, actually. From... | Read more »
‘Void-X’ is a Brutal Bullet Hell Shooter...
In the fall of 2020 developer James Swiney tickled our nostalgia bones with the highly enjoyable Zelda homage Aruna’s Adventure, which we picked as our Game of the Week on release. Prior to that though, Swiney made a mark in the horizontal shoot ’em... | Read more »

Price Scanner via MacPrices.net

Memorial Day Weekend iPhone Sale: Free $300 g...
Memorial Day Weekend iPhone sales are now live at Verizon’s low-cost wireless cell service, Visible. They will include a free $300 gift card with the purchase of a new 2022 iPhone 13 Pro Max, 13 Pro... Read more
In stock today: 13″ M1 MacBook Air for $899,...
B&H has Apple’s base 13″ M1 MacBook Air (Space Gray) in stock and on sale today for $100 off MSRP, only $899. Free 1-2 day shipping is available to most addresses in the US: 8-Core M1 Apple... Read more
In stock today: Open-box 16″ MacBook Pro with...
Having trouble finding a 16″ M1 Pro MacBook Pro to buy? QuickShip Electronics has open-box return 16″ M1 Pro MacBook Pros in stock and on sale for $500 off MSRP on their eBay store right now, each... Read more
16″ MacBook Pro with Apple M1 Pro CPU in stoc...
Stock of Apple’s 16″ MacBook Pro can be hard to find at resellers today. However, Apple reseller Expercom is reporting stock of the Silver 16″ MacBook Pro with a 10-Core M1 Pro CPU and 512GB SSD... Read more
Amazon has select 13″ M1 MacBook Airs on sale...
Amazon has select Apple 13″ M1 MacBook Airs on sale for $100 off MSRP today, only $899. Amazon does not have stock of these MacBooks, but they are available for order at the discounted price. The... Read more
Deal Alert! 14″ 10-Core M1 Pro MacBook Pros a...
Amazon has 14″ MacBook Pros with a 10-Core M1 Pro CPUs on sale today for $2249.99 shipped. That’s $250 off Apple’s MSRP. Amazon’s prices are the lowest currently available for 10-Core 14″ MacBook... Read more
24″ M1 iMacs with 8-Core CPU/8-Core GPU/512GB...
Amazon has 24″ M1 iMacs (8-Core CPU/8-Core GPU/512GB SSD) on sale for $1499.99 for a limited time, $200 off Apple’s MSRP. All colors are discounted, and their prices are the lowest available for... Read more
This wireless carrier is offering your choice...
Choose between a free 2022 iPhone SE or an iPhone 12 if you open a new line of service at Verizon. Value of the phone you choose is spread over a 36 month period of Unlimited service. No trade-in is... Read more
Details of Apple’s 2022 iPad discounts for Ed...
Apple will take $100 off 12″ M1 iPad Pros, $50-$100 off 11″ M1 iPad Pros, $50 off iPad Airs, $50 off 8.3″ iPad minis, & $20 off 10.2″ iPads for all teachers, students, and staff of any... Read more
Apple has a range of M1 and Intel-powered Mac...
Apple has a full line of Certified Refurbished M1-powered as well as previous-generation Intel-based Mac minis back in stock today starting at only $589, Certified Refurbished. Models with varying... Read more

Jobs Board

*Apple* Engineering Specialist - General Dyn...
…IT Service Delivery solutions to SEC. Currently, we are seeking an Apple Engineering Specialist in Washington, DC The responsibilities for candidates in this Read more
*Apple* Engineering Specialist - General Dyn...
…IT Service Delivery solutions to SEC. Currently, we are seeking an Apple Engineering Specialist in Washington, DC The responsibilities for candidates in this Read more
Sephora Beauty Advisor - *Apple* Blossom Ma...
Sephora Beauty Advisor - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.