0

I wrote a script that will open a workbook and run the macro within the workbook. The macro is supposed to open internet explorer and log into a website using the SendKeys command. The issue that I have is that internet explorer opens in the background while Excel is running in the front. Essentially, I would like the newly opened internet explorer to be in the front and selected so that I could use the SendKeys command. I know that the macro works when I run it manually and for some reason it also rarely works (after computer restart). I'm not sure if this is a timing issue.

The reason why I have to use SendKeys is because the log in button will only work if you manually type in the id and password. I also tried disabling the button using this but it didn't work so I have to use SendKeys.

With .querySelector("#subBtn")
                .disabled = False
                .Click

Multiple people already asked about this question but it seems like there isn't a definitive answer. Here are the codes that I tried.

Dim ie As New InternetExplorer
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
CreateObject("WScript.Shell").AppActivate "Internet Explorer"
ie.Navigate "Website here"

I also tried showing the i.e after the website loaded and it didn't work.

Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "website here"
While ie.Busy Or ie.ReadyState <> READYSTATE_COMPLETE
    DoEvents
Wend
ie.visible = True 
DoEvents

I also tried minimizing excel first and then setting i.e. visible=false and turn it back to true. This brings internet explorer to the front, but it is not selected (focus?) so the SendKeys command won't work.

    ActiveWindow.WindowState = xlMinimized
    ie.Visible = False
    DoEvents
    ie.Visible = True

I would like to have internet explorer in the front and selected so that I can SendKeys.

Chum Ba
  • 47
  • 6
  • AppActivate is a common way to go to bring to front (as you have tried). Often there are events you can trigger to make logins work without sendkeys either with .Focus, .FireEvent _eventType_ or attaching events (or combinations thereof). Can you share the url? Effectively, we only need to demonstrate password/username not valid. – QHarr May 16 '19 at 04:05
  • This is actually part of the question that you've answered before. Here is the link to the previous question. https://stackoverflow.com/questions/56082906/vba-to-click-on-button#. The issue with the code is that even though it enables the log in button by setting ```.disabled=false```. It actually doesn't click and process the information. The page only seems to refresh. You can tell the difference when you manually type in a random user id and password and click log in. You'll get a log on attempt error. – Chum Ba May 16 '19 at 04:27
  • I usually test my code and that code IIRC does correctly submit (as far as I could tell). I got incorrect credentials message which was to be expected given I entered random info. – QHarr May 16 '19 at 04:32
  • I believe you. Perhaps there is something in my settings that is erroring out for me. I'll keep trying your code to see if it's something on my end. – Chum Ba May 16 '19 at 04:36
  • Try to refer these link may help to solve the issue. http://dailydoseofexcel.com/archives/2008/05/23/giving-ie-the-focus/ and https://stackoverflow.com/questions/11196968/set-focus-to-internet-explorer-object-in-visual-basic – Deepak-MSFT May 16 '19 at 08:43

0 Answers0