Converting PDF data into Excel

Teenymoo

New Member
Joined
Dec 20, 2023
Messages
1
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I'm attempting to convert PDF data into Excel and am encountering the following problem; please assist in resolving the code.

1703490774773.png

Option Explicit
Option Compare Text

Sub pdfexcelMacro()
Dim pdf_path As String
Dim excel_path As sring
Dim fileName As String
Dim xlworkbook As Workbook
Dim xlworksheet As Worksheet
Dim wordApp As New Word.Application
Dim wordDoc As Word.Document
Dim wordRange As Word.Range

'file path ***** it should be axactly same. look at slash
pdf_path = ThisWorkbook.Path & "\pdf_files\"
excel_path = ThisWorkbook.Path & "\excel_files\"
'********************************************************

Application.ScreenUpdating = False
Application.StatusBar = False

wordApp.Visible = True

fileName = VBA.Dir(pdf_path)

While fileName <> ""
If fileName Like "*.pdf" Then
Application.CutCopyMode = False

'here we open word
Set wordDoc = wordApp.Documents.Open(pdf_path & fileName, Format:="pdf files", ReadOnly:=True)
Set wordRange = wordDoc.Paragraphs(1).Range
wordRange.WholeStory

Set xlworkbook = Excel.Workbooks.Add
Set xlworksheet = xlworkbook.Sheets(1)

'here we copy the word range
wordRange.Copy

'now pasting word data in excel
xlworksheet.PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:=False
xlworkbook.SaveAs (excel_path & VBA.Replace(fileName, ".pdf", "xlsx"))

xlworkbook.Close False
wordDoc.Close False
End If

fileName = VBA.Dir()
Wend

Application.CutCopyMode = False
Application.StatusBar = False
Application.ScreenUpdating = False
MsgBox "Done", vbInformation
'to display folder
Call Shell("explorer.exe" & excelpath, vbNormalFocus)

End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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