Home > database >  VBA in Publisher - Execute Macro on Keystroke
VBA in Publisher - Execute Macro on Keystroke

Time:07-04

I'm working within Microsoft Publisher.

I have a macro in module1 (that works) called "Export_as_Image".

I want to run this macro whenever I press F8.

I have created module2 with this code:

Sub SetKey()
Application.OnKey "{F8}", "Export_as_Image"
End Sub

When I run module2 I recieve the error "Compile error: Method or data member not found".

When I press F8 within Publisher, nothing happens.

Can anyone suggest what I may have done wrong, or a code to help me achieve what I'm looking for?

Thanks!

CodePudding user response:

After some experimenting I found that it's needed to run SetKey for the first time firstly. You may call it from an event-driven sub (Workbook_Open, for example). Looks like running it for the first time, registers the key shortcut to the specified code.

Also, add the full path to the code you need to run: "Module2.Export_as_Image".

Update (reply to Charlie's comment below)

You can even move SetKey's code to Workbook_Open and delete SetKey (see image below).

Run on open

  • Related