我使用excel VBA打開google結果的第一個返回頁面。從第一頁開始,我根據元素ID操作數據。在做這個過程中,我遇到了一個非常奇怪的行爲。Excel VBA打開第一個返回的google頁面 - 怪異行爲
讓我簡要介紹一下我正在嘗試做什麼。
我將在用戶表單中輸入名字和姓氏。對於給定的名字和姓氏,我將搜索linkedin個人資料。
例如,如果第一個名字是Sachin,姓氏是Tendulkar,我將使用VBA將搜索字詞Sachin Tendulkar linkedin傳遞給Google。對於返回的搜索結果,我將打開第一個搜索結果頁面並嘗試獲取LinkedIn個人資料數據。
我的代碼到目前爲止如下。
Private Sub CommandButton1_Click()
Dim ie As InternetExplorer
Dim RegEx As RegExp, RegMatch As MatchCollection
Dim MyStr As String
Dim pDisp As Object
Dim FirstName As String
Dim LastName As String
Dim sample As String
Set ie = New InternetExplorer
Set RegEx = New RegExp
Dim iedoc As Object
Dim openedpage As String
Dim inpagestrt, inpageend As Integer
Dim returnstatement As String
Dim detailname, locationdetails, profileexperience, profilecontact
Dim overview,skillslist, profilelanguages, profileeducation, publicgroups
detailname = ""
returnstatement = ""
locationdetails = ""
profileexperience = ""
profilecontact = ""
overview = ""
skillslist = ""
profilelanguages = ""
profileeducation = ""
publicgroups = ""
FirstName = TextBox1.Value
LastName = TextBox2.Value
ie.Navigate "http://www.google.com/search?hl=en&q=" & FirstName & "+" & LastName & "+linkedin&meta="
Do Until ie.ReadyState = READYSTATE_COMPLETE
Loop
MyStr = ie.Document.body.innerText
Set RegMatch = RegEx.Execute(MyStr)
'If a match to our RegExp searchstring is found then launch this page
If RegMatch.Count > 0 Then
ie.Navigate RegMatch(0)
Do Until ie.ReadyState = READYSTATE_COMPLETE
Loop
'****************************************
'EDITS
'****************************************
Set iedoc = ie.Document
Dim extractedHTML As String
Dim iStart, iEnd As Integer
extractedHTML = iedoc.getElementById("search").innerHTML
iStart = InStr(1, extractedHTML, "href=", vbTextCompare) + Len("href=") + 1
iEnd = InStr(iStart, extractedHTML, Chr(34), vbTextCompare)
'extract the text
extractedHTML = Mid(extractedHTML, iStart, iEnd - iStart)
'go to the URL
ie.Navigate extractedHTML
Set iedoc1 = ie.Document
'MsgBox iedoc1
On Error GoTo ErrHandler:
openedpage = iedoc1.getElementById("name").innerText
detailname = "NAME:" & vbCrLf & FirstName + " " + LastName
MsgBox ""
openedpage = ""
openedpage = iedoc1.getElementById("headline").innerText
'On Error GoTo ErrHandler:
MsgBox "LOCATION DETAILS:" & vbCrLf & openedpage
locationdetails = openedpage + vbCrLf
MsgBox locationdetails
openedpage = iedoc1.getElementById("profile-experience").innerText
profileexperience = openedpage + vbCrLf
openedpage = iedoc1.getElementById("profile-contact").innerText
profilecontact = openedpage + vbCrLf
openedpage = iedoc1.getElementById("overview").innerText
overview = openedpage + vbCrLf
openedpage = iedoc1.getElementById("skills-list").innerText
skillslist = openedpage + vbCrLf
openedpage = iedoc1.getElementById("profile-languages").innerText
profilelanguages = openedpage + vbCrLf
openedpage = iedoc1.getElementById("profile-education").innerText
profileeducation = openedpage + vbCrLf
openedpage = iedoc1.getElementById("pubgroups").innerText
publicgroups = openedpage + vbCrLf
returnstatement = locationdetails + profileexperience + profilecontact + overview + skillslist + profilelanguages + profileeducation + publicgroups
MsgBox returnstatement
ErrHandler:
openedpage = "NULL"
Resume Next
'****************************************
'End EDITS
'****************************************
Else
MsgBox "No linkedin profile found"
End If
Set RegEx = Nothing
Set ie = Nothing
End Sub
最奇怪的是當我註釋行號59時,我的位置詳細信息返回爲NULL。但是,如果我有該消息框,位置詳細信息將得到正確返回。我嘗試使用變量而不是消息框,但除了當我使用消息框時,位置細節對於所有場景都變爲NULL。
openedpage = iedoc1.getElementById("name").innerText
detailname = "NAME:" & vbCrLf & FirstName + " " + LastName
**MsgBox ""** (If i comment it out, location details becomes NULL. If it is uncommented, location details value is correct.
openedpage = ""
openedpage = iedoc1.getElementById("headline").innerText
'On Error GoTo ErrHandler:
**MsgBox "LOCATION DETAILS:" & vbCrLf & openedpage**
locationdetails = openedpage + vbCrLf
MsgBox locationdetails