Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a bunch of VB script in Excel that ultimately opens up a new Word document and places a bunch of information in it. Everything works well, but I am having one annoyance. The annoyance is that sometimes the Word document will open in front of everything else and sometimes it will open behind everything else.

I have tried duplicating the results and it appears to be random. Sometimes I think there is something consistent as to why it does it and then something happens so my theory doesn’t hold true. I must have generated the Word document over 100 times trying different stuff and can’t seem to figure it out.

I do have three monitors, running Office 2010, and Windows 7. I am not sure if the three monitors would have any affect as to why it isn’t consistent.

What I want is for it to be visible in front of everything when it opens.

This is the code I am using to make it visible and I tried it at the end, the beginning, and then at both the end and beginning of where it creates the document:

        wrdApp.Activate
        wrdApp.Visible = True

Anybody have any suggestions?

share|improve this question
    
I have a bunch of VB script in Excel Do you mean VBA? (vbscript and VBA are not the same thing) –  chris neilsen Nov 11 '13 at 8:19
    
Well, it is script within Microsoft Excel, so I don't think that is the same as VBA. Like if you go to Excel, right click on a sheet and select View Code. Please correct me if I am wrong in calling it script, but I thought VBA was different. –  Chris Nov 11 '13 at 8:23
1  
That is VBA. Please update your tags. Also to get help you will need to post more of your code, eg how wrdApp is created –  chris neilsen Nov 11 '13 at 8:25
    
If can't open word in a controlled way / synchronously (ie. wait for app to open, it reports when it's done/ready), try to "wait" (sleep) for app load to happen, then DoEvents, then the visibility code –  Mark Nov 11 '13 at 8:42
add comment

1 Answer 1

up vote 1 down vote accepted

What I want is for it to be visible in front of everything when it opens.

No need for APIs for this.

Instead of this

    wrdApp.Activate
    wrdApp.Visible = True

Use this

    wrdApp.Visible = True
    wrdApp.Activate

You can't really activate it if it is not visible. Also ensure that the last line in your macro before you destroy the object is wrdApp.Activate. You wouldn't want the wrdApp to loose focus because of any other code...

share|improve this answer
    
Thank you so much, I believe this works. I can't get it to open behind other applications, even after many attempts. Thanks! –  Chris Nov 11 '13 at 9:41
    
You can. Simply activate the current excel application. :) –  Siddharth Rout Nov 11 '13 at 9:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.