最近工作中需要使用在指定的網(wǎng)頁中下載一些文件。為了提高工作效率,準(zhǔn)備使用Excel+VBA來實現(xiàn)。
下面就分享幾種使用VBA代碼打開瀏覽器并打開指定的網(wǎng)頁的方法
方法一:用API打開默認(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.baidu.com", vbNullString, vbNullString, vbNormalFocus
End Sub
方法二:用“FollowHyperlink”方法:
Sub OpenWebPage2()
ActiveWorkbook.FollowHyperlink Address:="http://www.baidu.com", NewWindow:=True
End Sub
注意網(wǎng)址中要包含“http://”。
方法三:用“InternetExplorer”對象
Sub OpenWebPage3()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate ("www.baidu.com")
End Sub
方法四:用Shell語句
這個方法可以用指定的瀏覽器打開某個網(wǎng)頁。例如調(diào)用IE打開網(wǎng)址“www.baidu.com”
Sub OpenWebPage4()
Dim url As String
url = "www.baidu.com"
Shell "C:\Program Files\Internet Explorer\IEXPLORE.EXE " & url, vbNormalFocus
End Sub
說明:
1.將“C:\Program Files\Internet Explorer\IEXPLORE.EXE ”換成其他瀏覽器程序,則可用指定的瀏覽器打開網(wǎng)頁。例如系統(tǒng)中已安裝遨游瀏覽器(Maxthon),并安裝在“C:\Maxthon2”文件夾中,將上述語句更改為“C:\Maxthon2\Maxthon.exe ”將用Maxthon打開指定的網(wǎng)頁。
2.注意“C:\Program Files\Internet Explorer\IEXPLORE.EXE ”的結(jié)尾處有一空格。如果忽略此空格,Excel將出現(xiàn)錯誤提示“文件未找到”。