我已經創建了宏,我可以從任何網頁中獲取每個URL。VBA檢查URL的狀態
現在,我在列中有每個URL。
任何人都可以幫助寫入宏來檢查這個URL是否工作。 如果這個URL中的任何一個沒有工作,那麼它應該是錯誤的,不會在下一列的URL旁邊工作。
下面是我寫不過似乎這是不工作的代碼:
Sub CommandButton1_Click()
Dim ie As Object
Dim html As Object
Dim j As Integer
j = 1
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
url = "www.mini.co.uk"
ie.navigate url
Do While ie.READYSTATE <> READYSTATE_COMPLETE
Application.StatusBar = "Trying to go to website ..."
Loop
Application.StatusBar = " "
Set html = ie.document
'Dim htmltext As Collection
Dim htmlElements As Object
Dim htmlElement As Object
Set htmlElements = html.getElementsByTagName("*")
For Each htmlElement In htmlElements
'If htmlElement.getAttribute("href") <> "" Then Debug.Print htmlElement.getAttribute("href")
If htmlElement.getAttribute("href") <> "" Then Cells(j, 1).Value = htmlElement.getAttribute("href")
j = j + 1
Next
ActiveSheet.Range("$A$1:$A$2752").removeDuplicates Columns:=1, Header:=xlNo
End Sub
本規範來從網頁的URL。
下面的代碼我試圖檢查URL的狀態,如果它的工作與否。
Sub CommandButton2_Click()
Dim k As Integer
Dim j As Integer
k = 1
j = 1
'Dim Value As Object
'Dim urls As Object
'urls.Value = Cells(j, 1)
For Each url In Cells(j, 1)
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False
url = Cells(j, 1)
ie.navigate url
Do While ie.READYSTATE <> READYSTATE_COMPLETE
Application.StatusBar = "checking the Data. Please wait..."
Loop
Cells(k, 2).Value = "OK"
'Set html = ie.document
ie.Quit
j = j + 1
k = k + 1
Next
End Sub
但是,此代碼無法正常工作,請向我推薦更改。
問候, MAYUR Alaspure
嘗試圍繞這些鏈接https://msdn.microsoft.com/en-us/library/ms767625(v=vs.85).aspx和https://msdn.microsoft東西。 com/en-us/library/windows/desktop/aa383887(v = vs.85).aspx –