1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| Sub OpenPDF()
'-------------------------------------------'
' You need to create a bat file first with one single line of text
' pdftotext.exe -layout YourPage.pdf
' DOWNLOAD LINK: http://www.foolabs.com/xpdf/download.html
'-------------------------------------------'
' these lines look for a pdf file in your My Documents folder
Set WshShell = CreateObject("WScript.Shell")
ChDir (WshShell.SpecialFolders("MyDocuments"))
PageName = Application.GetOpenFilename("YourPage, *.pdf", , "YourPage")
' if no file is picked the macro ends
If PageName = "False" Then
Exit Sub
End If
' copies and renames the pdf file to the pdf2txt folder
FileCopy PageName, "C:\pdf2txt\YourPage.pdf"
ChDir ("C:\pdf2txt")
' THE BATFILE CONTAINS ONLY 1 LINE:
' pdftotext.exe -layout YourPage.pdf
TestValue = Shell("YourPage.bat", 1)
' because the bat file runs for 1 or 2 seconds (in my case)
' I let the Excel macro wait 5 seconds before doing anything else
' there are more ingenious ways for VBA to wait for the end of an
' application, but this suits me fine...
Application.Wait (Now + TimeValue("0:00:50"))
ChDir "C:\pdf2txt"
PageName = "C:\pdf2txt\YourPage.txt"
' the following reads the text that has been generated
ReadTextFile (PageName)
' insert your text parsing - text to columns - ingenious vba stuff hereafter...
End Sub
Sub ReadTextFile(chemin As String)
Dim FileNum As Integer
Dim r As Variant
Dim wb As Workbook
Dim Data As String
r = 1
FileNum = FreeFile
Set wb = Workbooks.Add
Open chemin For Input As #FileNum
Do While Not EOF(FileNum)
Line Input #FileNum, Data
ActiveSheet.Cells(r, 1) = Data
r = r + 1
Loop
Close #FileNum
End Sub |