I need code that ran in IE to work in Edge.

Gary Troutman 11 Reputation points
2021-08-31T05:34:46.03+00:00

I have a program that has developed over the last 20 and now contains over 2000 lines of code and is now in jeopardy, approaching final jeopardy. The problem is the extinction of IE.

The code uses the following code to open the IE browser and run a given URL:

Dim obj As Object
Set obj = CreateObject("InternetExplorer.Application") obj.Navigate ThisURL

Interestingly enough, it opens Edge and does activate the URL.

However, on the end of an hour after activating that browser instance, the following code that used to close the IE browser fails to close it in Edge. That code is in another sub() as follows.

Dim obj As Object: Dim ie As Object
Set obj = CreateObject("shell.application")
For Each ie In obj.Windows
On Error GoTo ForgetMe
If TypeName(ie.Document) = "HTMLDocument" Then
ie.Quit

A token URL can be:
ThisURL = "http://player.streamguys.com/gpb/sgplayer"

Now that I have to use Edge, all I am getting is tab after tab open to various URLs and I end up with many broadcasts running all at once. Tabs are not closing as needed.

How do I close the browser/tab on command from my program There can be no manual action required. It has to be automated.

I have 400 residents that benefit from this program so this is no trivial matter.

Microsoft Edge
Microsoft Edge
A Microsoft cross-platform web browser that provides privacy, learning, and accessibility tools.
2,354 questions
{count} vote

3 answers

Sort by: Most helpful
  1. Yu Zhou-MSFT 13,591 Reputation points Microsoft Vendor
    2021-08-31T08:42:55.24+00:00

    Hi @Gary Troutman

    The VBA code you show won't open Edge browser. I guess it opens Edge because this policy is set on your computer so the site is redirected to Edge. Besides, Edge doesn't have an API that VBA can use, and even more unlikely that you use an IE function ie.Quit to close Edge browser.

    As a workaround, you can try to call shell command in VBA to kill Edge process: Call Shell("TaskKill /F /IM msedge.exe"). I made a code example like below, it can close Edge browser successfully. You can also refer to it:

    Sub LOADEdge()  
       Set obj = CreateObject("Shell.Application")  
       obj.ShellExecute "microsoft-edge:http://player.streamguys.com/gpb/sgplayer"  
       Application.Wait (Now + TimeValue("00:00:10"))  
       Call Shell("TaskKill /F /IM msedge.exe")  
    End Sub  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Regards,
    Yu Zhou

    2 people found this answer helpful.

  2. Gary Troutman 11 Reputation points
    2021-08-31T21:50:37.053+00:00

    Ok, it looks as if it is working with one small cosmetic issue.

    Your code needed one line: Dim obj As Object:

    I did not finalize the suspicion that the code locked the window because I realized that the only line of code that I needed was: Call Shell("TaskKill /F /IM msedge.exe")

    Edge intercepted my lines of code directed to IE and implemented it in Edge. The problem was that Edge did not like the line to close the browser. I simply replaced the old IE code to close it and let Edge code finish the task.

    The program has been running now for a few hours and URLs are changing as needed. I will keep a watch on it for the week to see if it will work in real time this weekend.

    In a final cheap little visual effect, to run the browser minimized, I was using the code:
    obj.Visible = False
    It does not seem to be working in edge.
    Any thoughts to get that one running?

    1 person found this answer helpful.
    0 comments No comments

  3. Yu Zhou-MSFT 13,591 Reputation points Microsoft Vendor
    2021-09-01T03:12:16.04+00:00

    Hi @Gary Troutman

    You can't use obj.Visible = False to minimize Edge. The reason is the same as what I said before: Edge doesn't have an API that VBA can use. In this situation, we can only try to see if calling shell command in VBA can minimize the window.

    We need to use a third-party tool called NirCmd to minimize the window. After downloading and moving the tool to Windows directory, we can use the following line in VBA to minimize Edge window:

    Call Shell("nircmd win min process msedge.exe")  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Regards,
    Yu Zhou

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.