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.
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.
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
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!
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!