How to Launch the Default Browser and Display a Web Page in Excel VBA

目次

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

ItemDescription
targetURLStores the address of the web page you want to display. By retrieving this from a cell or form, dynamic URL specification is possible.
WScript.ShellAn object used to access Windows shell features. It can be created dynamically using CreateObject.
Run MethodSimply 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

PurposeImplementation Example
Display Local HTMLYou can open offline materials by specifying a file path (e.g., “C:\Guide\index.html”) in targetURL.
Link with User InputIt is possible to pass a URL entered via InputBox to Run and display it in the browser immediately.
Synchronous ExecutionBy 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.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次