How to execute macro when closing form

L

Lasse

Hi

Is it possible to execute a macro when closing a form?

I have created a modeless form where I assign some new shortcuts when opened
and I would like to clear them when closing the form.

/Lasse
 
R

Ryan Jamison

Lasse,

Not sure about the clearing the shortcuts, but check out "Close Event".

This sub will execute when you close a document. You could place your macro
inside the sub and it will execute when you close a document.

Ryan
 
J

Jay Freedman

Lasse said:
Hi

Is it possible to execute a macro when closing a form?

I have created a modeless form where I assign some new shortcuts when
opened and I would like to clear them when closing the form.

/Lasse

I'll assume that your "modeless form" is a UserForm. If that's true, then
you can put code in the UserForm_QueryClose procedure, which runs
automatically when the form is about to close (but before it actually
closes).

To create the proper procedure, go to the left-hand dropdown at the top of
the code window in the VBA editor and choose "UserForm"; then go to the
right-hand dropdown and choose "QueryClose". The full signature of the
procedure is

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

You probably won't need to use the parameters Cancel and CloseMode. You can
just insert the code that clears your shortcuts.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
L

Lasse

Hi Jay

Thank you very much for the reply. I was talking about a UserForm and your
suggestion worked perfectly.

When I open the modeless UserForm I would like to return to the actual
document, I have tried ActiveDocument.Select but it selects all the text. Any
idea?

/Lasse
 
J

Jay Freedman

Try Selection.Select instead.
Hi Jay

Thank you very much for the reply. I was talking about a UserForm and
your suggestion worked perfectly.

When I open the modeless UserForm I would like to return to the actual
document, I have tried ActiveDocument.Select but it selects all the
text. Any idea?

/Lasse
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top