You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Find and Replace Header in Word using applescript

I have been successfully using the following to edit content within word document, but it does not edit the content of headers and footers.


tell application "Finder" to set myFiles to every file of (choose folder)

repeat with aFile in myFiles

tell application "Microsoft Word"

set file_name to aFile as string

open file file_name

activate

set docRange to text object of active document

clear formatting of find object of docRange

execute find (find object of docRange) find text "String 1" replace with "String 2" replace replace all

save active document

close active document

end tell

delay 1

end repeat


There's a dead link in an old thread for a similar issue, so I was hoping that someone here could help.

Thanks in advance!


Posted on Sep 11, 2019 3:27 PM

Reply
Question marked as Top-ranking reply

Posted on Sep 16, 2019 1:31 PM

This is why I don't script Word :)


I could get an image in the header, and I could align the paragraph, but I couldn't manage the text separate from the image.

The image alignment options are applied to shapes, but shapes don't have associated file attributes, despite the fact the dictionary says that picture inherits from shape, and therefore should respond accordingly.


Alas, I think this means my lucky streak comes to an end. Maybe someone else sees this thread and can shine some light.

Similar questions

8 replies
Question marked as Top-ranking reply

Sep 16, 2019 1:31 PM in response to loonerk

This is why I don't script Word :)


I could get an image in the header, and I could align the paragraph, but I couldn't manage the text separate from the image.

The image alignment options are applied to shapes, but shapes don't have associated file attributes, despite the fact the dictionary says that picture inherits from shape, and therefore should respond accordingly.


Alas, I think this means my lucky streak comes to an end. Maybe someone else sees this thread and can shine some light.

Sep 11, 2019 4:13 PM in response to loonerk

I'm sure gods have tried AppleScripting Word and given up in despair.


A quick look at the dictionary hints that your problem lies around the fact that you're only checking the document body, as evidenced by the line:


set docRange to text object of active document


To check the headers you would need to execute the find against the header object. The dictionary hints that this is retrieved via the get header function:


get header

get header (verb)Returns a specific header object (from Text Suite)

FUNCTION SYNTAX

set theResult to get header section ¬

    index WdHeaderFooterIndex

RESULT

header footerPARAMETERS

ParameterRequiredTypeDescription

direct parameter. required section

index required WdHeaderFooterIndex Specifies which header to retrieve.


where the resulting header footer object contains a relevant text object.


Therefore, as a guess (I'm not brave enough to try), you're probably looking at something like:


tell application "Microsoft Word"

-- ...

set headerRef to text object of (get header of section 1 of active document index header footer primary)

execute find (find object of headerRef) find text "String 1" replace with "String 2" replace replace all

end tell


<barf> Excuse me while I go shower lol

Sep 11, 2019 11:00 PM in response to loonerk

I might be a magician, but I'm no Houdini.


I was able to get a script to insert an image, but trying to do it via the find command was more than my brain could handle.


If it's sufficient to insert the image into the header without the find/replace, I got this to work:


set headerRef to text object of (get header of section 1 of active document index header footer primary)

set img to (choose file) as text

make new inline picture at headerRef with properties {file name:img}


hope that helps

Sep 12, 2019 11:58 AM in response to loonerk

> I was hoping you could send me the dictionary to which you referred in your first message


The dictionary is visible in Script Editor (File -> Open Dictionary -> <select the app> )


However, my distain for Word is that its dictionary is almost incomprehensible and does not follow normal AppleScript standards (complicated in part due to Word's extensive feature set, as well as its VB origins).


In any case, what you're trying to do shouldn't be too hard - once you get the image in the document it shouldn't be too hard to manipulate.


Have a poke at the dictionary, and let me know when (not if! lol!) you have questions. :)

Sep 12, 2019 12:45 PM in response to Camelot

This is when....


tell application "Finder" to set myFiles to every file of (choose folder)

repeat with aFile in myFiles

tell application "Microsoft Word"

set file_name to aFile as string

open file file_name

activate

set headerRef to text object of (get header of section 1 of active document index header footer primary)

set img to (choose file) as text

make new inline picture at headerRef with properties {file name:img, alignmnet:align vertical top}

save active document

end tell

delay 1

end repeat


The align property is all I found, but it doesn't seem to work within the header. In word itself, you need to go to the picture format -> advanced layout and specify that horizontal Alignment is left relative to page and that vertical alignment is Top relative to Page.


This might be too complicated to represent in an apple script, but you're 2 for 2 as of yet, so I figured I'd ask.

Find and Replace Header in Word using applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.