0

I am automating Internet Explorer with VBA through an Access database.

For one of my programs I need to send a keystroke to the IE browser.
This involves switching the active window to IE from Access to set focus on the text box to use SendKeys. I found advice here to turn IE visibility off and back on.

With upgrade to Windows 10 this is not working smoothly anymore. Specifically, when I set IE visibility off and on, Access still remains the active window (and the IE task icon flashes orange to denote lack of focus). HOWEVER, once I open the VBA editor a single time, the code works again. Even if I close the editor it will continue to work. If I close Access and reopen it will again stop working until I open the VBA editor.

I tried AppActivate instead of the visibility toggle and it sometimes worked but more often than not I will get a runtime error 5 meaning it can’t find the window (despite the fact that it is the exact url/title name).

Any idea why the “visibility toggle” strategy is not working until I open the VBA editor?

Alternatively, are there any other methods to switch the active window?

EDIT:

Reproduceable version of the issue in Excel:

Private Sub Button_Click()

   'initiating a new instance of Internet Explorer and asigning it to objIE
    Set objIE = New InternetExplorer
 
    'make IE browser visible
    objIE.Visible = True
 
    'navigate IE to google
    objIE.navigate "https://www.google.com"
 
    'wait for the browser
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
       
    objIE.Visible = False
    objIE.Visible = True
    
    MsgBox pause
    
End Sub

You will notice that clicking the button after opening the VBA editor will open the google window and make it active. However, if you save your Excel/Access file and re-open, and this time do NOT open the editor, when you click the button it will open google but it will NOT be active (taskbar icon will be flashing orange). However once you open the editor again, the button will open and set focus.

Community
  • 1
  • 1

1 Answers1

0

What code are you using?

I tried this from Excel on a Win 10 PC and it works:

Sub test()
Dim IE As Object

Set IE = Nothing
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "www.google.com"
IE.Visible = True

End Sub

From here originally: VBA to Activate Internet Explorer Window

v-c0de
  • 142
  • 1
  • 3
  • 15