Up until MS Word 2016, it was possible to apply a font style (bold, italic, subscript etc.) by keystroke in the Find and Replace dialogue box. In Word 2019, that feature was removed, forcing the user to click through several tabs and selection boxes (and even menus) to apply the required style. Has anyone found a way to use KM to intercept a keystroke (e.g. ⌘I for italic or ⌘B for bold) and toggle the relevant setting on/off? The macro would need to work only when the F&R dialogue box is open, and to be able to call and apply MS Word's VBA codes to the text argument field.
For those of us who don't have MS Word, we might still want to help, but we would probably need to see screenshots or a video of the problem. If you show a video which allows us to see your mouse and keyboard clicks as you are currently solving the problem manually, we might be able to help you automate it.
Or you can wait for someone who has Word and who can replicate your situation based on your current description.
Thanks for your reply.
Here is the Find and Replace dialogue box:
In older versions of MS Word, I could just press ⌘B and "Font: Bold" would be added to the field. Now I have to click on Format to open the drop-down list:
and click on "Font…" to open up a new dialogue box:
Then click on the "Font style:" drop-down menu (specifically on the ⌄ symbol, because the blank bar is a text box) to reveal:
and then select "Italic" or "Bold" (or "Not Italic" or "Not Bold"), and click OK to get back to the Find and Replace box.
This is tedious and prone to slips of the finger.
One possibility is that KM can read the state of the font style from the window and then issue changes.
An alternative is that this is doable in VBA, but I have no clue how.
I think you'll have to use the UI. So something like:
Word Find Set Bold Formatting.kmmacros (4.0 KB)
...should get you started. Use the "Button" menu in the "Press Button" action to see a list of available buttons in the dialog. And change the penultimate action to "Italic" for italics, etc.
I don't know if you can set dialog properties on the fly with VB, but you should be able to spawn the dialog with them already set (eg Dialog.Show method (Word) | Microsoft Learn). But you'd have to create a VB macro for each setter (or each combination if you can't alter, only spawn) or do something clever with VB variables and parameter passing from an AppleScript since AS can only execute VB macros, not raw VB code.
Well, any clicks that you do manually can be done programatically by KM. I don't see any problem here using KM's mouse and image scripting to click on the same buttons an options that you click on manually.
That's also possible. You can use Find Image or OCR actions in KM to "read" the words in a dialog box. But I'm not sure why you want KM to read from the window since the human can also read and make the decision about which macro to trigger based on what he sees.
That looks more workable than the script I had come up with so far. And typing the name of the font style in the box (I never understood why it had to be a text box) is a neat trick.
However, this doesn't allow me to use the one keystroke combination to toggle any style on/off. I would need to have different keystroke combinations for on and off. Workable but not as elegant as I would hope.
I will keep working and report back.
No you wouldn't need different keystrokes for on and off. Your macro could actually read the words in the dialog box using the KM OCR action. Then your macro could generate different keystrokes depending on whether it sees "Font: Bold" or not.
I don't have MS Word so I can't see exactly what the windows look like in the different situations. So I don't think I can help you write a solution. I think you just need to find a way to write a macro that can read the conditions on the screen.
How would you do that in the UI, without mouse-clicks? Replicate that in KM...
One way would be to open the font format dialog, hit the Tab key once to activate the "Font style:" field, then type in "Bold" to turn it on or hit Delete to clear the field.
How do you know if the field is set, and what to? With the field active from above, "Select All". If the field is set then the "Copy" menu item will be available -- copy the text and evaluate the contents of System Clipboard.
The other way to check the field is with AppleScript -- System Events can get the value
, and it saves messing with the clipboard. Since the Clipboard method is simple keystroke actions that you can put together yourself, here's a demo of the AppleScript version:
Word Find Set Bold Formatting (Toggle Version).kmmacros (6.5 KB)
Thank you. I discovered two keystroke combinations that function in the Find and Replace dialogue box and get me two steps closer to your suggestion. I had envisaged your solution before you stepped it out, and now you have explained how to do what I had envisaged.
I will report back.
Brilliant! This gets me over the hump. Now I can expand this macro to account for all combinations of Bold and Italic. I'll post the finished macro when it's confirmed.
With thanks to everyone's help, I have worked up a Bold macro and an Italic macro (attached here). The full screenshot is too big to paste here.
An Undelrine macro will take some lateral thinking.
Toggle Bold.kmmacros (19.7 KB)
Toggle Italic.kmmacros (19.7 KB)
Especially since there are many kinds of underline!
To get you started here's an AppleScript that, if the "Find Font" dialog is open (as done in the previous macro), toggles between "Underline: (none)" and "Underline: Words only". This time it uses System Events to not only get the current value but also to simulate pressing either the down or up arrow (key code 125
and 126
) then the Return key to select the correct menu item.
tell application "System Events"
tell process "Microsoft Word"
tell window 1
tell tab group 1
tell pop up button "Underline style:"
click
if value is "(none)" then
key code 125
else
key code 126
end if
keystroke return
end tell
end tell
end tell
end tell
end tell
Thank you for the starter. However, it won't finish. What am I missing?
Sorry, I should have been clearer. Also, I didn't realise at the time about differences in underline types. So here's an adjusted version that'll turn on "Single" underline then toggle between that an "No underline" (it appears you cannot turn off "Underline" short of clearing the format choice entirely):
Word Find Underline Formatting (AS Toggle Version).kmmacros (4.8 KB)
Given the messing around required to find the proper System Events elements to use in the AppleScript you may find it easier to use KM's built-in image detection instead. Here's an example for underline toggling as above:
Word Find Underline Formatting (Image Toggle Version).kmmacros (196.3 KB)
(Image detection "in the frontmost window" wasn't working for me for menu items, which is why I'm getting the front window's frame and using that to set the detection area in the later actions.)
And I'm in business! Thanks for all your help. I embedded your code into my macro container (works only when Find and Replace
box is frontmost) and OK
button to finish. It switches between Underline
and No Underline
without complaint.
I'm very grateful for your perseverance.