1

我有一个使用“来自 Web 的数据”功能的宏。我已经登录了要从中提取数据的网站(在 Internet Explorer 中) - 但我在 Excel 中得到的结果只是一直告诉我我没有登录。

是否有通过 Excel 登录“来自 Web 的数据”的特殊方法?我知道它有效,因为我使用宏记录器来了解 Excel 如何获取数据 - 并且手动执行此操作,网站要求我登录“Excel IE 浏览器窗口”......但已经一个多小时了,所以我被注销。如何重新登录才能使用它?

如果有帮助,这是适用的数据提取代码(登录后 URL 工作正常):

 With ActiveSheet.QueryTables.Add(Connection:="URL;" & theURL, Destination:=webInfoWS.Range("$A$2"))
        .name = cel.Value & " hex"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlSpecifiedTables
        .WebFormatting = xlWebFormattingNone
        .WebTables = "3"
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
4

1 回答 1

0

同时,我找到了一个解决方法(主要来自这个线程):

Debug.Print "Opening " & theURL在之后和之前添加这个With ActiveSheet.QueryTables.Add(...)

    ''' Log in to the web stuff
    Set ie = CreateObject("InternetExplorer.Application")
    With ie
        .Visible = True
        .Navigate theURL
        Do Until .READYSTATE = 4
            DoEvents
        Loop
        If Left(.Document.Title, 5) <> "Welcome!" Then
            .Document.all.Item("username").Value = "My User Name"
            .Document.all.Item("password").Value = "MyPassword"
            .Document.forms(0).submit
        End If
      '   .Quit
    End With
    ''''''

这实际上是打开 IE 窗口,然后(自动)输入我的用户名和密码,然后提交。

但是,如果我再次运行宏(意味着我已经登录),这会给我一个错误,因为没有用户名/密码输入表单。

关于如何绕过的想法 - 使用On Error Goto Next,但我不喜欢使用它,但它可能是最好的选择。我想我会尝试获取窗口标题(通过 HTML)并检查这是否是登录表单......

编辑:关于如何知道.Item("____")is"username""password". 这只是来自 HTML 输入 ID 标记:

在此处输入图像描述

您会在我发现的帖子中注意到,其中的文本.Item()是不同的——我假设是因为 HTML ID 也不同。

编辑2:这不起作用!我可以登录,在IE中查看网页,但是当我到达 时.Refresh BackgroundQuery:=False,结果信息是说我需要登录的文字:/

于 2015-09-01T15:01:28.007 回答