0

学校が夏休みになり、自由な時間ができたので、個人用の書籍在庫システムを作成することにしました。調査中に、Jules によるこの投稿に出くわしました: ISBN -> bookdata Lookup to fill in database

以下のコードを実装しようとすると、最初Run-time error: Access is deniedSet 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")ます。

今受けてるRun-time error '91': Object variable or With block variable not set.

誰でもこれを解決する方法について何か考えがありますか? XML を扱うのは初めてです。テストのために、イミディエイト ウィンドウを使用してtestlookup("0007102968").

モジュール検索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

クラス モジュールの 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

編集:まだエラーが発生していますが、すぐにウィンドウに応答が表示されます:

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>

https リクエストのブラウザ結果:

4

1 に答える 1

1

VBA での XML は私にとって面倒な場合があるため、XML 応答テキストを取得して .xml ファイルにロードするだけMSXML2.DOMDocumentです。そこから、必要なデータを解析して取得できます。最初に、ファイルに Microsoft XML dll への参照があることを確認する必要があります (これを行うには、Visual Basic ウィンドウから [ツール] > [参照] を選択し、[Microsoft XML 6.0] を選択します)。

コードに関しては、次のようになります。

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

応答が 1 つしか得られないことが確実な場合は、for ループを取り除くことができます (その isbn を考えると、そうすべきだと思います...)

于 2013-06-20T18:46:15.380 に答える