Activate Word object

M

mike

Am working with an executable that invokes Word and preprocesses a Target
Document, I have noticed that if I don't set the objWord (application
object) to...

With objWord
.Visible = True
.Activate
End With

until the very end, processing the document goes much faster, however it
will randomly will finish with more than one resulting documents. That is to
say occasionally there will be a resulting Document1, Document2 and
MyDocument, each will have erroneous pieces.

With performance the goal of the effort, I hate to give up the savings by
Activating early-on but the random behavior has me concerned?

Is there a best practices for performance approach?
 
S

Shauna Kelly

Hi Mike

You generally get a performance gain from running word with .Visible =
False.

If you want to run with .Visible = False and not .Activate-d, then the code
itself should be written so that it will do exactly the same thing whether
or not Word is .Visible or .Activate-d.

I would suspect two likely problems with the code.

First, ensure that the code is always referring to the correct Document. For
example, don't rely on references to Word's ActiveDocument. Create a
reference to the Target Document, and use that reference consistently.

Second, don't use the .Selection. Instead, set a reference to a range, and
use that.

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
M

mike

Thank you Shauna,

I researched the usage of ActiveDocument and it looks like the original
author did implement the reference like you mentioned.

I ended up with...

With objWord
.Visible = True
.Activate
.Visible = False
End With

Am getting the performance and consistency.

I will look at the Range and Selection implementations as you also
mentioned.

Thanks again, sincerely, - Mike
 

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