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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AppActivate ( ) not happening until I click to return focus 1

Status
Not open for further replies.

cloverdog

Technical User
Mar 4, 2008
41
GB
I have written a small routine in VBA originally using MS Word VBA but now in Access VBA. The sub( ) is triggered by a Button on a form. It Opens ‘Word’ and ‘Firefox’ copies the initial web page and pastes it into ‘Word’.

It works but for one thing. Once started it runs down as far as copying the web page then stops. I then have to click on the border of MS Access to get it moving again. Then Word is activated and the paste done. It is as though clicking Access returns the focus and so flushes the remaning events.

The Sleep( ) sub uses an API call and is in milliseconds. I did replace this with a a call to a subroutine using the same API but which also gave plenty of DoEvents. Nothing changed, I still had to click on the MSAccess Window to get it to finish. I have tried commenting out all SendKeys but it makes no difference.

The code is:

Dim WordID, FirefoxID

WordID = Shell("C:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE", vbNormalFocus) ' Activate Microsoft
AppActivate WordID, 1 ' Activate Microsoft Word.

FirefoxID = Shell("C:\Program Files\Mozilla Firefox\Firefox.exe", vbNormalFocus)
Sleep 2000
AppActivate FirefoxID, True
SendKeys "{ESC}", True
SendKeys "^a", True
SendKeys "^c", True
' --** Runs to here then need to click on border of access **--
AppActivate WordID, True ' Activate Microsoft Word.
SendKeys "^v", True
MsgBox "done"


Please could you let me know why this happens and, if there is one, suggest a solution.

Thank you for your time
 
My inclination would be to avoid sendkeys, so I would automate Word by setting a reference to it and using CreateObject(Word.Applicaton) to create a new word instance and go from there (I usually record macros in Word / Excel and figure out how to convert them to work as automation).

Although I suspect that will not solve the fundamental problem of Firefox hogging the focus... I would look for a way to accomplish the same thing without shell and sendkeys for Firefox.
 
Thank you Lameid. I came to the same conclusion but really wanted to know if there was something I was missing.

I have tried swapping Firefox for Internet Explorer with the same result (ie not being able to switch back to Word).

There are times when it is very useful to be able to switch between programs and back again. I just can't believe VBA won't do this. I even tried swapping Firefox for InternetExplorer with no send keys. The result was just the same (with or without SendKeys statements in the routine).

Hopefully someone has found a solution to this basic problem of switching back and forth.
 
The thing is, FF has focus and there is nothing to return focus to the controlling app (exccept actually clicking it).

It is outside my knowledge, but is there a way to get a copy from FF without actually using Activate? You would of course have to make an object instance of it...

 
Sorted it! Thank you Lameid and Fumei for your posts.

I hate to say it but, being in a hurry, I pasted code from an MS help example. Something which should not be done.

All I needed to do was set the boolean parameter in AppActivate to False. Setting it to FALSE tells the calling application not to wait for the focus to be returned. This was the right thing to do for the first two calls but not the third.

Reading your replies, this morning, made it jump out. Sometimes it really does help to just get another perspective or two.

Thanks again.

Cloverdog
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top