Returning the focus to the original doc before doing an action

C

Carole

My UserForm has a command button to create a summary at
the top of the active document (say DocA.doc). Because my
UF is set to ShowModal = false, I am looking for a way to
return the focus to DocA.doc when the user pushes the
cmdCreateSummary button otherwise the summary gets
recorded on whatever document is active at the time. So I
use this to identify the original document (DocA.doc):

Private Sub UserForm_Activate()
Static strActiveDoc
strActiveDoc = ActiveDocument.Path & _
Application.PathSeparator & ActiveDocument.Name
End Sub

Then when the user pushes cmdCreateSummary, my first line
of code is this:

Windows(strActiveDoc).Activate

but it does not work. What is wrong with this
logic/syntax or does anyone have a better alternative.

Thank you.

Carole
 
J

Jonathan West

Hi Carole

Carole said:
My UserForm has a command button to create a summary at
the top of the active document (say DocA.doc). Because my
UF is set to ShowModal = false, I am looking for a way to
return the focus to DocA.doc when the user pushes the
cmdCreateSummary button otherwise the summary gets
recorded on whatever document is active at the time. So I
use this to identify the original document (DocA.doc):

Private Sub UserForm_Activate()
Static strActiveDoc
strActiveDoc = ActiveDocument.Path & _
Application.PathSeparator & ActiveDocument.Name
End Sub

Then when the user pushes cmdCreateSummary, my first line
of code is this:

Windows(strActiveDoc).Activate

but it does not work. What is wrong with this
logic/syntax or does anyone have a better alternative.

You're on the right general lines, but there's a better way.

Try this instead

Static oDoc as Document
Set oDoc = ActiveDocument

Then at the end, you have this line

oDoc.Activate

Alternatively, in the code that inserts the summary, replace all mentions of
ActiveDocument with oDoc, and the code will insert into oDoc even if it is
not the active window.
 
C

Carole

Hi Jonathan,

My code is as follows:

Private Sub UserForm_Activate()
Static oDoc as Document
Set oDoc = ActiveDocument
End Sub

The above works. But the following does not. I put the
oDoc.Activate in another procedure as follows:

Private Sub cmdCreateSummary_Click()
oDoc.Activate
....
End Sub

and I get the following message: Object required (Error
424).

Thank you.
 
J

Jonathan West

Hi Carole


Carole said:
Hi Jonathan,

My code is as follows:

Private Sub UserForm_Activate()
Static oDoc as Document
Set oDoc = ActiveDocument
End Sub

The above works. But the following does not. I put the
oDoc.Activate in another procedure as follows:

Private Sub cmdCreateSummary_Click()
oDoc.Activate
...
End Sub

and I get the following message: Object required (Error
424).

Ah, that is because oDoc was declared only to work within the
UserForm_Activate routine. Delete the "Static oDoc as Document" line, and
instead put the following above the first Sub statement in the userform.

Private oDoc as Document
 
C

Carole

Hi Jonathan,

Wow, it worked. I am always amazed when something does.
I love programming.

This is the final touch to my project which started months
ago. The final product is pretty amazing. Tomorrow, I am
launching my new macro to 80 people. This is going to
improve the quality of our research database tremendously.

I would like to take this opportunity to thank you and all
the mvps who have helped me through this project
especially Doug, Jay and Cindy. Thank you Dave for one of
your articles. I could not have done it without you
guys. Merry Christmas and thanks a million!

Carole
 

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