none
How to close a specific internet explorer tab?

    Question

  • I am using VB.net in VS 2015.

    In my program I am opening Amazons web page to get some information and I don't have any trouble doing that. However, nothing that I can come up with works to close the page programmatically. I found some code on one of the Microsoft forums that looks like it should work. I put this code into my program and at least it ran with no errors. Now when I run the program it runs without any problems except it does not close the web page. I also found a couple or responses to a similar question, from someone else, that stated this could not be done using visual basic. is that true or should this code work or am I trying to do this completely wrong.

    Private Sub lblItemLookup_Click(sender As Object, e As EventArgs) Handles lblItemLookup.Click
            System.Diagnostics.Process.Start("http://www.amazon.com")
        End Sub
    
    	
        Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
            Dim allProcesses() As Process = System.Diagnostics.Process.GetProcessesByName("Iexplorer")
            For Each xProcess As Process In allProcesses
                If xProcess.MainWindowTitle.Contains("Amazon") Then
                    xProcess.Kill()
                End If
            Next
        End Sub



    JR

    Sunday, April 30, 2017 2:22 AM

Answers

  • In my program I am opening Amazons web page to get some information and I don't have any trouble doing that. However, nothing that I can come up with works to close the page programmatically.

    This works for me:

        Dim IE As Object
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            IE = CreateObject("InternetExplorer.Application")
            IE.visible = True
        End Sub
    
        Private Sub UpdateURL()
            Dim URL As String = "<whatever>"
            IE.navigate(URL)
        End Sub
    
        Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
            IE.Quit()
        End Sub

    If you want to use a process you should use the Process.Start method as a function to start the application. That means you will get a Process object that has the methods required to correctly manage the process.

    • Marked as answer by J_Reid Sunday, April 30, 2017 6:39 AM
    Sunday, April 30, 2017 3:07 AM

All replies

  • In my program I am opening Amazons web page to get some information and I don't have any trouble doing that. However, nothing that I can come up with works to close the page programmatically.

    This works for me:

        Dim IE As Object
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            IE = CreateObject("InternetExplorer.Application")
            IE.visible = True
        End Sub
    
        Private Sub UpdateURL()
            Dim URL As String = "<whatever>"
            IE.navigate(URL)
        End Sub
    
        Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
            IE.Quit()
        End Sub

    If you want to use a process you should use the Process.Start method as a function to start the application. That means you will get a Process object that has the methods required to correctly manage the process.

    • Marked as answer by J_Reid Sunday, April 30, 2017 6:39 AM
    Sunday, April 30, 2017 3:07 AM
  • Thank you! I had to do a little rearranging by combining some of the code into a function so I could call it from several places and have it return different values. But the code works great and is fairly straightforward. You made my day (or Night) I am still pretty new at this and I had thrown everything I could think of at this with very little luck.

    JR

    Sunday, April 30, 2017 6:46 AM