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

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.

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

Mockman, thank you so much!

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

	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

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.