Thursday, October 6, 2022

#1 2022-06-03 08:53:54 am

MaryQue
Member
Registered: 2020-01-08
Posts: 8

Microsoft Word - Find text with a user-defined character style applied

Hi, all,

I'm struggling with a script to poll a long document looking for and capturing any text that has a specific user-defined style applied to it. It's easily done in the UI, but I'm really having trouble with how to do it via script.

I've got a kludge more or less working in which the character style is set to a language that's not going to be used elsewhere. I can set the language ID of the find object to that language and get what I need, but it's definitely a workaround.

Is there a more direct way to target text using a specific character style by name? Not a newcomer to AppleScript, but most of my experience is with InDesign scripting and I find the Microsoft products more than a little baffling to script.

Thanks for any guidance you can give me.

Mary

Offline

 

#2 2022-06-03 01:41:49 pm

Mockman
Member
From:: Toronto
Registered: 2020-05-27
Posts: 243

Re: Microsoft Word - Find text with a user-defined character style applied

This is from the Word 2004 applescript reference (p21). As is, it won't do everything you ask but it demonstrates how to find text with a specified style. To see it work, set some text to the 'heading 1' style, select all, and run the script. Each time you run the script, it will grab the next instance of the styled text. I imagine that it could be modified to grind through the document and automatically create a list of every instance. Hope it at least points you in the right direction. I'm using Word 2011 so it's possible that things have changed with newer versions.

Applescript:

tell application "Microsoft Word"
   
   set selFind to find object of selection
   clear formatting selFind
   set style of selFind to style heading1
   
   execute find selFind find text "" wrap find find stop with find format and match forward

   if found of selFind is true then
       display dialog (get content of text object of selection)
   end if
end tell

Offline

 

#3 2022-06-04 06:38:22 am

MaryQue
Member
Registered: 2020-01-08
Posts: 8

Re: Microsoft Word - Find text with a user-defined character style applied

Mockman, thank you so much!

What you found in the 2004 guide needed a little tweak to work with user-defined styles:

Applescript:

   set style of selFind to Word style "My Style Name" of active document
   

.. but it does work. I did already have the part working to cycle through the document and capture the found text and return it to a list, tested using other attributes, but this was the missing piece that will let me work directly with the applied custom style.

I guess a full read of the 2004 guide  is in order. I tried searching it for info before posting here but I guess I didn't hit on the right search terms to find what you found. It wasn't the "beach read" I had in mind for this summer, but it may make any future Office/Word scripting less painful.

Again, thanks for your help, it's much appreciated!

Mary

Offline

 

#4 2022-06-04 08:30:44 am

Mockman
Member
From:: Toronto
Registered: 2020-05-27
Posts: 243

Re: Microsoft Word - Find text with a user-defined character style applied

My pleasure. I can't support your choice for beach reading but as long as you leave the excel 2004 reference at home I can look the other way. Oddly, all I searched for was 'style' but I confess that at some point in the past I had read the doc (almost in its entirety) so it made some sense to me. Forgot about the 'user' part of your request as I have given up on using styles in word (which haven't changed for the better since v5) but glad it helped.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)