Word Macro Question

Is there a way to set a macro up for use in Word, in text I have highlighted, to change to color Blue, Bold, Italic and change font size. *Then unhighlight?

You'd either have to have your macro navigate through the formatting palette, which seems to be a real pain, or set up a style in advance. If you set up the style, just have KM open the Style dialog, then have it type the name of the style and then have it press Return. Make sure you name the style uniquely so it's always selected—start it with KMKM# or whatever.

To unhighlight, have KM send a right arrow and then a left arrow, which will leave the cursor right where it was.

-rob.

Do you mean "selected", or do you mean highlighted as in "used Word's Text Highlight Colour tool on"?

Good question—I assumed selected, but if they meant highlighted, that changes things :).

-rob.

If you want to change the formatting of the selected text in Word, and aren;t averse to using an AppleScript action, you can use a macro action like this:

Edit: Oops, forgot about the unselecting part. Updated the AS code accordingly.

1 Like

I understand that the OP has highlighted some text and not just selected it. Highlighted is a font attribute.

I have googled for a solution but not found one. Then I queried ChatGPT (with several iterations), but it couldn't find a solution either:

tell application "Microsoft Word"
    activate
    set doc to active document
    set myRange to create range doc start 0 end 0
    set foundHighlight to true

    -- Create a find object to search within the document
    repeat while foundHighlight
        set myFind to find object of myRange

        -- Clear previous search settings and set options for finding highlighted text
        clear formatting of myFind
        set highlight of myFind to true

        -- Execute the find operation
        set foundHighlight to execute find myFind match wildcards false wrap find continue

        if foundHighlight is true then
            -- Apply formatting: font size 14, bold, underline, blue color
            set font size of text object of myRange to 14
            set bold of font of text object of myRange to true
            set underline of font of text object of myRange to underline single
            set color of font of text object of myRange to blue
        end if
    end repeat
end tell

This doesn't work. :frowning:

On a side note: I must say that AppleScript for Ms Word is a lot more complicated than VBA. At least for me ... Ms Word for Windows also has a macro recorder.

This AppleScript will search for highlighted text (as in "used Word's Text Highlight Colour tool on") and reformat it to make it bold, italic, big, and blue, and then remove the highlighting.

tell application "Microsoft Word"
	activate
	
	--start at the beginning of the document
	home key selection move unit a story extend by moving
	
	--set some properties on the find object
	set findObj to find object of selection
	tell findObj
		clear formatting
		
		-- clear out all the matching properties
		set match all word forms to false
		set match byte to false
		set match case to false
		set match fuzzy to false
		set match sounds like to false
		set match wildcards to false
		
		-- change this to false if you also want to pick up
		-- runs of highlighted text that aren't complete words
		set match whole word to true
		
		-- we don't care what the text says
		set content to ""
		-- start at the top
		set forward to true
		-- we want to find text that is highlighted
		set highlight to true
		-- end at the bottom
		set wrap to find stop
	end tell
	
	--perform the find operation
	set foundHighlighting to true
	repeat while foundHighlighting
		tell findObj to set foundHighlighting to execute find
		
		if foundHighlighting then
			set foundRange to text object of selection
			tell foundRange
				set bold to true
				set italic to true
				tell font object
					set font size to 14 -- or whatever size you want
					set color index to blue
				end tell
				set highlight color index to no highlight
				-- now continue our search after this word
				collapse range direction collapse end
			end tell
		end if
	end repeat
	
end tell
1 Like

Thank you @roosterboy. Great work!

I've saved your code in a new AppleScript, titled Ms Word Replace Character Formatting.

Very useful. Thank you for the clear comments. This is a very useful template for font changing scripts.

At the risk of stating the obvious -- you could also use Word's "Advanced Find and Replace":

In which case, I think this will work:

tell application "Microsoft Word"
	set theFind to find object of text object of active document
	set theReplacement to replacement of theFind
	set theFormat to font object of theReplacement
	set properties of theFormat to {bold:true, italic:true, font size:36, color index:blue}
	set findCriteria to {content:"", highlight:1}
	set replaceCriteria to {content:"", highlight:0, font object:theFormat}
	set properties of theFind to findCriteria
	set properties of theReplacement to replaceCriteria
	execute find theFind replace replace all
end tell

Comments, @roosterboy? I'm sure you've forgotten more about scripting Word than I've ever known!

2 Likes

That worked great, @Nige_S !

And it serves as a good reminder to me that sometimes you can get so used to doing something one way, you forget there can be other, possibly better, ways to do it. I had just taken an existing script I had and tweaked it for this particular case without stopping to think of alternate methods. So thanks for that!

Yes Highlighted. Sorry pages calls it select All.

The do you mean selected? That is completely different in Word to Highlighted, which is a style.

Selected == text is chosen, ready to do something with. Click elsewhere in the document and the text will no longer be selected
Highlighted == text styled for emphasis

You are right. What I mean is the whole text is put in a state that a find ?title and replace with the local_title variable can be performed. In Pages it is command A, in Word it was done with a system event.

In your first post you wanted to change text formatting on a text selection. Now it sounds like you want to replace text across the whole document. Which is it?

A good way to prepare for a post like this -- and a good first stage of writing any macro -- is to do what you want by hand, taking notes as you go. That'll let you include needed information in your question. In this particular case, for example: Is this to affect specific text you have selected or all the text? If it is all the text, is that all the "main" text or everything including headers and footers? If it is a multi-page document, just the current page's text or all the text on all the pages. And so on.

As best I can explain the ?Title would be replaced with the actual title for the lesson. Lesson I just finished I would have written The Love of the Cross for my Title. When I run the Macro anywhere a ?Title would be "The Love of the Cross" would replace ?Title. Next I have a ?dat in my input then when --execute find find text "?sdat" replace with theDate replace replace all -- runs it would put in the date I want in this case would be 03 November 2024 ams

| Nige_S
October 31 |

  • | - |

Macmike:

You are right. What I mean is the whole text is put in a state that a find ?title and replace with the local_title variable can be performed.

In your first post you wanted to change text formatting on a text selection. Now it sounds like you want to replace text across the whole document. Which is it?

A good way to prepare for a post like this -- and a good first stage of writing any macro -- is to do what you want by hand, taking notes as you go. That'll let you include needed information in your question. In this particular case, for example: Is this to affect specific text you have selected or all the text?

Specific text — ?Title, ?dat, and ?Series

Prompt for User Input “Enter the values for the Pages document”
Where Local_Title inputted data would replace the ?Title in the template
Local_Date inputted data would replace the ?sdat
Local Series entry would replace ?Series
This would occur both in the body and the footer.

Not trying to replace all the text.

(Attachment Document2.pdf is missing)

Here is my Template the entries with ? like ?Title are what is being replaced.

T

| Nige_S
October 31 |

  • | - |

Macmike:

You are right. What I mean is the whole text is put in a state that a find ?title and replace with the local_title variable can be performed.

That is exactly what I am trying to do. Works in Word want it to work in Pages.

As I understand it, Pages' AppleScript support is pretty basic, and I don't see any way to access the Find/Replace function from there (someone better than I at AppleScript may see a way). So I think the only way you're going to do this is by replicating what you'd do by hand:

For each "?" value in your input, you need Keyboard Maestro to:

Cmd-A to select all
Cmd-F to open Find
Type in the "?" value you want to find
Press Tab
Type in the value you entered for that "?" value
Press Tab
Press Enter (to click the Replace All button)

Alternatively, those last two steps could probably be replaced with a Press Button action.

I would probably store the three values in one variable, one entry per row. You could then do a "For Each - lines in variable" as the loop, and would just have to set ?Title, ?dat, or ?Series based on which loop number you're on.

-rob.