下面的幾段VBA代碼都可以打開(kāi)瀏覽器并打開(kāi)指定的網(wǎng)頁(yè):
方法一:用API打開(kāi)默認(rèn)的瀏覽器
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
注意網(wǎng)址中要包含“http://”。
方法三:用“InternetExplorer”對(duì)象
Sub OpenWebPage3()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate ("www.excel123.cn")
End Sub
方法四:用Shell語(yǔ)句
這個(gè)方法可以用指定的瀏覽器打開(kāi)某個(gè)網(wǎng)頁(yè)。例如調(diào)用IE打開(kāi)網(wǎng)址“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
說(shuō)明:
1.將“C:\Program Files\Internet Explorer\IEXPLORE.EXE ”換成其他瀏覽器程序,則可用指定的瀏覽器打開(kāi)網(wǎng)頁(yè)。例如系統(tǒng)中已安裝遨游瀏覽器(Maxthon),并安裝在“C:\Maxthon2”文件夾中,將上述語(yǔ)句更改為“C:\Maxthon2\Maxthon.exe ”將用Maxthon打開(kāi)指定的網(wǎng)頁(yè)。
2.注意“C:\Program Files\Internet Explorer\IEXPLORE.EXE ”的結(jié)尾處有一空格。如果忽略此空格,Excel將出現(xiàn)錯(cuò)誤提示“文件未找到”。