Excel VBA:如何从Internet Explorer复制一个字段的值

我试图访问Internet Explorer上的表单,然后复制一些字段(我知道那里的名称和ID)的值,并将这些值复制到Excel工作表中。 这是我试图做的简化版本。 所以现在我可以打开我的网页,然后打开包含在其中的子窗体。 但是当我复制我感兴趣的字段的值时,我无法设置variables,因此我可以将其复制回Excel。
这是我的代码如下。 我的问题之间的意见。

Private Sub Open_multiple_sub_pages_from_main_page() Dim i As Long Dim IE As Object Dim Doc As Object Dim objElement As Object Dim objCollection As Object ' Create InternetExplorer Object Set IE = CreateObject("InternetExplorer.Application") ' You can uncoment Next line To see form results IE.Visible = True ' Send the form data To URL As POST binary request IE.navigate "http://webpage.com/" ' Wait while IE loading... While IE.Busy DoEvents Wend Set objCollection = IE.Document.getElementsByTagName("input") i = 0 While i < objCollection.Length If objCollection(i).Name = "txtUserName" Then ' Set text for search objCollection(i).Value = "1234" End If If objCollection(i).Name = "txtPwd" Then ' Set text for search objCollection(i).Value = "password" End If If objCollection(i).Type = "submit" And objCollection(i).Name = "btnSubmit" Then ' submit button if found and set Set objElement = objCollection(i) End If i = i + 1 Wend objElement.Click ' click button to load page ' Wait while IE re-loading... While IE.Busy DoEvents Wend ' Show IE IE.Visible = True Set Doc = IE.Document Dim links, link, value_to_copy Dim j As Integer 'variable to count items j = 0 Set links = IE.Document.getelementbyId("dgTime").getElementsByTagName("a") n = links.Length While j <= n 'loop to go thru all "a" item so it loads next page links(j).Click While IE.Busy DoEvents Wend '------------THE PROBLEM IS HERE--------------------------- Set value_to_copy = IE.Document.getelementbyId("mainTable").getElementsByTagName("txtProject").innerText '----------VALUE_TO_COPY WILL REMAIN AT "" VALUE IE.Document.getelementbyId("DetailToolbar1_lnkBtnSave").Click 'save Do While IE.Busy Application.Wait DateAdd("s", 1, Now) 'wait Loop IE.Document.getelementbyId("DetailToolbar1_lnkBtnCancel").Click 'close Do While IE.Busy Application.Wait DateAdd("s", 1, Now) 'wait Loop Set links = IE.Document.getelementbyId("dgTime").getElementsByTagName("a") j = j + 2 Wend End Sub 

以下是我想要检索“txtProject”值的页面的HTML代码。 在特定情况下,值是“0000001”。 这是我需要复制的价值。

 <table width="100%" class="Form" id="mainTable" border="0" cellspacing="0" cellpadding="0"> <tbody><tr id="TRCustomer"> <td class="titleLabel"><span id="lblCustomer"> <u>C</u>lient : </span></td> <td><input name="txtCustomer" id="txtCustomer" accesskey="C" language="javascript" onchange="__doPostBack('txtCustomer','')" type="text"></td> <td><a class="Button" id="lnkBtnCustomer" href="javascript:__doPostBack('lnkBtnCustomer','')"> <img id="imgCustomer" alt="" src="images/toolbar/b_preview.gif" border="0"></a></td> <td class="tdDescriptionLabel"><span class="DescriptionLabel" id="lblCustomerDescription">&nbsp;</span></td> </tr> <tr id="TRProject"> <td width="110" class="titleLabel"> <span id="lblProject">Pro<u>j</u>et : </span></td> <td width="152"><input name="txtProject" tabindex="2" id="txtProject" accesskey="J" language="javascript" onkeypress="return LookupButton(event,'lnkBtnProject')" onchange="__doPostBack('txtProject','')" type="text" value="0000001"></td> <td width="20"><a class="Button" id="lnkBtnProject" href="javascript:__doPostBack('lnkBtnProject','')"> <img id="imgProject" alt="" src="images/toolbar/b_preview.gif" border="0"></a></td><td class="tdDescriptionLabel"> <span class="DescriptionLabel" 

任何帮助将不胜感激。
Tx提前。

在一个文件的id应该是唯一的,所以你可以直接使用:

 value_to_copy = IE.Document.getelementbyId("txtProject").Value 

getElementsByTagName()不用于通过id来定位元素,并且在任何情况下返回一个匹配集合 ,而不是单个元素,所以你不能指定它的返回值。 这将是你通常使用它的方式:

 value_to_copy = IE.Document.getelementsbyTagName("input")(0).Value 

Set关键字用于对象,这里有一个variables。 删除从赋值语句中Setvalue_to_copy = IE.Document.getelementbyId("mainTable").getElementsByTagName("txtProject").innerText

innerText属性返回一个Stringtypes。 这意味着,您不需要使用关键字Set ,而是将variables设置为指向特定对象的指针。 string是本地types,所以不要使用关键字Set。 试试这个:

 '------------THE PROBLEM IS HERE--------------------------- value_to_copy = IE.Document.getelementbyId("mainTable").getElementsByTagName("txtProject").innerText '----------VALUE_TO_COPY WILL REMAIN AT "" VALUE 

这是一个微妙的问题。 简而言之,您不应该使用innertext属性。 而是要检索由txtProject elementchildNodes属性返回的“value” node值。

您正在检索的数据实际上并不是“innertext”。 Innertext将是在开始和结束标签之间出现的文本。 从MSDN https://msdn.microsoft.com/en-us/library/ie/ms533899(v=vs.85).aspx :

The innerText property is valid for block elements only. By definition, elements that do not have both an opening and closing tag cannot have an innerText property.

例如,在以下片段中:

 <h1>Header Text</h1> 

<h1>标签的内文是“标题文本”

在你的代码片段中,标签实际上没有任何内部文本。 你有一个带有属性的td元素,但是<td...></td>之间没有任何关系:

 <td width="152"><input name="txtProject" tabindex="2" id="txtProject" accesskey="J" language="javascript" onkeypress="return LookupButton(event,'lnkBtnProject')" onchange="__doPostBack('txtProject','')" type="text" value="0000001"></td>' 

这就是为什么innertext属性失败。 你的片段getElementsByTagName("txtProject")返回一个Element对象。 元素是nodes集合。 您的元素具有名为nametabindexidaccesskeylanguageonkeypressonchangetypevalue节点。 每个节点都有一个与之相关的值。 您需要访问value节点并查询其值。

我怀疑下面的工作,或者类似的东西。

 set txtProject = IE.Document.getelementbyID("mainTable").getElementsByTagName("txtProject") ' returns a `<td..></td>` element value_to_copy = txtProject.childNodes("value") 'Should return the default property of the node "value". 'The default property should be the string "00000001". 'But I'm not sure so you might want to do more 'research on `childNodes` and `nodes` 

或者可选地,但不那么直观:

 value_to_copy = IE.Document.getelementbyID("mainTable") _ .getElementsByTagName("txtProject") _ .childNodes("value") 

我从https://msdn.microsoft.com/en-us/library/ms757053(v=vs.85).aspx下面的代码片段中调整了这一点。 所以如果我上面的build议不起作用,也许这会有所帮助。

The following script example uses the childNodes property (collection) to return an IXMLDOMNodeList, and then iterates through the collection, displaying the value of each item's xml property.

 root = xmlDoc.documentElement; oNodeList = root.childNodes; for (var i=0; i<oNodeList.length; i++) { Item = oNodeList.item(i); WScript.Echo(Item.xml); }