2014-06-30 91 views
0

我试图自动化的过程,我必须查询网站:http://rgl.faa.gov/Regulatory_and_Guidance_Library/rgAD.nsf/MainFrame?OpenFramesetVBA IE自动化

有一个输入文本字段<input name="query size="20"/>",我想填充,但我在努力这样做。目前我正在测试我的代码,看看我是否可以引用标签。

Sub fill() 
Dim IE As Object 
Set IE = CreateObject("InternetExplorer.Application") 

IE.navigate "http://rgl.faa.gov/Regulatory_and_Guidance_Library/rgAD.nsf/MainFrame?OpenFrameset" 
IE.Visible = True 
While IE.busy 
DoEvents 
Wend 

For Each it In IE.Document.getElementsByTagName("input") 
      If it.Name = "newquery" Then 
       MsgBox ("yup") 
      End If 
     Next 
End Sub 

我想我的问题是,在输入栏处于2个框架集和框架......

任何想法,如果这甚至有可能吗?

回答

4

除非你确切地知道哪些帧输入是,这是一个递归搜索一个经典情况:

Sub fill() 
    Dim IE As Object, elem As Object 
    Set IE = CreateObject("InternetExplorer.Application") 

    IE.navigate "http://rgl.faa.gov/Regulatory_and_Guidance_Library/rgAD.nsf/MainFrame?OpenFrameset" 
    IE.Visible = True 

    While IE.Busy: DoEvents: Wend 
    While IE.document.readyState <> "complete": DoEvents: Wend 

    Set elem = FindInputByName(IE.document, "newquery") 
    If Not elem Is Nothing Then 
    elem.Value = "It works!" 
    End If 
End Sub 

Function FindInputByName(document As Object, name As String) As Object 
    Dim i As Integer, subdocument As Object, elem As Variant 
    Set FindInputByName = Nothing 

    For i = 0 To document.frames.Length - 1 
    Set subdocument = document.frames.Item(i).document 
    Set FindInputByName = FindInputByName(subdocument, name) 
    If Not FindInputByName Is Nothing Then Exit Function 
    Next i 

    For Each elem In document.getElementsByTagName("INPUT") 
    If elem.name = name Then 
     Set FindInputByName = elem 
     Exit Function 
    End If 
    Next elem 
End Function 
+0

我试图运行的代码,但我遇到了它甾体抗炎药是“对象不支持此属性或一些错误方法。”调试时突出显示函数中的区域For Each frame In document.frames .. – ltsai

+0

显然,frames'集合不允许'For Each'。我已经用一个常规的For'循环代替了,现在它似乎工作。另外请注意,我已经更改为'while IE.document.readyState <>“complete”' - 我认为*比轮询IE.Busy'好,但我不完全确定。因人而异。 – Tomalak

+0

我不知道我在做什么错,但我复制并粘贴你的代码,并收到一个新的错误“ByRef参数类型不匹配”。我试图改变它的ByVal,仍然没有按预期工作。 – ltsai

0

你应该尝试安装和使用opentwebst库:http://codecentrix.com/download.html

不要忘记在项目引用中添加opentwebst。

我花了更少的时间来产生下面然后写这篇文章的代码:

Sub OpenTwebstMacro 
    Dim core As ICore 
    Set core = New OpenTwebstLib.core 

    Dim browser As IBrowser 
    Set browser = core.StartBrowser("http://rgl.faa.gov/Regulatory_and_Guidance_Library/rgAD.nsf/MainFrame?OpenFrameset") 

    Call browser.FindElement("input text", "name=newquery").InputText("YOUR QUERY") 
    Call browser.FindElement("input button", "uiname=Go").Click 
End Sub