1

我正在使用 excel VBA 打开谷歌结果的第一个返回页面。从第一页开始,我根据元素 ID 操作数据。在执行此过程时,我遇到了一个非常奇怪的行为。

让我简要概述一下我正在尝试做的事情。

我将在用户表单中输入名字和姓氏。对于给定的名字和姓氏,我将搜索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
4

2 回答 2

0

我认为您的宏需要一些额外的时间来加载所有(或一些额外的)数据。如果你有你的 MsgBox,你无意中在你找到并关闭你的 MsgBox 之前给了这个过程一些时间。

如果您放置其他类型的“等待”点(例如Application.Waitor Do...Loop)而不是 MsgBox 会怎样。请让我们知道结果。

于 2013-03-15T07:17:46.220 回答
0

Internet Explorer 需要一些时间来加载和呈现页面。添加一行

Do While ie.Busy : WScript.Sleep 100 : Loop

指令后ie.Navigate extractedHTML。加载第一页时,您已经在做类似的事情:

ie.Navigate "http://www.google.com/search?hl=en&q=" & FirstName _
  & "+" & LastName & "+linkedin&meta="
Do Until ie.ReadyState = READYSTATE_COMPLETE
Loop

尽管您也应该在那里添加睡眠指令。

于 2013-03-15T12:03:57.990 回答