首页 > 解决方案 > 无法摆脱 vba 中的“旧格式或无效类型库”错误

问题描述

我已经在 vba 中编写了一个脚本来从网页中获取一些集合名称,并且脚本会相应地获取它们,直到它在执行中的某个地方捕获错误。这是我第一次遇到这样的错误。

我的脚本正在做的是获取下面的所有链接Company Sets,然后跟踪它深入一层的每个链接,然后跟踪它下面的所有链接Set Name再深入一层,最后从那里解析表格。随着脚本越来越大,我解析了PUBLISHED SET存储在变量而不是表中的名称。bName我使用 IE 来获取,PUBLISHED SET因为导致编码问题的线索很少。

我搜索了所有地方以找到任何解决方法,但没有运气。

但是,我遇到了这个线程,其中有一个用 vb 编写的建议解决方案,但不知道如何使它在 vba 中工作。

我正在尝试的脚本:

Sub FetchRecords()
    Const baseUrl$ = "https://www.psacard.com"
    Const link = "https://www.psacard.com/psasetregistry/baseball/company-sets/16"
    Dim IE As New InternetExplorer, Htmldoc As HTMLDocument
    Dim Http As New XMLHTTP60, Html As New HTMLDocument, bName$, tRow As Object
    Dim post As Object, elem As Object, posts As Object, I&, R&, C&
    Dim key As Variant
    Dim idic As Object: Set idic = CreateObject("Scripting.Dictionary")

    With Http
        .Open "GET", link, False
        .send
        Html.body.innerHTML = .responseText
    End With

    Set posts = Html.querySelectorAll(".dataTable tr td a[href*='/psasetregistry/baseball/company-sets/']")
    For I = 0 To posts.Length - 7
        idic(baseUrl & Split(posts(I).getAttribute("href"), "about:")(1)) = 1
    Next I

    For Each key In idic.Keys
        With Http
            .Open "GET", key, False
            .send
            Html.body.innerHTML = .responseText
        End With

        For Each post In Html.getElementsByTagName("a")
            If InStr(post.getAttribute("title"), "Contact User") > 0 Then
                If InStr(post.ParentNode.getElementsByTagName("a")(0).getAttribute("href"), "publishedset") > 0 Then
                    IE.Visible = True
                    IE.navigate baseUrl & Split(post.ParentNode.getElementsByTagName("a")(0).getAttribute("href"), "about:")(1)
                    While IE.Busy = True Or IE.readyState < 4: DoEvents: Wend
                    Set Htmldoc = IE.document
                    bName = Htmldoc.querySelector("h1 b.text-primary").innerText
                    If InStr(bName, "/") > 0 Then bName = Split(Htmldoc.querySelector(".inline-block a[href*='/contactuser/']").innerText, " ")(1)
                    R = R + 1: Cells(R, 1) = bName
                End If
            End If
        Next post
    Next key
    IE.Quit
End Sub

提取 70 到 90 之间的记录后,我得到指向以下行的错误:

bName = Htmldoc.querySelector("h1 b.text-primary").innerText

错误看起来像:

Automation Error: old format or invalid type library

在用vb编写的链接线程中提出的解决方案(无法转换为vba):

'save the current settings for easier restoration later
Dim oldCI As System.Globalization.CultureInfo = _
System.Threading.Thread.CurrentThread.CurrentCulture

'change the settings
System.Threading.Thread.CurrentThread.CurrentCulture = _
New System.Globalization.CultureInfo("en-US")

Your code here

'restore the previous settings
System.Threading.Thread.CurrentThread.CurrentCulture = oldCI

标签: vbavb.netweb-scrapingxmlhttprequestinternet-explorer-11

解决方案


推荐阅读