Introduction
This article explains the procedure to automatically launch the default browser from Excel VBA and open a specific web page. Using morinokabu.com as an example, we will introduce code and tips useful for improving business efficiency and instantly displaying internal portals.
Environment
- OS: Windows 10 / 11
- Excel: Microsoft 365, or Excel 2016 and later
- Language: VBA (Visual Basic for Applications)
Sample Code
Sub LaunchDefaultBrowser()
Dim targetURL As String ' URL to display
Dim shellObj As Object ' WScript.Shell object
targetURL = "https://morinokabu.com/"
Set shellObj = CreateObject("WScript.Shell")
shellObj.Run targetURL ' Display page in default browser
End Sub
Code Explanation
| Item | Description |
| targetURL | Stores the address of the web page you want to display. By retrieving this from a cell or form, dynamic URL specification is possible. |
| WScript.Shell | An object used to access Windows shell features. It can be created dynamically using CreateObject. |
| Run Method | Simply passing the URL will automatically launch the default browser and display the page. Use the 2nd and 3rd arguments if you want to control waiting or window style. |
Application Ideas
| Purpose | Implementation Example |
| Display Local HTML | You can open offline materials by specifying a file path (e.g., “C:\Guide\index.html”) in targetURL. |
| Link with User Input | It is possible to pass a URL entered via InputBox to Run and display it in the browser immediately. |
| Synchronous Execution | By setting shellObj.Run targetURL, 1, True, you can make VBA wait for the next process until the browser is closed. |
FAQ
Q. How do I specify a particular browser?
A. Please specify the executable file name, such as shellObj.Run “msedge.exe “”” & targetURL & “”””. However, this requires that the browser is installed on the PC.
Q. What should I do if the URL contains Japanese characters?
A. You can prevent errors by encoding the URL using a function like EncodeURL before assigning it.
Summary
By using the Run method of WScript.Shell, you can instantly launch the default browser from Excel VBA and display a web page. Please make use of this as a mechanism to open internal manuals or external sites with a single click.
