Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Updating all fields (including header and footer) in a document using AppleScript

591 views
Skip to first unread message

pej...@officeformac.com

unread,
Mar 13, 2008, 11:00:46 AM3/13/08
to
Version: 2004
Operating System: Mac OS X 10.5 (Leopard)
Processor: intel

Hi!

I am writing document template script with AppleScript hoping it eventually will work with both Word 2004 and 2008. I am using property fields filling them with data from variables from the script. Then I am updating the property fields with the command

"update field every field of active document"

This updates all the fields in the main document, but not the fields in the header and footer. I have tried several different solutions and none has worked so far. I would really appreciate some help on how to update fields in the header and footer as well.

John McGhie

unread,
Mar 14, 2008, 6:43:45 AM3/14/08
to
Here you go:

Convert this to AppleScript....

Public Sub FieldsUpdateAll()
Dim FieldID As Long
Dim aStory As Range

For Each aStory In ActiveDocument.StoryRanges
FieldID = aStory.Fields.Update
If FieldID <> 0 Then
aStory.Fields(FieldID).Select

MsgBox "Field " & aStory.Fields(FieldID).Code.Text & _
" has an error", vbExclamation
End

End If
Next ' aStory

End Sub


On 14/3/08 12:30 AM, in article ee93b...@webcrossing.caR9absDaxw,
"pej...@officeformac.com" <pej...@officeformac.com> wrote:

--
Don't wait for your answer, click here: http://www.word.mvps.org/

Please reply in the group. Please do NOT email me unless I ask you to.

John McGhie, Consultant Technical Writer
McGhie Information Engineering Pty Ltd
http://jgmcghie.fastmail.com.au/
Sydney, Australia. S33°53'34.20 E151°14'54.50
+61 4 1209 1410, mailto:jo...@mcghie.name

Shawn Larson [MSFT]

unread,
Mar 14, 2008, 1:20:33 PM3/14/08
to
Word uses a ‘story’ concept.  Different parts of a document are considered a ‘story’ and need to be referenced individually.  For example there is a main document story, a header/footer story, a footnote story and so on.  To update the fields within a header, the header needs to be referenced.  To add another wrinkle, the header ‘object’ within AppleScript does not contain fields, but it does contain a ‘text object’.  The text object does contain fields, so we need to reference that text object so we can access the fields.  

So here is a small AppleScript example to update all the fields in the main header and in the current document:

tell application "Microsoft Word"
    -- Get a reference to the main header in the current document
   set refHeader to get header of section 1 of active document index header footer primary
   -- Update every field in the header
   update field (every field of text object of refHeader)
    -- Update every field in the document
   update field every field of
active document
end
tell

HTH,
Shawn Larson
Mac Word Test
Microsoft MacBU
--
This posting is provided “AS IS” with no warranties, and confers no rights.

John McGhie

unread,
Mar 15, 2008, 5:02:34 AM3/15/08
to
Hi Shawn:

Are you saying that in AppleScript, you do not have to iterate the headers
and footers?

In VBA, there will potentially be six header and footer "stories" per
section, and potentially 255 Sections per document :-)

I know nothing about AppleScript, so I don't understand how your script
handles that?

Cheers


On 15/3/08 2:50 AM, in article C40001F1.2AAF%sha...@NOSPAM.microsoft.com,
"Shawn Larson [MSFT]" <sha...@NOSPAM.microsoft.com> wrote:

> Word uses a Śstoryą concept. Different parts of a document are considered a
> Śstoryą and need to be referenced individually. For example there is a main


> document story, a header/footer story, a footnote story and so on. To
> update the fields within a header, the header needs to be referenced. To

> add another wrinkle, the header Śobjectą within AppleScript does not contain
> fields, but it does contain a Śtext objectą. The text object does contain


> fields, so we need to reference that text object so we can access the
> fields.
>
> So here is a small AppleScript example to update all the fields in the main
> header and in the current document:
>
> tell application "Microsoft Word"
> -- Get a reference to the main header in the current document
> set refHeader to get header of section 1 of active document index header
> footer primary
> -- Update every field in the header
> update field (every field of text object of refHeader)
> -- Update every field in the document
> update field every field of active document
> end tell
>
> HTH,
> Shawn Larson
> Mac Word Test
> Microsoft MacBU

--

Shawn Larson [MSFT]

unread,
Mar 17, 2008, 1:42:37 PM3/17/08
to
Absolutely not, you still need to iterate the headers and footer with AppleScript as well.  My point was to provide a small, straightforward AppleScript sample to update the fields in the main header of the document.  Even though I did specify the code sample would update fields in the main document header, I should have been more direct, stating it would only update fields in the section 1 main header.  

I could have also made it clear that my sample was not an AppleScript conversion of the VBA code you provided.  


HTH,
Shawn Larson
Mac Word Test
Microsoft MacBU


On 3/15/08 2:02 AM, in article C401C6C2.C3E5%jo...@mcghie.name, "John McGhie" <jo...@mcghie.name> wrote:

> Hi Shawn:
>
> Are you saying that in AppleScript, you do not have to iterate the headers
> and footers?
>
> In VBA, there will potentially be six header and footer "stories" per
> section, and potentially 255 Sections per document :-)
>
> I know nothing about AppleScript, so I don't understand how your script
> handles that?
>
> Cheers
>
>
> On 15/3/08 2:50 AM, in article C40001F1.2AAF%sha...@NOSPAM.microsoft.com,
> "Shawn Larson [MSFT]" <sha...@NOSPAM.microsoft.com> wrote:
>
>> Word uses a ‘story’ concept.  Different parts of a document are considered a
>> ‘story’ and need to be referenced individually.  For example there is a main

>> document story, a header/footer story, a footnote story and so on.  To
>> update the fields within a header, the header needs to be referenced.  To
>> add another wrinkle, the header ‘object’ within AppleScript does not contain
>> fields, but it does contain a ‘text object’.  The text object does contain

>> fields, so we need to reference that text object so we can access the
>> fields.  
>>
>> So here is a small AppleScript example to update all the fields in the main
>> header and in the current document:
>>
>> tell application "Microsoft Word"
>>     -- Get a reference to the main header in the current document
>>     set refHeader to get header of section 1 of active document index header
>> footer primary
>>     -- Update every field in the header
>>     update field (every field of text object of refHeader)
>>     -- Update every field in the document
>>     update field every field of active document
>> end tell
>>
>> HTH,
>> Shawn Larson
>> Mac Word Test
>> Microsoft MacBU

--

pej...@officeformac.com

unread,
Mar 20, 2008, 9:09:23 AM3/20/08
to
Thank you Shawn!

That example really solved my problem. Since my headers and footers were first page ones I only changed the reference to the first page headers and footers instead and it works just great! Now all that remains for me is to convert this macro from "vanilla" AppleScript into AppleScript studio. We will see how that works. But there is no way to embed the scripts in to Word like you do with the macros? They have to be separate applications that run outside of Word and tells Word what to do?

All the best

Peter Johansson

Shawn Larson [MSFT]

unread,
Mar 20, 2008, 4:17:46 PM3/20/08
to
Peter,

Good, glad to hear that helped and more importantly that you were able to customize the code to your needs.  

You cannot embed AppleScript code into a Word document or assign it to a menu item or toolbar button.  In fact, I don’t think any Macintosh application allows you to embed AppleScript code in its own file format.  What you can do is save the AppleScript script as a compiled script in the ‘~/Documents/Microsoft User Data/Word Script Menu Items’ folder.  After that, the script will be listed off of the Script menu within Word.  


Shawn Larson
Mac Word Test
Microsoft MacBU


pej...@officeformac.com

unread,
Mar 31, 2008, 10:56:23 AM3/31/08
to
Hi again Shawn!

Another question (perhaps I should have started a new thread). Anyway, I have read the AppleScript reference guide, but I still don't get how I get Word to open a new document based on a template. As of now the template is located inside my XCode package. As I understand I must install it on in the template folder before I can open it or? Then there was something about accessing it with index numbers?

Either I write the script so it installs this file so word can open a new document based on it or I just want to open a new file based on this document template. I would really appreciate a bit of sample code on how to open a new document based on a template file.

Best wishes

Peter Johansson

Daiya Mitchell

unread,
Mar 31, 2008, 12:52:45 PM3/31/08
to

Shawn Larson [MSFT]

unread,
Mar 31, 2008, 2:14:06 PM3/31/08
to
Peter,

Yea, it would probably be best to create a new post for a new question.  But, we can take care of this question with this post.  

I don’t think you’ll need to place your template in the Template folder.  I think the key test will be can you open your template in your XCode package via Word’s File, Open?  I really think as long as the template location is along a valid path, the following AppleScript code should work for you:

   tell application “Microsoft Word”
      set stTemplPath to "HD:Microsoft Office 2008:Office:Media:Templates:Brochures:Advantage Brochure.dotx"
      create new document attached template stTemplPath
   end tell

In the above example, I’m using the ‘create new document’ command with the ‘attached template’ property.  


Shawn Larson
Mac Word Test
Microsoft MacBU


pej...@officeformac.com

unread,
Mar 31, 2008, 4:45:53 PM3/31/08
to
Hi again!

Thanks for the quick reply. I did not manage to make the "create new document" command to work. It does not compile and stops at "new" saying "Expected end of line, etc. but found identifier". What did I do wrong?

Best wishes

Peter Johansson

Shawn Larson [MSFT]

unread,
Apr 1, 2008, 1:15:41 AM4/1/08
to
Because I didn’t review this thread - I forgot you are working with Mac Word 2004.  I’ll need to review how to accomplish this with the Word 2004 dictionary.  Hopefully I’ll be able to track that down tomorrow.  


Shawn Larson
Mac Word Test
Microsoft MacBU
--
This posting is provided "AS IS" with no warranties, and confers no rights.  

Find out everything about Microsoft Mac Newsgroups at:
[http://www.microsoft.com/mac/community/community.aspx?pid=newsgroups]
Check out product updates and news & info at: [http://www.microsoft.com//mac]

pej...@officeformac.com

unread,
Apr 1, 2008, 8:18:01 AM4/1/08
to
Hi again!

Yes, I am using Word 2004. That is also why I previously was asking on how to access the script from within Word, since the 2004 version does not have an AppleScript menu. I want this script to be compatible with both 2004 and 2008 versions of Word since it is a document template macro that will "replace" the VBA one that is available for Windows.

Best wishes

Peter Johansson

Shawn Larson [MSFT]

unread,
Apr 4, 2008, 2:13:13 PM4/4/08
to
Peter,

I think I’d like to frame this with ‘Good News’ and ‘Bad News’. :)

Good News:
The AppleScript process of creating a new document based off of an existing template – as one does via the Project Gallery – is vastly improved in Mac Word 2008.  

Bad News:
To replicate with AppleScript the results from creating a new document via the Project Gallery does require the steps / sample code that Diaya pointed out in the following link:
http://www.microsoft.com/mac/developers/default.mspx?MODE=ct&CTT=PageView&clr=99-21-0&target=11de1a06-e83c-478b-ab63-0fb48efa1c841033&srcid=a96edcd4-ee10-49e2-b836-ce785291c6ea1033&ep=8&rtype=2&pos=2&quid=f816c52e-66fd-490f-8a77-f1bc3b3e8859

For Mac Word 2004, the basic steps are:
  • Create new document
  • Attach your template to the new document.  This will give the document access to any Styles, Style definitions, AutoText entries, etc. that are specific to that template.  
  • Use the Insert, File command to insert any text, fields, tables, pictures, etc. that are included in the template.  Below is small example.  The link I gave above is very thorough and should be helpful for you.  

    tell application “Microsoft Word”
        activate
        set stTemplPath to <<Path to template, including the template name>>
        set newDoc to make new document
        set attached template of newDoc to stAttTemplPath
        insert file at text object of newDoc file name stAttTemplPath
    end tell

Not fun, not the answer you wanted, nor the answer I wanted to give you.  

Feel free to post any new questions in new posts.  

HTH,

Shawn Larson
Mac Word Test
Microsoft MacBU


0 new messages