![](/sites/all/themes/custom_front/images/you_are_here_red.gif)
![](/sites/default/files/beta-site.gif)
Moving from Microsoft Office VBA to AppleScript:
MacTech's Guide to Making the Transition
Introduction
|
Table of Contents
Page Prev and Page Next buttons at bottom of the page.
|
April, 2007
Page 19
3. Word
Word VBA Macros to AppleScript
Make sure you have read Chapter 2: AppleScript in Microsoft Office (General) first. It deals with many important features of VBA, including Word VBA, that need to be treated very differently in AppleScript, as well as some equivalent control and operator statements in AppleScript you need to know. All examples in that chapter, as it happens, are for Word.
This chapter consists of typical, and popular, subroutines (macros) in VBA, and their equivalent in AppleScript, with comments as necessary to explain differences in the AppleScript approach. Almost all the examples come from
<http://www.word.mvps.org/FAQs/MacrosVBA/>
(Note: If you are using Safari as your browser, the page may come up blank initially. Refresh the page a few times to see it, and you may need to do the same for other www.word.mvps.org links.) Jonathan West, Word MVP, who also wrote some of them, suggested all the examples.
Note: when testing out scripts in Script Editor, you may wish to begin every one with an activate command on its own line just inside the tell application "Microsoft Word" block, as its first line. That will bring Word to the front so you can see what is happening. It is not necessary to include activate when running saved scripts from the script menu while Word is in the front (and there may be other occasions when you are running a script from elsewhere and don't want Word to come to the front). I have not included activate in the sample scripts here, but you may wish to do so.
Before launching into scripting Word, it is a good idea to read the introductory section Using the Word Dictionary and especially the section Working with text range objects on p.18, of the Word AppleScript Reference available in PDF format free from Microsoft at <http://www.microsoft.com/mac/resources/resources.aspx?pid=asforoffice>. Although it is largely aimed at AppleScripters unfamiliar with Word's Object Model rather than vice versa, it is full of useful knowledge for all scripters of Word.
Remember that this article was written while Office 2004 is current: some problems with Word 2004's AppleScript discussed here may be fixed in Word 2008 and workarounds provided here may no longer be needed there.
Note that although the class name in AppleScript for VBA's Range Object is text range (in the Text Suite), the name of the property corresponding to the Range Property in countless classes is almost always called text object (returning an object of text range class). Similarly the font class is the "type" of the font object property in other classes. There is a very good reason why application developers avoid using the same name for a class as for a property, which is explained below in the Find and Replace Text section, and in more detail in Chapter 6: Entourage.
Also very important to understand, since you may have to work around it in your scripts, is that text ranges you define are not "dynamic": you can manipulate them at will, but if you change their start or end positions or their content, the variable you may have set to the range will now not return the altered range, and you need re-set the variable to the result returned, if it is the altered range, or get hold of it some other way.
This is an unfortunate by-product of how Range had to be implemented in AppleScript, and may be a thorn in your side when you come to convert your macros, but there is virtually always some way to work around the changes. Two sections below – Delete duplicate paragraphs using a Text Range object and Working with Bookmarks – both go into this issue in some detail, with examples.
New Document , Open Document, and Templates
Make a New Document
Just a reminder, already mentioned in Chapter 2, that the way to make a new blank document equivalent to
Application.Documents.Add
is
make new document
Normally in AppleScript, you would expect to be able to specify many, or most, of the properties of document in a with properties parameter, right at creation time, as in Chapter 2's example of making a new table at a document. (A scripter would expect that only elements, if any are needed, would have to be added afterwards.) But that is not the case with making new documents in Word: properties cannot be set at inception of the document, which needs to be created first before they can be altered.
This seems to be an "Office special" and corresponds with how the Add Method works, so should not surprise people accustomed to VBA, although it would surprise a regular AppleScripter accustomed to other applications. In the same way that in VBA the only arguments that can be specified with Documents.Add are Template, NewTemplate, DocumentType, Visible (more on templates in a moment), but not any of the 50 or so Properties of Document itself, which are specified later, so it is with AppleScript.
For example, in VBA, to set the font and a bit of content, you would do this:
Set NewDoc = Documents.Add
With NewDoc.Content
.Font.Name = "Arial"
.Text = "Here is some Text."
End With
An AppleScripter would expect to be able to do this:
tell application "Microsoft Word"
set newDoc to make new document with properties {text object:¬
< Previous Page Next Page>
![](/sites/all/themes/custom_front/img/search_text.gif)
- SPREAD THE WORD:
- Slashdot
- Digg
- Del.icio.us
- Newsvine