【问题标题】:Excel Web Query Object and Cookies: Is there a better way?Excel Web 查询对象和 Cookie:有更好的方法吗?
【发布时间】:2013-10-12 05:14:22
【问题描述】:

我有一个 HTML 网页正在工作,我想从表中查询数据到 excel 2007。这个网页需要我使用密码登录。我使用普通的 IE7 浏览器登录,然后转到 DATA -> 连接 -> 我的连接并编辑查询。这会读取 IE7 cookie 缓存,当它显示“Web 查询未返回数据”时,我通过单击“重试”重新发布数据以连接到服务器的安全性。完成此操作后,数据导入正常。

我可以做到这一点,而且只需要每天做一次。我的应用程序的其他用户发现这很困难,这导致了我的问题:

有没有办法用 VB 自动 POST 这个数据?我在想也许我应该使用 IE.Document.cookie 的 cookie 属性?

【问题讨论】:

    标签: excel vba salesforce single-sign-on excel-web-query


    【解决方案1】:

    在继续进行 Web 查询(设置对 XML 库的引用)之前,我正在调用以下登录脚本。环顾四周,找到一些说明如何找到 POST 参数。

    Sub XMLHttpLogin()
    
        Dim i As Integer
        Dim sExpr As String
        Dim sPar As String, sURL as String
        Dim sResp As String
        Dim XMLHttp As MSXML2.XMLHTTP60
    
        Set XMLHttp = New MSXML2.XMLHTTP60
    
        sPar = "name=user1&pass=pass1&form_id=form1" 'The parameters to send.
        sURL = "http://www.stackoverflow.com"
    
        With XMLHttp
            .Open "POST", sURL, True 'Needs asynchronous connection
            .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
            .send (sPar)
    
            i = 0 'wait until data has been downloaded
            Do While i = 0
                If .readyState = 4 Then
                    If .Status = 200 Then Exit Do
                End If
                DoEvents
            Loop
    
            sResp = .responseText 'contains source code of website
            sExpr = "not-logged-in" 'look for this string in source code
    
            If InStr(1, sResp, sExpr, vbTextCompare) Then
                MsgBox "Not logged in. Error in XMLHttpLogin"
            End If
        End With
    End Sub
    

    【讨论】:

    • 感谢您的帮助!它确实帮助我了解了您对 XML HTTP 库的想法。我还没有掌握它的窍门,但我可以说它非常强大。比 IE 使用 DOM 从网页上抓取数据要好得多。
    猜你喜欢
    • 2011-06-07
    • 1970-01-01
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    • 2012-05-16
    • 2023-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多