2

I'm creating a post as I've been researching this topic and have found some contradicting answers surrounding the ability to automate Edge IE Mode with VBA without third-party applications (see solutions below).

I currently have an app that accomplishes the following in IE:

  • Verifies whether or not an instance of IE is already open and navigated to a specific URL;
  • Creates a new IE instance if the above isn't true;
  • Scrapes data available from the HTML doc; and
  • Clicks elements to navigate to other tabs and pages.

Leading up to the deprecation of IE, I'm unsure how to direct my code to specifically communicate with Edge IE mode instead of it attempting to create a brand new instance of Internet Explorer. Does anyone possibly have specific examples that I could reference?


Response 1: https://stackoverflow.com/a/71994505/9791828

In that thread, one of the responses indicates that this automation is possible.

As of 25th April 2022, you can now directly automate Edge IE Mode with VBA without any additional third party-software. The below guidance has been well tested by me and my colleagues after obtaining it from exchanging with our partnered Microsoft Support team.

What You Need to Do

  1. Your Windows version needs to be at least 20H2. You can check your Windows version with this guide here.

  2. Your Windows needs to have the following KBs installed: KB5011487. Or simply update your Windows and this should be automatically installed as well. You can check your Windows update history with this guide here.

  3. Finally install the below registry keys on your Windows and restart:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Internet Explorer\Main] "NotifyDisableIEOptions"=dword:00000002

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Internet Explorer\Main\EnterpriseMode] "EnableGlobalWindowListInIEMode"=dword:00000001

Per the MS Support team, the above method should work until 2029. Official documentation on this might be coming soon I believe.

After the above steps, VBA shall be able to interact with Edge IE Mode as if it is an Internet Explorer window. Your current code that automates the InternetExplorer.Application object will work with Edge IE mode as well.

Response 2: https://learn.microsoft.com/en-us/answers/questions/829365/vba-automation-with-edge-ie-mode.html

One of the community experts stated that this isn't possible without Selenium and a driver.

If Selenium is not an option, I think you can't automate with Edge IE mode.

The only way to automate with Edge IE mode is using IEDriver. You can't do it with only VBA. You need to:

  1. Define InternetExplorerOptions with additional properties that point to the Microsoft Edge browser.

  2. Start an instance of InternetExplorerDriver and pass it InternetExplorerOptions. IEDriver launches Microsoft Edge and then loads your web content in IE mode.

For more information, you can refer to this doc.

pacman
  • 21
  • 1
  • 5

2 Answers2

1

Unfortunately, it seems to be impossible to implement IE mode automation in the Edge browser using VBA so far. Even with SeleniumBasic, since there's no InternetExplorerOptions class or the like available, you cannot run the automation in the IE mode using VBA.

However, for now you don't have to worry about IE automation after the IE retirement. I tried to disable the IE11 browser following Response 1, and it turned out that VBA code still successfully launched IE browser window. You could expect your VBA code will work after the IE retirement.

For more information of IE retirement, please read this article:

I ran into issues with my application which utilizes IE through automation. Will this be fixed? (Updated: April 14, 2022)

As part of the IE 11 application retirement, certain COM automation scenarios were inadvertently broken. These IE COM objects have been restored to their original functionality as of the Windows 11 November 2021 “C” update and the Windows 10 March 2022 “B” update. The COM scenarios will also continue to work after the IE11 desktop application is disabled after June 15, 2022. If you continue to experience issues after taking the update, please contact App Assure for remediation assistance.

Yu Zhou
  • 11,532
  • 1
  • 8
  • 22
  • 1
    Hi @pacman May I know if you have got any chance to check my answer? I am glad to help if you have any other questions. – Yu Zhou May 24 '22 at 02:28
0

My VBA (v7.1.1126) interrogates the HTML from IE directly; so I have used Selenium with Python (v3.10.6) to pull the equivalent data from Chrome and save it to a text file which my VBA can then access and interpret. VBA can call Python directly.

Step 1: Install the latest stable [Python][1]

Step 2: Install the latest stable [PyCharm][2] and read how to use it from the same link

Step 3: Open Chrome. Click on three dots near top right. Click on Settings then About Chrome to see the version of your Chrome. Download the corresponding driver [here.][3] Save the driver in the PyCharm PATH folder

Step 4: Create a new project and install the packages selenium and webdriver-manager as explained in the PyCharm link above

Step 5: Create a new blank Python file vba.py This willl be updated by the VBA in the next step.

Step 6: Add this code to a new VBA procedure to ensure that it works for you:

    Public Sub GetHtml(sURL$, sHTML$, iChannel%, sOutputFile$)
    
    'sURL - web page HTML code sought
    'sHTML - where HTML pulled is stored
    'iChannel - channel used for reading/writing output from/to textfiles
    'sOutputFile - where HTML is saved for manual interrogation when debgging e.g. \Output.txt
    
    Dim iLoopCount%
    Dim complete$, pythonexe$, script$, script2$, sFile_Path$, textline$
            
    'Python Script
    
    script = "" & _
    "from selenium import webdriver" & vbCrLf & _
    "from selenium.webdriver.chrome.service import Service" & vbCrLf & _
    "from webdriver_manager.chrome import ChromeDriverManager" & vbCrLf & _
    "from selenium.webdriver.chrome.options import Options" & vbCrLf & _
    "import time" & vbCrLf & _
    "outputFile = r""" & ActiveWorkbook.path & sOutputFile & """" & vbCrLf & _
    "completeFile = r""" & ActiveWorkbook.path & "\complete.txt""" & vbCrLf & _
    "options = Options()" & vbCrLf & _
    "options.headless = True" & vbCrLf & _
    "driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)" & vbCrLf & _
    "driver.get(""" & sURL & """)" & vbCrLf & _
    "driver.delete_all_cookies()" & vbCrLf & _
    "driver.implicitly_wait(12)" & vbCrLf & _
    "time.sleep(12)" & vbCrLf & _
    "pageSource = driver.page_source" & vbCrLf & _
    "driver.quit()" & vbCrLf & _
    "with open(outputFile, 'w', encoding='utf-8') as f:" & vbCrLf & _
    "    f.write(pageSource)" & vbCrLf & _
    "f.close()" & vbCrLf & _
    "with open(completeFile, 'w') as f2:" & vbCrLf & _
    "    f2.write('complete')" & vbCrLf & _
    "f2.close()"
    
    'Write the Script into a python file
    sFile_Path = "C:\Users\user\PycharmProjects\VbaProject1\Main\vba.py"
    Close #iChannel
    Open sFile_Path For Output As #iChannel
    Print #iChannel, script
    'closing channel forces complete write of script to python file
    Close #iChannel
    
    Close #iChannel
    Open ActiveWorkbook.path & "\complete.txt" For Output As #iChannel
    Print #iChannel, "incomplete"
    Close #iChannel
    
    pythonexe = "C:\Users\user\PycharmProjects\VbaProject1\venv\Scripts\python.exe"
    
    'restart point for python crash below
    complete = "incomplete"
    Do While complete = "incomplete"
        'Execute Script.py
        Call Shell(pythonexe & " " & sFile_Path)
        
        Application.Wait (Now + TimeValue("0:00:10"))
        
        'this loop minimises waiting time for the python run to complete
        
        iLoopCount = 1
        Do While complete = "incomplete" And iLoopCount < 7
            Application.Wait (Now + TimeValue("0:00:05"))
            
            'close textfile if python run not complete and file open
            Close #iChannel
            
            Open ActiveWorkbook.path & "\complete.txt" For Input As #iChannel
            Line Input #iChannel, complete
            
            'this is rarely true and if so it so far only happens for a python error
            ' which is resolved by running the python code from PyCharm in debug mode
            ' and then setting the next statement to the restart point above
            ' and resuming play of the VBA
            If complete = "incomplete" And iLoopCount > 5 Then Stop
            
            Close #iChannel
            iLoopCount = iLoopCount + 1
        Loop
    Loop
    
    Open ActiveWorkbook.path & sOutputFile For Input As #iChannel
    sHTML = ""
    Do Until EOF(iChannel)
        Line Input #iChannel, textline
        sHTML = sHTML & textline
    Loop
    
    End Sub

Python code inserted by VBA into vba.py above with additional comments:

'''python

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
import time
outputFile = r"C:\Users\user\Documents\HP Laptop\Documents\Documents\Jobs\DIT\IDMB\Output.txt"
completeFile = r"C:\Users\user\Documents\HP Laptop\Documents\Documents\Jobs\DIT\IDMB\complete.txt"
options = Options()
# headless means that browser requests solely pull the data without creating a GUI - a visual representation of a page
options.headless = True
# point driver to Chrome driver previously saved in PATH folder - Step 3
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
# request HTML page
driver.get("https://www.imdb.com/title/tt0097334/?ref_=adv_li_i")
driver.delete_all_cookies()
driver.implicitly_wait(12)
time.sleep(12)
# HTML
pageSource = driver.page_source
# close browser
driver.quit()

# save HTML for subsequent dissection by VBA
with open(outputFile, 'w', encoding='utf-8') as f:
    f.write(pageSource)
f.close()

# mark run as complete 
with open(completeFile, 'w') as f2:
    f2.write('complete')
f2.close()

  [1]: https://www.python.org/downloads/
  [2]: https://www.jetbrains.com/help/pycharm/installation-guide.html
  [3]: https://chromedriver.storage.googleapis.com/index.html
Cyrus
  • 356
  • 3
  • 14