Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set focus using VBA

Status
Not open for further replies.

wmbb

Technical User
Jul 17, 2005
320
0
0
NL
We are using an internal application that opens MS WORD creating a report using VBA macro's.
If the report already exist, a message pops up in WORD (initiated by a VBA macro) showing that the report already exist.
This works fine as long as MS WORD was not opened at that moment.
If MS WORD is started by the application WORD get the focus and is on top of the windows.
But, when WORD was already open when the internal application is activating WORD, the internal application stays in focus (on top) of the windows and the popup message is underneath other windows and waits for user-input (pressing the OK button).
Because this popup window is not on top, users sometimes wait for minutes until they find out that the popup windows is underneath the other windows and they have to activate this window by themselves.
Is there a way in VBA to take focus of MS WORD or this popup window (message box) ?

I hope you understand the problem of this story.
 
How do you get access to the word application? An example of getting word active after GetObject:
Code:
Dim wdApp As Word.Application
Set wdApp = GetObject(, "Word.Application")
wdApp.Visible = True
wdApp.Activate

combo
 
Hi Combo,

Thanks for your quick response.
Our internal application generates a data-file with the data from the application.
Subsequently a WORD template is opened from within the application containing the AutoOpen macros.
If a sequence of WORD is already open the template is opened in the background and the message-box is also displayed in the background.
I would like to add a command in the AutoOpen macro to bring the WORD application on top (in focus).

Using your code, an error occurs: Cannot activate application !

I also tried the command AppActivate "Microsoft Word" without succes.

Other suggestions ?
 
wmbb said:
Subsequently a WORD template is opened from within the application containing the AutoOpen macros.
If the code is in a word document/template, use only [tt]Application.Activate[/tt].

combo
 
Do you mean just put "Application.Activate" in the beginning of the code ?
Doing so, an error occurs: Cannot activate application !
 
Please describe the whole process and critical code. In my second post I assumed that the line of code is in the word document.

As I understood:
1) you have an external tool that generates data and calls word,
2) word uses a template with code, that processes the data and generates a report,
3) there is a problem with displaying word when an instance is already open in the background.
Am I ok? If not, how precisely it work (host application, code that opens word, any code in word template)?
The questions:
1) why can't you activate word by the tool that opens word (I assumed this in the first code sample, should work for early binding - i.e. referencing word library)?
2) where did you put the "Application.Activate" so that it does not work?

combo
 
Add vbSystemModal to the Buttons option of the message box.

This WILL NOT bring it to the top if Word in the background (MS changed the behaviour of system modal dialogs a long time ago because they decided that forcing things to the front and grabbing the focus was a bad thing to do), but it will cause it to appear in the taskbar and to flash there which should achieve the affect of geting the user's attention
 
Thank you both for your support.

Combo:
The situation you described in point 1 to 3 is exactly the point.
The code the internal application opens the WORD session is WinShellExec(cReport_Dot);
where cReport_Dot is a variable with value "LimsReportMacro.dot"
If WORD is not open at the moment of execution, WORD is started and the template is opened.
In this template there is a AutoOpen macro filling in the data from the datafile.
In this case, Word is in focus and you can see the messages initiated by the macro's.

But, if WORD is already started the template is opened in the background and the internal application stays in focus.
In that case Word is in the background and the messages initiated by the macro's in the template also appear behind other windows.

My goal is to display the messages (for example that the report already exist with the same name) on top of all windows so the user will see that the macro is waiting for a user action.

I've put the code Application.Activate as the first command in the AutoOpen macro.

Code:
Sub AutoOpen()
Application.Activate
StartConversion
End Sub

Sub StartConversion()

  'Check Locations
  If checkLocations = False Then GoTo stopmacro
  
  'Read LIMSdata from C:\StarLIMS9\reportdata\DataFromLims.txt
  nextstep = True
  ReadLimsData
  If nextstep = False Then GoTo stopmacro

.....



 
>My goal is to display the messages (for example that the report already exist with the same name) on top of all windows

I'll just repeat - current best practice is to NOT do this in Windows, which is why Microsoft modified the behaviour of system modal windows
 
Hi strongm,

I've tried your option but nothing was flashing in the taskbar !
 
I did some tests with API ShellExecute function instead. For a module in a template only AutoNew procedure fires. The AutoOpen procedure starts automatically when the file containing it is opened, in this case the template file. Are you sure that you create a new document based on LimsReportMacro.dot template?
I had no problem with activating the word.
What happens when you debug the code (add "Stop" at the beginning of procedure in a template)? Does the Application.Activate at the end of procedure make a difference? Alternatively you can try to play with word's Tasks collection.


combo
 
No, I don't create a new document based on the template.
The template is opened as dot file (Read Only) just to run the macro's.
Moving application.activate at the end of the procedure will not work because the message is generated before.
Maybe I just forget about this......
 
>but nothing was flashing in the taskbar

Perhaps you'd like to show your code
 
Sorry, I added the wrong code to the button.
I added vbApplicationModal instead of vbSystemModal.

vbSystemModal seems to work with my testcode.
I'll try this in the real code and solve my problem this way....

I don't think it's useful to publish my whole code because it is several pages long.

For now thanks for your help.
 
The template is opened as dot file (Read Only) just to run the macro's."

If you are not creating a new document from the template - you just want access to procedures (macros) - then you do not need to open the file at all. Load it dynamically as an add-in. Once loaded (but NOT opened), you have full access to the cde in the templae.

Gerry
 
Hoi Gerry,

That could be an option but the internal application just opens this template and I don't have contol of it.
But the problem stays because the messages are opend in the background when WORD is already started.
I'll solve the problem with the suggestion of strongm.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top