VBA Access:XML,错误“91”:对象变量或未设置块变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17217166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:47:26  来源:igfitidea点击:

VBA Access: XML, Error '91': Object variable or With block variable not set?

xmlvbaxml-parsingms-access-2007access-vba

提问by Analytic Lunatic

Now that school is out for the summer and I have some free time I have chosen to create a personal book inventory system. While researching I came across this post by Jules: ISBN -> bookdata Lookup to fill in a database

现在学校放暑假了,我有一些空闲时间,我选择创建一个个人图书库存系统。在研究时,我发现了 Jules 的这篇文章:ISBN -> bookdata Lookup to fill in a database

When trying to implement my code below, I first got a Run-time error: Access is deniedwhen using Set xmlhttp = CreateObject("MSXML2.xmlhttp"). I found a post (http://social.msdn.microsoft.com/Forums/en-US/1abda1ce-e23c-4d0e-bccd-a323aa7f2ea5/access-is-denied-while-using-microsoftxmlhttp-to-get-a-url-link-in-vbscript-help) that said to change the line to Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.6.0").

在尝试实现下面的代码时,我首先Run-time error: Access is denied在使用Set xmlhttp = CreateObject("MSXML2.xmlhttp"). 我发现了一个帖子(http://social.msdn.microsoft.com/Forums/en-US/1abda1ce-e23c-4d0e-bccd-a323aa7f2ea5/access-is-denied-while-using-microsoftxmlhttp-to-get-a -url-link-in-vbscript-help) 表示将行更改为Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.6.0").

I now am receiving Run-time error '91': Object variable or With block variable not set.

我现在收到 Run-time error '91': Object variable or With block variable not set.

Anyone have any ideas on how to resolve this? I'm new to working with XML. For testing I am using the Immediate Window and typing testlookup("0007102968").

任何人都对如何解决这个问题有任何想法?我是 XML 的新手。为了进行测试,我正在使用立即窗口并输入testlookup("0007102968").

Module SearchISBN:

模块搜索ISBN:

Option Compare Database
Option Explicit

Public Function testlookup(value As String)
    Dim book
    Set book = New isbn
    book.Lookup (value)
    Debug.Print book.Title
    Debug.Print book.PublisherText
End Function

Class Module isbn:

类模块 isbn:

Option Compare Database
Option Explicit

'https://stackoverflow.com/questions/2454348/isbn-bookdata-lookup-to-fill-in-a-database
' AccessKeys created with account on ISBNDB.com
' Reference in (Tools->Refernces) made to "Microsoft XML"

Dim strTitle As String
Dim strAuthor As String
Dim strPublisher As String
Dim strSummary As String
Dim strPrice As Currency
Dim strISBN10 As Integer
Dim strISBN13 As Integer
Dim strNotes As String
'Dim strPersRating As String
Dim accessKey As String

Private Sub Class_Initialize()
    ' Set AccessKey value of ISBNDB API
    accessKey = "NSOY388Z"
End Sub
Property Get Title() As String
    Title = strTitle
End Property
Property Get Author() As String
    Author = strAuthor
End Property
Property Get Publisher() As String
    Publisher = strPublisher
End Property
Property Get Summary() As String
    Summary = strSummary
End Property
Property Get Price() As Currency
    Price = strPrice
End Property
Property Get ISBN10() As Integer
    ISBN10 = strISBN10
End Property
Property Get ISBN13() As Integer
    ISBN13 = strISBN13
End Property
Property Get Notes() As String
    Notes = strNotes
End Property

Public Function Lookup(isbn As String) As Boolean
    Lookup = False
'    Dim xmlhttp
'    Dim strTest As String
'    strTest = "https://isbndb.com/api/books.xml?access_key=" & accessKey & "&results=texts&index1=isbn&value1=" & isbn
'    Debug.Print strTest
'    'Run-time error, access is denied
'    ' Set xmlhttp = CreateObject("MSXML2.xmlhttp")
'    Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
'    xmlhttp.Open "Get", strTest, False '"https://isbndb.com/api/books.xml?access_key=" & accessKey & "&results=texts&index1=isbn&value1=" & isbn, False
'    xmlhttp.send
'    Debug.Print xmlhttp.responseText
'    Debug.Print "Response: " & xmlhttp.responseXML.XML '
'    Dim xmldoc
''    Set xmldoc = CreateObject("Microsoft.XMLDOM")
''    xmldoc.loadXML (xmlhttp.responseXML.XML)
''    ERROR
''    If (xmldoc.selectSingleNode("//BookList").getAttribute("total_results") = 0) Then
''        MsgBox "Invalid ISBN or not in database"
''        Exit Function
''    End If
''    If (xmldoc.selectSingleNode("//BookList").getAttribute("total_results") > 1) Then
''        MsgBox "Caution, got more than one result!"
''        Exit Function
''    End If
'
'    Set xmldoc = New DOMDocument
'    xmldoc.loadXML (xmlhttp.responseText)
'
'
'
'    strTitle = xmldoc.selectSingleNode("//BookData/TitleLong").Text
'    strAuthor = xmldoc.selectSingleNode("//BookData/AuthorsText").Text
'    strPublisher = xmldoc.selectSingleNode("//BookData/PublisherText").Text
'    strNotes = xmldoc.selectSingleNode("//BookData/Notes").Text
'    strSummary = xmldoc.selectSingleNode("//BookData/Summary").Text
'

    Dim xmlhttp As MSXML2.xmlhttp
    Dim xmldoc As MSXML2.DOMDocument
    Dim XMLNodes As MSXML2.IXMLDOMNodeList
    Dim xmlElement As MSXML2.IXMLDOMElement
    Dim bookTitle As String
    Dim myErr As MSXML2.IXMLDOMParseError

    Dim strTest As String
    strTest = "https://isbndb.com/api/books.xml?access_key=" & accessKey & "&results=texts&index1=isbn&value1=" & isbn

    ' Fetch the XML - THIS IS WHERE I AM NOW GETTING ERROR
    xmlhttp.Open "Get", strTest, False '"https://isbndb.com/api/books.xml?access_key=" & accessKey & "&results=texts&index1=isbn&value1=" & isbn, False
    xmlhttp.send

    Set xmldoc = New DOMDocument
    xmldoc.loadXML (xmlhttp.responseText)

    Set XMLNodes = xmldoc.getElementsByTagName("BookData")

    Dim i As Integer

    ' Get the data
    For i = 1 To XMLNodes.length
        Set xmlElement = XMLNodes.nextNode
        bookTitle = xmlElement.getElementsByTagName("Title").Item(0).nodeTypedValue
    Next

    Lookup = True

End Function

EDIT: Still getting the error, but am now getting a response in the immediate window:

编辑:仍然收到错误,但现在在即时窗口中收到响应:

https://isbndb.com/api/books.xml?access_key=NSOY388Z&results=texts&index1=isbn&value1=0007102968
Response: 
<?xml version="1.0" encoding="UTF-8"?>

<ISBNdb server_time="2013-06-20T16:20:00Z">
<BookList total_results="1" page_size="10" page_number="1" shown_results="1">
<BookData book_id="the_times_book_of_quotations" isbn="0007102968" isbn13="9780007102969">
<Title>The Times book of quotations</Title>
<TitleLong></TitleLong>
<AuthorsText></AuthorsText>
<PublisherText publisher_id="times_books">[Glasgow] : Times Books : 2000.</PublisherText>
<Summary></Summary>
<Notes>Includes index.</Notes>
<UrlsText></UrlsText>
<AwardsText></AwardsText>
</BookData>
</BookList>
</ISBNdb>

Browser results of https request:

https请求的浏览器结果:

回答by Jaycal

Since XML in VBA can sometimes be a pain for me, I just take the XML response text and then load it into a MSXML2.DOMDocument. From there you can parse and get the data you need. First you need to make sure that the file has a reference to Microsoft XML dll (do this by selecting Tool > References from the Visual Basic window and then selecting "Microsoft XML 6.0").

由于 VBA 中的 XML 有时对我来说可能很痛苦,我只是获取 XML 响应文本,然后将其加载到MSXML2.DOMDocument. 从那里您可以解析并获取您需要的数据。首先,您需要确保该文件具有对 Microsoft XML dll 的引用(通过从 Visual Basic 窗口中选择工具 > 引用,然后选择“Microsoft XML 6.0”来执行此操作)。

As for the code, it would look something like this:

至于代码,它看起来像这样:

Dim xmlhttp As MSXML2.xmlhttp
Dim xmldoc As MSXML2.DOMDocument
Dim XMLNodes As MSXML2.IXMLDOMNodeList
Dim xmlElement As MSXML2.IXMLDOMElement
Dim bookTitle as String

' Fetch the XML
Set xmlhttp = CreateObject("Microsoft.xmlHTTP")
xmlhttp.Open "Get", yourURL, False
xmlhttp.send

' Create a new XMLDocument to which to load the XML text
Set xmlDoc = New DOMDocument
xmldoc.LoadXML (xmlhttp.responseText)

' Get all of the BookData Nodes and fetch the first node
Set XMLNodes = xmldoc.getElementsByTagName("BookData")

' Get your data (retrieved only the Title as an example)
for i = 1 to XMLNodes.length
    Set myElement = XMLNodes.NextNode
    bookTitle = myElement.getElementsByTagName("Title").Item(0).nodeTypedValue
Next

You can get rid of the for loop if you are sure that you'll only get one response (given that its isbn i guess you should... )

如果你确定你只会得到一个响应,你可以摆脱 for 循环(鉴于它的 isbn 我猜你应该......)