Book list - getting book details from amazon using Excel VBA barcode lookups

后端 未结 4 1098
忘了有多久
忘了有多久 2020-12-13 16:28

I have a barcode reader and bunch of books. For each of the books, I want to list the book name and the author in an Excel spreadsheet.

My view is that some VBA cod

4条回答
  •  心在旅途
    2020-12-13 16:40

    I just found this thread as I was attempting to do the same thing. Unfortunately I'm on a MAC, so these answers don't help. With a bit of research I was able to do get it to work in MAC Excel with this module:

    Option Explicit
    
    ' execShell() function courtesy of Robert Knight via StackOverflow
    ' http://stackoverflow.com/questions/6136798/vba-shell-function-in-office-    2011-for-mac
    
    Private Declare Function popen Lib "libc.dylib" (ByVal command As String,       ByVal mode As String) As Long
    Private Declare Function pclose Lib "libc.dylib" (ByVal file As Long) As Long
    Private Declare Function fread Lib "libc.dylib" (ByVal outStr As String, ByVal size As Long, ByVal items As Long, ByVal stream As Long) As Long
    Private Declare Function feof Lib "libc.dylib" (ByVal file As Long) As Long
    
    Function execShell(command As String, Optional ByRef exitCode As Long) As String
        Dim file As Long
        file = popen(command, "r")
    
        If file = 0 Then
            Exit Function
        End If
    
        While feof(file) = 0
            Dim chunk As String
            Dim read As Long
            chunk = Space(50)
            read = fread(chunk, 1, Len(chunk) - 1, file)
            If read > 0 Then
                chunk = Left$(chunk, read)
                execShell = execShell & chunk
            End If
        Wend
    
        exitCode = pclose(file)
    End Function
    
    Function HTTPGet(sUrl As String) As String
    
        Dim sCmd As String
        Dim sResult As String
        Dim lExitCode As Long
        Dim sQuery As String
    
        sQuery = "method=getMetadata&format=xml&fl=*"
        sCmd = "curl --get -d """ & sQuery & """" & " " & sUrl
        sCmd = "curl --get -d """ & sQuery & """" & " " & sUrl
    
        sResult = execShell(sCmd, lExitCode)
    
        ' ToDo check lExitCode
    
        HTTPGet = sResult
    
    End Function
    
    Function getISBNData(isbn As String) As String
      Dim sUrl As String
      sUrl = "http://xisbn.worldcat.org/webservices/xid/isbn/" & isbn
      getISBNData = HTTPGet(sUrl)
    
    End Function
    
    
    
    Function getAttributeForISBN(isbn As String, info As String) As String
      Dim data As String
      Dim start As Integer
      Dim finish As Integer
    
    
     data = getISBNData(isbn)
     start = InStr(data, info) + Len(info) + 2
     finish = InStr(start, data, """")
     getAttributeForISBN = Mid(data, start, finish - start)
    
    
    End Function
    

    This is not all my original work, I pasted it together from another site, then did my own work. Now you can do things like:

    getAttributeForISBN("1568812019","title")

    This will return the title of that book. Of course you can apply this formula to all of the ISBNs in column A to look up multiple titles, or authors, or whatever.

    Hopefully this helps someone else out there!

提交回复
热议问题