How to copy data from pdf to excel using VBA for multiple sheets?

harikris2013

Board Regular
Joined
May 3, 2013
Messages
65
Hi,

I have 200 sheets of data in PDF file which needs to copy to 200 sheets in excel workbook

I need to use only acrobat reader 5.

I have below macro which is copying only one and pasting to sheet1

Please modify the below macro

Sub StartAdobe()

Dim AdobeApp As String
Dim AdobeFile As String
Dim StartAdobe

AdobeApp = "C:\Program Files (x86)\Adobe\Acrobat 5.0\Reader\AcroRd32.exe"
AdobeFile = "C:\Users\Dell\Desktop\Dead Men Tell No Tales.pdf"

StartAdobe = Shell("" & AdobeApp & " " & AdobeFile & "", 1)

Application.OnTime Now + TimeValue("00:00:05"), "FirstStep"

End Sub


Private Sub FirstStep()


SendKeys ("^a")
SendKeys ("^c")

Application.OnTime Now + TimeValue("00:00:10"), "SecondStep"

End Sub

Private Sub SecondStep()

AppActivate "Microsoft Excel"

Range("A1").Activate
SendKeys ("^v")


End Sub
 
Hello,

I stepped through the code and I see it deleting the sheet it creates. However, the file names are not the same and it should not be deleting them. These are the file names:
Audit Report - A234099 CS
Internal Audit Report – A
A110525 - Lane Archive Te
A289122 - Internal Audit
A19005 - Retail - Financi
A16806 - US Funds Transfe
Internal Audit - Direct C
A137855 Internal Audit Re
A170584 - Retail - Balanc

Now that I think about it, I don't need to delete the sheet anyways. I don't need to worry about duplication so I just commented that section out.
However, is there a way to name the sheet as A###### instead of the file name. The A###### is part of the file name and I don't really need the file name since it is very long and will be truncated. I just need it to look at the filename and name the sheet as A###### (could be 5 or 6 numbers after the letter A). Is this possible?

Thank you
 
Upvote 0

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Are these 200 pages in one pdf, or 200 pdf files? I am assuming the latter although you write that it is only one file.

You need to loop through each of the files in a sub that then calls the startAdobe sub. But somewhere you will also want to close the pdf files.

Let's assume that all the pdf files are in one folder. The following will do the trick. I have used some of your code, but not the paste part, as we can use Excel's paste for that.


Read throuh the code (plus comments) and see what it does and how it differs from yours


<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> LoopThroughFiles()<br> <SPAN style="color:#00007F">Dim</SPAN> strFile <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, strPath <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br> <SPAN style="color:#00007F">Dim</SPAN> colFiles <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">New</SPAN> Collection<br> <SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br> <SPAN style="color:#00007F">Dim</SPAN> rLog <SPAN style="color:#00007F">As</SPAN> Range, rOut <SPAN style="color:#00007F">As</SPAN> Range<br> <SPAN style="color:#00007F">Dim</SPAN> wsLog <SPAN style="color:#00007F">As</SPAN> Worksheet, wsOutp <SPAN style="color:#00007F">As</SPAN> Worksheet<br> <br> strPath = "C:\TEMP\"<br> strFile = Dir(strPath)<br> <SPAN style="color:#007F00">' Make a log sheet</SPAN><br> <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN><br> <SPAN style="color:#00007F">Set</SPAN> wsLog = Sheets("PdfProcessLog")<br> <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0<br> <SPAN style="color:#00007F">If</SPAN> wsLog <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br> <SPAN style="color:#00007F">Set</SPAN> wsLog = ThisWorkbook.Sheets.Add(before:=Sheets(1))<br> wsLog.Name = "PdfProcessLog"<br> <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br> <SPAN style="color:#00007F">Set</SPAN> rLog = wsLog.Range("A1")<br> rLog.CurrentRegion.ClearContents<br> rLog.Value = "PDF files copied to sheets"<br> <br> <SPAN style="color:#007F00">' load all the files in a Collection</SPAN><br> <SPAN style="color:#00007F">While</SPAN> strFile <> ""<br> <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">StrComp</SPAN>(Right(strFile, 3), "pdf", vbTextCompare) = 0 <SPAN style="color:#00007F">Then</SPAN><br> colFiles.Add strFile<br> <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br> strFile = Dir<br> <SPAN style="color:#00007F">Wend</SPAN><br> <br> Application.DisplayAlerts = <SPAN style="color:#00007F">False</SPAN><br> <br> <SPAN style="color:#007F00">'Loop through the pdf's stored in the collection</SPAN><br> <SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> colFiles.Count<br> <SPAN style="color:#007F00">'List filenames in Column A of the log sheet</SPAN><br> rLog.Offset(i, 0).Value = colFiles(i)<br> strFile = Left(colFiles(i), Len(colFiles(i)) - 4)<br> <br> <SPAN style="color:#007F00">' Delete sheet with filename if exists</SPAN><br> <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN><br> <SPAN style="color:#00007F">Set</SPAN> wsOutp = Sheets(strFile)<br> <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0<br> <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> wsOutp <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br> wsOutp.Delete<br> <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br> <SPAN style="color:#007F00">' (Re)Create the worksheet, give it the file name</SPAN><br> <SPAN style="color:#00007F">Set</SPAN> wsOutp = ThisWorkbook.Sheets.Add(after:=wsLog)<br> wsOutp.Name = strFile<br> <br> <SPAN style="color:#007F00">' Now open the file, and copy contents</SPAN><br> OpenClosePDF colFiles(i), strPath<br> CopyStep wsOutp<br> <SPAN style="color:#00007F">Next</SPAN> i<br> <br> Application.DisplayAlerts = <SPAN style="color:#00007F">True</SPAN><br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><SPAN style="color:#00007F">Sub</SPAN> OpenClosePDF(<SPAN style="color:#00007F">ByVal</SPAN> sAdobeFile <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> sPath <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>)<br><br> <SPAN style="color:#00007F">Dim</SPAN> sAdobeApp <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br> <SPAN style="color:#00007F">Dim</SPAN> vStartAdobe <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br> <br> sAdobeApp = "C:\Program Files (x86)\Adobe\Acrobat 5.0\Reader\AcroRd32.exe"<br> sAdobeApp = "C:\Program Files\Adobe\Reader 11.0\Reader\AcroRd32.exe"<br> vStartAdobe = Shell("" & sAdobeApp & " " & sPath & sAdobeFile & "", 1)<br> Application.Wait (Now + TimeValue("0:00:01"))<br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CopyStep(wsOutp <SPAN style="color:#00007F">As</SPAN> Worksheet)<br><br> <SPAN style="color:#007F00">' select all & copy</SPAN><br> SendKeys "^a", <SPAN style="color:#00007F">True</SPAN><br> SendKeys "^c", <SPAN style="color:#00007F">True</SPAN><br> Application.Wait (Now + TimeValue("0:00:01"))<br> <SPAN style="color:#007F00">' Paste into the sheet from cell A1</SPAN><br> wsOutp.Paste Cells(1, 1)<br> <br> Application.Wait (Now + TimeValue("0:00:01"))<br> AppActivate "Adobe Reader"<br> <SPAN style="color:#007F00">' close Reader</SPAN><br> SendKeys "%{F4}", <SPAN style="color:#00007F">True</SPAN><br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br></FONT>
hi expert,
can you show the above code again ? as it is not clear, thank you.
 
Upvote 0
VBA Code:
Sub LoopThroughFiles()
    Dim strFile As String, strPath As String
    Dim colFiles As New Collection
    Dim i As Integer
    Dim rLog As Range, rOut As Range
    Dim wsLog As Worksheet, wsOutp As Worksheet
    
    strPath = "C:\TEMP\"
    strFile = Dir(strPath)
    ' Make a log sheet
    On Error Resume Next
    Set wsLog = Sheets("PdfProcessLog")
    On Error GoTo 0
    If wsLog Is Nothing Then
        Set wsLog = ThisWorkbook.Sheets.Add(before:=Sheets(1))
        wsLog.Name = "PdfProcessLog"
    End If
    Set rLog = wsLog.Range("A1")
    rLog.CurrentRegion.ClearContents
    rLog.Value = "PDF files copied to sheets"
    
    ' load all the files in a Collection
    While strFile  ""
        If StrComp(Right(strFile, 3), "pdf", vbTextCompare) = 0 Then
            colFiles.Add strFile
        End If
        strFile = Dir
    Wend
    
    Application.DisplayAlerts = False
    
    'Loop through the pdf's stored in the collection
    For i = 1 To colFiles.Count
        'List filenames in Column A of the log sheet
        rLog.Offset(i, 0).Value = colFiles(i)
        strFile = Left(colFiles(i), Len(colFiles(i)) - 4)
        
        ' Delete sheet with filename if exists
        On Error Resume Next
        Set wsOutp = Sheets(strFile)
        On Error GoTo 0
        If Not wsOutp Is Nothing Then
            wsOutp.Delete
        End If
        ' (Re)Create the worksheet, give it the file name
        Set wsOutp = ThisWorkbook.Sheets.Add(after:=wsLog)
        wsOutp.Name = strFile
        
        ' Now open the file, and copy contents
        OpenClosePDF colFiles(i), strPath
        CopyStep wsOutp
    Next i
    
    Application.DisplayAlerts = True

End Sub
Sub OpenClosePDF(ByVal sAdobeFile As String, ByVal sPath As String)

    Dim sAdobeApp As String
    Dim vStartAdobe As Variant
    
    sAdobeApp = "C:\Program Files (x86)\Adobe\Acrobat 5.0\Reader\AcroRd32.exe"
    sAdobeApp = "C:\Program Files\Adobe\Reader 11.0\Reader\AcroRd32.exe"
    vStartAdobe = Shell("" & sAdobeApp & " " & sPath & sAdobeFile & "", 1)
    Application.Wait (Now + TimeValue("0:00:01"))

End Sub


Private Sub CopyStep(wsOutp As Worksheet)

    ' select all & copy
    SendKeys "^a", True
    SendKeys "^c", True
     Application.Wait (Now + TimeValue("0:00:01"))
   ' Paste into the sheet from cell A1
    wsOutp.Paste Cells(1, 1)
   
    Application.Wait (Now + TimeValue("0:00:01"))
    AppActivate "Adobe Reader"
    ' close Reader
    SendKeys "%{F4}", True

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,108
Messages
6,123,131
Members
449,097
Latest member
mlckr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top