Activate assigned document

A

Alejandro

I have a code that is creating and filling a new document with a template as
base. Herefore the user is requested to enter some specific data in an
userform, activated through a custom toolbarbutton. After code running I
would like to set the focus to this new document but somehow it doesn't
work.

I tried:
Documents(1).Activate
Windows(ActiveDocument.Name).Activate
Application.ScreenRefresh

and other combinations. I see that while code is running the new window is
activated but after the code is ended the focus is returned to the document
in which the code was started.

Any suggestions would be most welcome.

Alexander
 
J

Jonathan West

Hi Alejandro


Alejandro said:
I have a code that is creating and filling a new document with a template as
base. Herefore the user is requested to enter some specific data in an
userform, activated through a custom toolbarbutton. After code running I
would like to set the focus to this new document but somehow it doesn't
work.

I tried:
Documents(1).Activate
Windows(ActiveDocument.Name).Activate
Application.ScreenRefresh

and other combinations. I see that while code is running the new window is
activated but after the code is ended the focus is returned to the document
in which the code was started.

Any suggestions would be most welcome.

The trick is to keep tabs on the document you want to deal with when it is
created. Do, if a new document is created using the Documents.Add method,
you would proceed something like this.

In the declarations section of your userform (above the first Sub
statement), declare a module-level object variable like this

Private oDoc As Document

When you create the document, assign it to the object variable like this

Set oDoc = Documents.Add(Template:="My template.dot")

(Use whatever template you are basing the document on, in place of "My
template.dot")

You can put information into the document from the UserForm and be sure that
it is going to the right document even if it is not currently activated, by
assigning text to ranges in oDoc where you would normally assign them to
ranges in ActiveDocument.

At the end, when you want the document displayed, you can use the following

oDoc.Activate
 
A

Alejandro

Your suggestion seems not to work either. Also setting focus to another
document does not work is this code. I find this very odd because I have
another project in which this does work.

During code running I have set Application.Screenupdating to False but
before I enter the code to activate the right window I reset it to True.
First I thought that maybe this could influence the code but after removing
these lines I still cannot reset the focus. Any ideas?

Alexander
 
J

Jonathan West

What are you doing with the UserFirm when the user has finished enetering
the data into it?
 
A

Alejandro

Your question made me run some tests and this is what I found out. The
userform was open because I needed data from the form to insert in to the
document. I now added an instruction to hide the userform, then perform the
adding and custimization of the new document. After code running the
userform is unloaded. This works: I can set my focus to the new added
document.

Thanks for your assistance.
 

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