VBA:无法在< input>中提取值使用getelementsbyTagname()标记 [英] VBA: not able to pull value within <input> tag using getelementsbyTagname().Value

查看:210
本文介绍了VBA:无法在< input>中提取值使用getelementsbyTagname()标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在stackflow上的第一篇文章:)我已经使用谷歌搜索VBA知识并编写了大约一个月的一些VBA.

This is my first post on stackflow :) I've been Googling VBA knowledge and writing some VBA for about a month.

我的计算机信息:

1.window 8.1

1.window 8.1

2.excel 2013

2.excel 2013

3.ie 11

我的Excel参考

Microsoft对象库:是

Microsoft Object Library: yes

Microsoft Internet Controls:是

Microsoft Internet Controls: yes

Microsoft Form 2.0对象库:是

Microsoft Form 2.0 Object library: yes

Microsoft Script Control 1.0:是

Microsoft Script Control 1.0: yes

问题:

我试图使用VBA从Internet Explorer自动检索数据. 我想从一个名为"u_0_1"的ID(位于一个名为"facebook"的ID下)检索输入标签中的值.我期望在单元格c2上检索值"AQFFmT0qn1TW".但是,在运行VBA运行时错误'91':object变量或未设置块变量的情况下,它弹出了此味精. 我已经使用诸如

I was trying to retrieve data from internet explorer automatically using VBA. I would like to retrieve the value within an input tag from a id called "u_0_1" which is under a id called "facebook". I am expecting to retrieve the value "AQFFmT0qn1TW" on cell c2. However, it got this msg popped up after I run the VBA "run-time error '91':object variable or with block variable not set. I have been trying this for a couple of weeks using different methods such as,

1.getelementsbyClassname

1.getelementsbyClassname

2.getelementbyid

2.getelementbyid

3.getelementsbyTagname

3.getelementsbyTagname

但这根本不起作用.

URL:

http://coursesweb.net/javascript/getelementsbytagname

下面是我的VBA代码.你们能帮我一点忙吗?

Below is my VBA code. Could you guys help me out a little bit please?

Private Sub CommandButton1_Click()
Dim ie As Object
Dim Doc As HTMLDocument
Dim getThis As String



Set ie = CreateObject("InternetExplorer.Application")

    ie.Visible = 0


    ie.navigate "http://coursesweb.net/javascript/getelementsbytagname"
    Do
    DoEvents
    Loop Until ie.readyState = 4



    Set Doc = ie.document


    getThis = Trim(Doc.getElementById("u_0_1")(0).getElementsByTagName("input")(0).Value)
    Range("c2").Value = getThis
End Sub            

推荐答案

感谢您的帮助.我不知道JS和VBA在getelementsby()方法方面有区别.并使用循环方法查找ID,我发现它也非常有用.

Thanks for your help. I have no idea that there is difference between JS and VBA in aspect of getelementsby () methods. And using the loop method to find the id which I find it very useful as well.

我仍然有一些问题要从表单或输入类型中检索值.希望您能对我有所帮助,也可以给我一些建议.

I still have some issues to retrieve value from a form or input type. I hope that you could help me or give me some suggestions as well.

预期结果:

获取值"AQFFmT0qn1TW",并将其自动复制到单元格("c2")上.

retrieve the value "AQFFmT0qn1TW" and copy it on Cell ("c2") automatically.

实际结果:

没有返回到单元格("C2")

nothing return to Cell ("C2")

下面是HTML元素.

<form rel="async" ajaxify="/plugins/like/connect" method="post" action="/plugins/like/connect" onsubmit="return window.Event &amp;&amp; Event.__inlineSubmit &amp;&amp; Event.__inlineSubmit(this,event)" id="u_0_1">
<input type="hidden" name="fb_dtsg" value="AQFFmT0qn1TW" autocomplete="off">         

以下是基于您的代码的VBA代码.

Private Sub CommandButton1_Click()
Dim ie As Object
Dim Doc As HTMLDocument
Dim Elements As IHTMLElementCollection
Dim Element As IHTMLElement

Set ie = CreateObject("InternetExplorer.Application")

ie.Visible = 0

ie.navigate "http://coursesweb.net/javascript/getelementsbytagname"
Do
DoEvents
Loop Until ie.readyState = 4

Set Doc = ie.document

Set Elements = Doc.getElementsByTagName("input")

For Each Element In Elements
    If Element.name = "fb_dtsg" Then
        Range("c2").Value = Element.innerText
    End If
Next Element

Set Elements = Nothing

End Sub

干杯.

这篇关于VBA:无法在&lt; input&gt;中提取值使用getelementsbyTagname()标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆