0

Core question/in a nutshell:

How can I assign the following VBA macro to a hotkey/key combo in Microsoft Word for MacOS? It's driving me around the bend.

System info:

  • Hardware: M1 Macbook Pro
  • OS: MacOS Sonoma 14.2.1
  • Microsoft Word: Version 16.81 for Mac

Problem at hand:

I'm trying to set up VBA macros to do the following:

a) turn selected text green b) turn selected text red and strike it through

While written VBA code appears to work when manually selecting 'run', I'm struggling to get it to set to hotkeys. I want to be able to press a key combo (i.e. ctrl + G, or ctrl + R) and have said macros run. Despite my best efforts, I appear unable to do this and don't know why.

Attempts so far:

I've tried recording a macro and assigning a key combo from within the pop-up menu that opens when pressing 'record macro'. I've then taken the steps and saved. Sadly, changing font colour doesn't appear to be recorded when using 'record macro', so I've had to alter the VBA code within the macro to the following:

Sub ChangeToGreen()
'
' ChangeToGreen Macro
'
    With Selection.Font
        .Color = RGB(0, 255, 0) ' This sets the color to green
    End With
End Sub

I've tried finding menus within Microsoft Word on MacOS to manually assign this to a hotkey/key combo after saving, but haven't been able to find one. Please help!

1
  • Also, consider instead of doing this as direct formatting using vba, use character styles instead. Styles can also be assigned to keyboard shortcuts. See my page: addbalance.com/usersguide/stylesImportance.htm Commented Jun 26, 2024 at 15:22

2 Answers 2

0

Okay, I figured it out.

For anyone who has the same problem - go into the 'tools' menu, then 'customise keyboard'. From there, you'll be able to select whichever macro as desired and assign your hotkey of choice.

Sign up to request clarification or add additional context in comments.

Comments

0

It sounds as if you are in the right place, but it may help to check that you are then doing the right thing.

There are two options you can use: the one you already found in the dialog box when you start the Macro Recorder, and the Mac Word Tools->Customize Keyboard... option in the Mac Menu Bar, which is similar. NB, you can't get there from the VB Editor menu, only the main Word program.

This is different from Windows Word, where the option is available via the Ribbon/QAT organizer dialog box.

If you use the Tools-Customize Keyboard... menu, you need to do the following (pretty much the same as on Windows). My guess is that you are already doing all this but let's check...

  • Select Macros in the Categories: dropdown box
  • Locate and select the macro (e.g. ChangeToGreen) in the Commands: dropdown box
  • Ensure you are saving to the place you want in the SaveChanges In: option at the bottom of the dialog box
  • Click in the Press New Keyboard Shortcut: box
  • press the keyboard combination you want, e.g. control-G Here you may find that some combinations do not work on Mac. On Mac, there's also a distinction between keyboard combinations that use the Control key and those that use the Command Key. Word should tell you if the key is already being used for some other command/macro.
  • Ensure you click the Assign button
  • Click the OK button

Back in Word, you should be able to select some text and press control-G to run the macro.

I've just been through all that on an M1 Mac mini, Mac OS Sonoma. And yes, the VB Recorder hasn't been well maintained and there are lots of things, even some quite simple ones, that it doesn't record. (The Recorder doesn't record this action on the Windows version either)

The other thing you could do if none of that seems to be working is to use a macro to assign one of more keyboard combinations. It's not a bad idea to create and maintain such a macro so that you can re-create your preferred shortcuts when necessary, e.g. if Normal.dotm is corrupted. So if you do that, make sure you save a copy of your macro as a text file from the VB Editor).

e.g.

Sub AssignKBShortcuts()
With Application
  .CustomizationContext = NormalTemplate
  ' assign control-G to ChangeToGreen
  .KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, wdKeyG), _
    KeyCategory:=wdKeyCategoryMacro, Command:="ChangeToGreen"
  ' you can assign more keys here
  
  End With
End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.