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

Latest Forum Discussions

See All

Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »
Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links below... | Read more »
Marvel Future Fight celebrates nine year...
Announced alongside an advertising image I can only assume was aimed squarely at myself with the prominent Deadpool and Odin featured on it, Netmarble has revealed their celebrations for the 9th anniversary of Marvel Future Fight. The Countdown... | Read more »

Price Scanner via MacPrices.net

Every model of Apple’s 13-inch M3 MacBook Air...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices are the lowest currently available for new 13″ M3 MacBook Airs among... Read more
Sunday Sale: Apple iPad Magic Keyboards for 1...
Walmart has Apple Magic Keyboards for 12.9″ iPad Pros, in Black, on sale for $150 off MSRP on their online store. Sale price for online orders only, in-store price may vary. Order online and choose... Read more
Apple Watch Ultra 2 now available at Apple fo...
Apple has, for the first time, begun offering Certified Refurbished Apple Watch Ultra 2 models in their online store for $679, or $120 off MSRP. Each Watch includes Apple’s standard one-year warranty... Read more
AT&T has the iPhone 14 on sale for only $...
AT&T has the 128GB Apple iPhone 14 available for only $5.99 per month for new and existing customers when you activate unlimited service and use AT&T’s 36 month installment plan. The fine... Read more
Amazon is offering a $100 discount on every M...
Amazon is offering a $100 instant discount on each configuration of Apple’s new 13″ M3 MacBook Air, in Midnight, this weekend. These are the lowest prices currently available for new 13″ M3 MacBook... Read more
You can save $300-$480 on a 14-inch M3 Pro/Ma...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
24-inch M1 iMacs available at Apple starting...
Apple has clearance M1 iMacs available in their Certified Refurbished store starting at $1049 and ranging up to $300 off original MSRP. Each iMac is in like-new condition and comes with Apple’s... Read more
Walmart continues to offer $699 13-inch M1 Ma...
Walmart continues to offer new Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBook for sale by... Read more
B&H has 13-inch M2 MacBook Airs with 16GB...
B&H Photo has 13″ MacBook Airs with M2 CPUs, 16GB of memory, and 256GB of storage in stock and on sale for $1099, $100 off Apple’s MSRP for this configuration. Free 1-2 day delivery is available... Read more
14-inch M3 MacBook Pro with 16GB of RAM avail...
Apple has the 14″ M3 MacBook Pro with 16GB of RAM and 1TB of storage, Certified Refurbished, available for $300 off MSRP. Each MacBook Pro features a new outer case, shipping is free, and an Apple 1-... Read more

Jobs Board

*Apple* Systems Administrator - JAMF - Activ...
…**Public Trust/Other Required:** None **Job Family:** Systems Administration **Skills:** Apple Platforms,Computer Servers,Jamf Pro **Experience:** 3 + years of Read more
IT Systems Engineer ( *Apple* Platforms) - S...
IT Systems Engineer ( Apple Platforms) at SpaceX Hawthorne, CA SpaceX was founded under the belief that a future where humanity is out exploring the stars is Read more
Nurse Anesthetist - *Apple* Hill Surgery Ce...
Nurse Anesthetist - Apple Hill Surgery Center Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Now Read more
Housekeeper, *Apple* Valley Village - Cassi...
Apple Valley Village Health Care Center, a senior care campus, is hiring a Part-Time Housekeeper to join our team! We will train you for this position! In this role, Read more
Sublease Associate Optometrist- *Apple* Val...
Sublease Associate Optometrist- Apple Valley, CA- Target Optical Date: Apr 20, 2024 Brand: Target Optical Location: Apple Valley, CA, US, 92307 **Requisition Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.