AppleScript find and highlight in MS Word

Hi everyone, I'm new to scripting and struggling to below script:

set docPath to "/path/to/file"

tell application "Microsoft Word"
	set doc to open docPath
	set doc to the active document
	set myfind to find object of text object of doc
	set properties of myfind to {match case:false, match whole word:false, match wildcards:true}
	set findResult to execute find myfind find text "\\[*\\]" replace with myfind replace replace all with highlight
	#close active document saving yes
	
end tell


I have tested the above script and it do find the word I wanted it to find (if replace with "", all text in a square bracket will be deleted, that's the way i tested it)


But the need is to find the text in square bracket, and HIGHLIGHT them for further edits. I've read the dictionary but struggling to understand how it could be achieved.


Also like to know how to return the find items, instead of boolean? the current result returns true or false indicating if there is any finding, but if it can point out which exactly find, maybe can try highlight it seperately?


Many thanks in advance!


Posted on Aug 21, 2023 12:07 AM

Reply

Similar questions

4 replies

Aug 24, 2023 6:25 PM in response to Sebastian0619

Could you provide a more concise description of what you are trying to accomplish with your Applescript? What is your ultimate goal? It's not entirely clear just from your example code.


The latest 365 versions of Office for Mac now include almost complete VisualBasic support. In Word, go to to the Settings -> Ribbon & Toolbar and on the right column scroll down and check the Developer checkbox. You can now use VisualBasic to script Word or perhaps record a macro instead. While AppleScript can do some things, using Word's built-in scripting may be a better choice. There's also much more online documentation for VBA Office scripting than AppleScript to automate things in Microsoft Word.

Aug 24, 2023 7:32 PM in response to James Brickley

Hi James,


Thanks for your help.


My job is highly associated with documents, and when drafting documents there may be a lot of information TBD so that they are covered with square bracket like "[Mr/Ms] Wayne"

And for most of time a project may have multiple documents, with multiple outstanding info. While clearing out, I may sometime too confident not to global search in each of them for a square bracket, which caused me some consequances.


With above being said. The ultimate goal is that, I can use the Shortcut pass the applescript (as I'd like it to be processed without open the MS Word app) to search for square bracket and other mark indicating the TBD info, highlight those info and rename with affix of "[TBD]" and the file name may looks like "[TBD] filename.docx". Once the script in place, I can simply run the script to find out which documents is not clean, and make no further mistake.

Aug 24, 2023 10:05 PM in response to Sebastian0619

Food for thought, there may be many solutions but it sounds like potentially rethinking your personal workflow versus forcing a script to fit your workflow would simplify things. For example, what about creating a folder structure where you move completed documents to a final destination indicating they've been scrubbed of TBD entries? Also having a pending folder.


If you do script something, it might be better to create an Office VBA script within the Word normal.dot template that always loads when Word is running. The script could trigger a scan for the TBD entries in the current document when attempting to exit Word or close the document. If it finds any TBD entries it can pop-up and alert you and offer the option of searching for the TBD entries and jumping to each TBD so you can change them. Then when you close the document it runs another scan and if no more TBD entries it saves and closes and could even default to the final destination folder you've defined. If you chose not to correct the TBD entries or stop midway and choose not to continue, it saves the document to a pending review folder of your choice.


I still think AppleScript is not the best way to approach this problem, if all the documents are Microsoft Office files.


Sometimes we go too far with automation. It's a tricky thing to get right.


--- The Matrix ---

Spoon boy: Do not try and bend the spoon. That’s impossible. Instead… only try to realize the truth.

Neo: What truth?

Spoon boy: There is no spoon.

Neo: There is no spoon?

Spoon boy: Then you’ll see, that it is not the spoon that bends, it is only yourself.



This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

AppleScript find and highlight in MS Word

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