Guest User

pdftables-vba--2015-07-07

a guest
Jul 7th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '--- https://support.microsoft.com/en-us/kb/195763
  2. '    NB: remove PtrSafe if old Excel
  3. Private Declare PtrSafe Function GetTempPath Lib "kernel32" _
  4.          Alias "GetTempPathA" (ByVal nBufferLength As Long, _
  5.          ByVal lpBuffer As String) As Long
  6.  
  7. '--- https://support.microsoft.com/en-us/kb/195763
  8. '    NB: remove PtrSafe if old Excel
  9. Private Declare PtrSafe Function GetTempFileName Lib "kernel32" _
  10.          Alias "GetTempFileNameA" (ByVal lpszPath As String, _
  11.          ByVal lpPrefixString As String, ByVal wUnique As Long, _
  12.          ByVal lpTempFileName As String) As Long
  13.          
  14. Private Function CreateTempFile(sPrefix As String) As String
  15.     '--- https://support.microsoft.com/en-us/kb/195763
  16.    '    Generate the name of a temporary file
  17.         Dim sTmpPath As String * 512
  18.          Dim sTmpName As String * 576
  19.          Dim nRet As Long
  20.  
  21.          nRet = GetTempPath(512, sTmpPath)
  22.          If (nRet > 0 And nRet < 512) Then
  23.             nRet = GetTempFileName(sTmpPath, sPrefix, 0, sTmpName)
  24.             If nRet <> 0 Then
  25.                CreateTempFile = Left$(sTmpName, _
  26.                   InStr(sTmpName, vbNullChar) - 1)
  27.             End If
  28.          End If
  29. End Function
  30.  
  31. Private Function pvToByteArray(sText As String) As Byte()
  32.     '--- http://tinyurl.com/vbapost
  33.    pvToByteArray = StrConv(sText, vbFromUnicode)
  34. End Function
  35.  
  36. Private Function pvPostFile(sUrl As String, sFileName As String, Optional ByVal bAsync As Boolean) As Variant
  37.     '--- HTTP POST a file as multipart
  38.    '--- http://tinyurl.com/vbapost -- modified slightly
  39.    Const STR_BOUNDARY  As String = "3fbd04f5Rb1edX4060q99b9Nfca7ff59c113"
  40.     Dim nFile           As Integer
  41.     Dim baBuffer()      As Byte
  42.     Dim sPostData       As String
  43.  
  44.     '--- read file
  45.    nFile = FreeFile
  46.     Open sFileName For Binary Access Read As nFile
  47.     If LOF(nFile) > 0 Then
  48.         ReDim baBuffer(0 To LOF(nFile) - 1) As Byte
  49.         Get nFile, , baBuffer
  50.         sPostData = StrConv(baBuffer, vbUnicode)
  51.     End If
  52.     Close nFile
  53.     '--- prepare body
  54.    sPostData = "--" & STR_BOUNDARY & vbCrLf & _
  55.         "Content-Disposition: form-data; name=""uploadfile""; filename=""" & Mid$(sFileName, InStrRev(sFileName, "\") + 1) & """" & vbCrLf & _
  56.         "Content-Type: application/octet-stream" & vbCrLf & vbCrLf & _
  57.         sPostData & vbCrLf & _
  58.         "--" & STR_BOUNDARY & "--"
  59.     '--- post
  60.    With CreateObject("Microsoft.XMLHTTP")
  61.         .Open "POST", sUrl, bAsync
  62.         .SetRequestHeader "Content-Type", "multipart/form-data; boundary=" & STR_BOUNDARY
  63.         .Send pvToByteArray(sPostData)
  64.         If Not bAsync Then
  65.             pvPostFile = .ResponseBody
  66.         End If
  67.     End With
  68. End Function
  69.  
  70.  
  71.  
  72. Private Sub pdftables_worker(filename As String)
  73.     data = pvPostFile("https://pdftables.com/api?key=INSERTKEYHERE&format=xlsx-single", filename, False)
  74.     xls_file = CreateTempFile("pdf")
  75.     nFileNum = FreeFile
  76.     Dim data_bytearray() As Byte 'needed to get rid of header
  77.    data_bytearray = data
  78.    
  79.     Open xls_file For Binary Lock Read Write As #nFileNum
  80.     Put #nFileNum, , data_bytearray
  81.     Close #nFileNum
  82.     Workbooks.Open (xls_file)
  83.        
  84. End Sub
  85.  
  86.  
  87. Sub pdftables()
  88.     '--- https://msdn.microsoft.com/en-us/library/office/aa219843(v=office.11).aspx
  89.  
  90.     'Declare a variable as a FileDialog object.
  91.    Dim fd As FileDialog
  92.  
  93.     'Create a FileDialog object as a File Picker dialog box.
  94.    Set fd = Application.FileDialog(msoFileDialogFilePicker)
  95.  
  96.     'Declare a variable to contain the path
  97.    'of each selected item. Even though the path is a String,
  98.    'the variable must be a Variant because For Each...Next
  99.    'routines only work with Variants and Objects.
  100.    Dim vrtSelectedItem As Variant
  101.  
  102.     'Use a With...End With block to reference the FileDialog object.
  103.    With fd
  104.  
  105.         'Use the Show method to display the File Picker dialog box and return the user's action.
  106.        'The user pressed the action button.
  107.        If .Show = -1 Then
  108.  
  109.             'Step through each string in the FileDialogSelectedItems collection.
  110.            For Each vrtSelectedItem In .SelectedItems
  111.  
  112.                 'vrtSelectedItem is a String that contains the path of each selected item.
  113.                'You can use any file I/O functions that you want to work with this path.
  114.                'This example simply displays the path in a message box.
  115.                'MsgBox "The path is: " & vrtSelectedItem
  116.                pdftables_worker (vrtSelectedItem)
  117.                
  118.             Next vrtSelectedItem
  119.         'The user pressed Cancel.
  120.        Else
  121.         End If
  122.     End With
  123.  
  124.     'Set the object variable to Nothing.
  125.    Set fd = Nothing
  126.  
  127. End Sub
Add Comment
Please, Sign In to add comment