Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XML到VBA Excel_Xml_Vba_Excel - Fatal编程技术网

XML到VBA Excel

XML到VBA Excel,xml,vba,excel,Xml,Vba,Excel,各位下午好 我试图通过此链接从xml文件中获取表数据,以便自动生成报告: 然而,我所做的一切都给了我错误 我必须输入登录名和密码,并访问此链接以获取xml,如下所示: <information> <reset>10/9/2014 8:24:07 AM</reset> <date>10/9/2014 10:49:45 AM</date> <header> <statheader id="0">Agent</

各位下午好

我试图通过此链接从xml文件中获取表数据,以便自动生成报告:

然而,我所做的一切都给了我错误

我必须输入登录名和密码,并访问此链接以获取xml,如下所示:

<information>
<reset>10/9/2014 8:24:07 AM</reset>
<date>10/9/2014 10:49:45 AM</date>
<header>
<statheader id="0">Agent</statheader>
<statheader id="6">Current online/offline time</statheader>
<statheader id="1">Login time (server local)</statheader>
<statheader id="5">Online status</statheader>
<statheader id="4">Times offline</statheader>
<statheader id="3">Total time offline</statheader>
<statheader id="2">Total time online</statheader>
</header>
<statistics>
<item id="41810">
<statistic id="0">A_G_Silva A_G_Silva</statistic>
<statistic id="6">01:20:43</statistic>
<statistic id="1">10/9/2014 9:29:02 AM</statistic>
<statistic id="5">Unavailable</statistic>
<statistic id="4">0</statistic>
<statistic id="3">00:00:00</statistic>
<statistic id="2">00:00:00</statistic>
</item>
<item id="40663">
<statistic id="0">Alan_Dominguez Alan_Dominguez</statistic>
<statistic id="6">02:21:00</statistic>
<statistic id="1">10/9/2014 8:28:45 AM</statistic>
<statistic id="5">Unavailable</statistic>
<statistic id="4">0</statistic>
<statistic id="3">00:00:00</statistic>
<statistic id="2">00:00:00</statistic>
</item>
</statistics>
</information>

2014年9月10日上午8:24:07
2014年10月9日上午10:49:45
代理人
当前在线/离线时间
登录时间(服务器本地)
在线状态
离线时报
脱机总时间
在线总时间
A_G_西尔瓦A_G_西尔瓦
01:20:43
2014年9月10日上午9:29:02
不可用的
0
00:00:00
00:00:00
艾伦·多明格斯艾伦·多明格斯
02:21:00
2014年9月10日上午8:28:45
不可用的
0
00:00:00
00:00:00
你知道我如何通过VBA实现这一点吗?
谢谢大家!

向VBA项目添加两个引用:

  • Microsoft Internet控件
  • Microsoft HTML对象库

  • 字段后面的HTML有一个
    id=password
    id=username
    ,这使得使用getElementById查找非常容易

    以下是填写字段和提交表单的工作示例:

    Sub PushAddPhotoButton()
        Dim IEexp As InternetExplorer
        Set IEexp = New InternetExplorer
        IEexp.Visible = True
        IEexp.navigate "http://channels9.us.dell.com/netagent/scripts/srvgate.dll?Action=10020&XMLType=A_OnlineOffline_Result"
    
        'Wait for page to load
        Do While IEexp.readyState <> 4: DoEvents: Loop
    
        'Fill in user name and password
        IEexp.Document.getElementById("username").Value = "myUsername"
        IEexp.Document.getElementById("password").Value = "myPassword"
    
        'Submit form
        IEexp.Document.getElementById("submit").Click
    
        'Wait for page to load again
        Do While IEexp.readyState <> 4: DoEvents: Loop
    
        'Do what you need to do here to get XML
        'If you don't do anything then the IE browser will just flicker and close
    
        IEexp.Quit
        Set IEexp = Nothing
    End Sub
    
    Sub-PushAddPhotoButton()
    Dim IEexp作为InternetExplorer
    Set IEexp=新的InternetExplorer
    IEexp.Visible=True
    IEexp.navigate“http://channels9.us.dell.com/netagent/scripts/srvgate.dll?Action=10020&XMLType=A_OnlineOffline_Result"
    '等待页面加载
    Do While IEexp.readyState 4:DoEvents:Loop
    '填写用户名和密码
    IEexp.Document.getElementById(“用户名”).Value=“myUsername”
    IEexp.Document.getElementById(“密码”).Value=“myPassword”
    “提交表格
    IEexp.Document.getElementById(“提交”)。单击
    '等待页面再次加载
    Do While IEexp.readyState 4:DoEvents:Loop
    '在这里执行获取XML所需的操作
    如果你什么都不做,IE浏览器就会闪烁并关闭
    退出
    设置IEexp=Nothing
    端接头
    

    下面是一个屏幕截图,脚本在单击按钮之前停止


    感谢您的快速回答!我收到一个错误:运行时错误“-2147417848(80010108)”:自动错误-调用的对象已与其客户端断开连接。Set userName=IEexp.Document.getElementById(“userName”)它已经用Low尝试过了,但我仍然收到相同的错误。好的,我验证了它也不安全。我注释掉了等待行(DoWhile),得到了相同的错误。页面加载速度变慢,找不到元素。您可以通过手动单步执行代码(F8)来测试这一点。确保网页在跨过用户名行之前已完全加载。如果有效,则验证这是一个计时问题。作为
    输入
    表单元素,用户名和密码应设置为
    .Value
    ,而不是
    .InnerText
    。e、 g.
    IEexp.Document.getElementById(“用户名”).Value=“myUsername”