Skip to main content

How to Open a Web Page Using VBA

Use API method to open url with default browser

Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Sub OpenWebPageAPI()
    Dim url As String
    
    url = "https://excelbaby.com/"
    
    ShellExecute 0, vbNullString, url, vbNullString, vbNullString, vbNormalFocus
End Sub

Use API method to open url with specified browser

Sub OpenWebPageShellExecute()
    Dim objShell As Object
    Dim url As String
    
    Set objShell = CreateObject("Shell.Application")
    url = "https://excelbaby.com/"
    
'    Call objShell.ShellExecute("chrome.exe", "-url " & url, "", "open", 1)
    Call objShell.ShellExecute("msedge.exe", "-url " & url, "", "open", 1)
End Sub

Use the FollowHyperlink method to open url with default browser

Sub OpenWebPageFollowHyperlink()
    Dim url As String
    
    url = "https://excelbaby.com/"
    
    ActiveWorkbook.FollowHyperlink Address:=url, NewWindow:=True
End Sub

Use the InternetExplorer object to open url with Internet Explorer

Sub OpenWebPageIEobj()
    Dim IE As Object
    Dim url As String
    
    Set IE = CreateObject("InternetExplorer.Application")
    url = "https://excelbaby.com/"
    
    IE.Visible = True
    IE.Navigate (url)
End Sub

Use Shell method to open url with specified browser

Sub OpenWebPageShell()
    Dim url As String
    Dim BrowserPath As String
    
'    BrowserPath = """C:\Program Files\Internet Explorer\IEXPLORE.EXE"""
'    BrowserPath = """C:\Program Files\Google\Chrome\Application\chrome.exe """
'    BrowserPath = """C:\Program Files\Google\Chrome Dev\Application\chrome.exe"""
    BrowserPath = """C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"""
    
    url = "https://excelbaby.com/"
    
    Shell BrowserPath & url, vbNormalFocus
End Sub

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>

1 comment
  1. GS
    gst

    Interesting. However how can I save the webpage? The html file.