下面的幾段VBA代碼都可以打開瀏覽器并打開指定的網頁:
方法一:用API打開默認的瀏覽器
Private Declare 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 OpenWebPage1()
ShellExecute 0&, vbNullString, "www.zhixing123.cn", vbNullString, vbNullString, vbNormalFocus
End Sub
方法二:用“FollowHyperlink”方法:
Sub OpenWebPage2()
ActiveWorkbook.FollowHyperlink Address:="http://www.excel123.cn", NewWindow:=True
End Sub
注意網址中要包含“http://”。
方法三:用“InternetExplorer”對象
Sub OpenWebPage3()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate ("www.excel123.cn")
End Sub
方法四:用Shell語句
這個方法可以用指定的瀏覽器打開某個網頁。例如調用IE打開網址“www.zhixing123.cn”
Sub OpenWebPage4()
Dim url As String
url = "www.excel123.cn"
Shell "C:\Program Files\Internet Explorer\IEXPLORE.EXE " & url, vbNormalFocus
End Sub
說明:
1.將“C:\Program Files\Internet Explorer\IEXPLORE.EXE ”換成其他瀏覽器程序,則可用指定的瀏覽器打開網頁。例如系統中已安裝遨游瀏覽器(Maxthon),并安裝在“C:\Maxthon2”文件夾中,將上述語句更改為“C:\Maxthon2\Maxthon.exe ”將用Maxthon打開指定的網頁。
2.注意“C:\Program Files\Internet Explorer\IEXPLORE.EXE ”的結尾處有一空格。如果忽略此空格,Excel將出現錯誤提示“文件未找到”。






