MHamid
10-02-2019, 09:55 PM
Hello,
I am using the code below in Excel to open a PDF File in Word and extract the data as text only into Excel. The code works perfectly fine as it is, however, I want to loop through all pdf files within the folder instead of hardcoding the filename in pathAndFileName variable. Can someone assist with this piece of the code?
My goal is to be able to:
Open each PDF file in Word
Extract its data as text into the Excel “Test”
Run two excel macros to parse the data that Ineed into another sheet within the same workbook set in the code
Close Word without saving the file
Clear both Word and PDF
Clear the data in Excel “Test” worksheet
Move PDF File to another folder
Repeat steps 1-6 for the next PDF File
Can anyone assist in tweaking this code to do what I need?
Sub pdf_To_Excel_Word()
'Macro opens PDF Files as an editable Word Document
'Copies the contents of the Word document
'Pastes the Clipboard contents into Excel
'Declare Variables
Dim myWorksheet As Worksheet
Dim wordApp AsWord.Application
Dim myWshShell As wshShell
Dim pathAndFileName As String
Dim registryKey As String
Dim wordVersion As String
'Set Variables
Set myWorksheet =ActiveWorkbook.Worksheets("Test")
Set wordApp = NewWord.Application
Set myWshShell = New wshShell
pathAndFileName ="C:\Users\mh15601\Desktop\Projects\Audit Plan\2019\Testing\PDF Files\DONOT DELETE\A171065-ICG-TTS-Cash Management _
and Trade-Citibank NA-IndonesiaAudit Report.pdf"
wordVersion = wordApp.Version
registryKey ="HKCU\SOFTWARE\Microsoft\Office\" & wordVersion &"\Word\Options\"
'Open and Copy PDF Files
myWshShell.RegWriteregistryKey & "DisableConvertPdfWarning", 1,"REG_DWORD"
wordApp.documents.Open Filename:=pathAndFileName, confirmconversions:=False
myWshShell.RegWriteregistryKey & "DisableConvertPdfWarning", 0,"REG_DWORD"
'Copy Data from Word
wordApp.ActiveDocument.Content.Copy
'Excel
With myWorksheet
.Range("A1").Select
.PasteSpecialFormat:="Text"
End With
'Close Word
wordApp.QuitSaveChanges:=wDoNotSaveChanges
'Clear Word and PDF
Set wordApp = Nothing
Set myWshShell = Nothing
End Sub
Thank you
I am using the code below in Excel to open a PDF File in Word and extract the data as text only into Excel. The code works perfectly fine as it is, however, I want to loop through all pdf files within the folder instead of hardcoding the filename in pathAndFileName variable. Can someone assist with this piece of the code?
My goal is to be able to:
Open each PDF file in Word
Extract its data as text into the Excel “Test”
Run two excel macros to parse the data that Ineed into another sheet within the same workbook set in the code
Close Word without saving the file
Clear both Word and PDF
Clear the data in Excel “Test” worksheet
Move PDF File to another folder
Repeat steps 1-6 for the next PDF File
Can anyone assist in tweaking this code to do what I need?
Sub pdf_To_Excel_Word()
'Macro opens PDF Files as an editable Word Document
'Copies the contents of the Word document
'Pastes the Clipboard contents into Excel
'Declare Variables
Dim myWorksheet As Worksheet
Dim wordApp AsWord.Application
Dim myWshShell As wshShell
Dim pathAndFileName As String
Dim registryKey As String
Dim wordVersion As String
'Set Variables
Set myWorksheet =ActiveWorkbook.Worksheets("Test")
Set wordApp = NewWord.Application
Set myWshShell = New wshShell
pathAndFileName ="C:\Users\mh15601\Desktop\Projects\Audit Plan\2019\Testing\PDF Files\DONOT DELETE\A171065-ICG-TTS-Cash Management _
and Trade-Citibank NA-IndonesiaAudit Report.pdf"
wordVersion = wordApp.Version
registryKey ="HKCU\SOFTWARE\Microsoft\Office\" & wordVersion &"\Word\Options\"
'Open and Copy PDF Files
myWshShell.RegWriteregistryKey & "DisableConvertPdfWarning", 1,"REG_DWORD"
wordApp.documents.Open Filename:=pathAndFileName, confirmconversions:=False
myWshShell.RegWriteregistryKey & "DisableConvertPdfWarning", 0,"REG_DWORD"
'Copy Data from Word
wordApp.ActiveDocument.Content.Copy
'Excel
With myWorksheet
.Range("A1").Select
.PasteSpecialFormat:="Text"
End With
'Close Word
wordApp.QuitSaveChanges:=wDoNotSaveChanges
'Clear Word and PDF
Set wordApp = Nothing
Set myWshShell = Nothing
End Sub
Thank you