0

I'm making a macro that opens Internet Explorer, navigates and logs into a website. Everything works fine, but I need to bring the IE Window up front, and activate it, so I can use SendKeyson it. I've found websites and videos with different approaches on a command called AppActivate and i've tried many of them, but even if I copy the entire code (which works for the author) it won't work for me, I always get an error: Invalid Procedure Call or Argument - Error 5

A list of everything I've found and tried:

Dim objIE As InternetExplorerMedium
Set objIE = New InternetExplorerMedium
objIE.navigate "http://google.com"
'makes it visible, but not active
objIE.Visible = True

'A list of ways I've tried:
objIE.AppActivate "Google - Internet Explorer"
AppActivate "Google - internet Explorer"
'the above supposedly looks for the title of the page
AppActivate objIE
AppActivate (objIE)
AppActivate "objIE"

observations: I'm running this inside Excel 2013 and I'm using Windows 7 with IE 11.

SamRosignoli
  • 29
  • 1
  • 2
  • 6
  • Why are you using VBA to do this? What is your end goal? (I'm asking as a person who once wrote things like this and over the years have learned there are better ways than automating IE) – Cody G Sep 01 '17 at 20:37
  • My boss wanted Excel to do all of this, but we´re getting tired of it. Do you have a better idea? What do you think I should use? I wanted to use Chrome instead, IE is too slow. i just need a good tool to automate Chrome. – SamRosignoli Sep 04 '17 at 17:17
  • Are you just testing the log in functionality of a web page? (What are you doing after you log in?) – Cody G Sep 04 '17 at 18:48
  • Also chrome has chrome headless now which you can automate! (Pretty exciting times...). People also use Selenium and phantomjs (no longer maintained) – Cody G Sep 04 '17 at 18:50
  • Here's the basic answer: Whatever data is it is that you're trying to get through a web page is stored somewhere else!! If you're scraping information off a web page you may not be aware that doing something like that may end up getting you hit with captcha's which is just going to cause your web scraping program to fail (not robust!). If it's an internal webpage you need to speak with your database admins to get you the information you want. If it's an outside company's webpage you should look at seeing if they have a SOAP or REST/JSON API --- sometimes they're free sometimes not. – Cody G Sep 04 '17 at 18:53
  • But if you're merely getting a CSV file from a webpage after authenticating to it (and they offer no other forms of authentication), then throw it into excel... then excel/IE is actually your best bet. – Cody G Sep 04 '17 at 18:56

2 Answers2

1

I always just make IE an untyped object, like so

Sub test()
Dim IE As Object

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

End Sub

The IE window has focus when this finishes running on my end.

barvobot
  • 887
  • 1
  • 7
  • 17
0

Someone write similar things here:

Get existing IE via VBA

With a little modification: (SetForegroundWindow Lib "user32" ) So that after it search for the Existing IE, it will appear on the top of our screen

*PtrSafe / LongPtr is to be used in 64-bit system *You may delete it if you are using 32-bit


Call Library

Public Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal HWND As LongPtr) As LongPtr

Main Sub

Sub Test1()

    Dim IE1 As Object
    Set IE1 = ThisIE
    
    IE1.navigate "http://stackoverflow.com"
    
    Do While IE1.readyState <> READYSTATE_COMPLETE
    Loop
    
    SetForegroundWindow (IE1.HWND)
End Sub

Function to be called

Function ThisIE() As Object

    For Each ThisIE In CreateObject("Shell.Application").Windows()
        If (Not ThisIE Is Nothing) And ThisIE.Name = "Internet Explorer" Then Exit For
    Next ThisIE
    
    If ThisIE Is Nothing Then Set ThisIE = CreateObject("InternetExplorer.Application")
        ThisIE.Visible = True
        
End Function