Good morning,
I would like to convert the PDF file to Word one (from PDF to DOCX) using Excel macro.
So far I learned the process from this video:
https://www.youtube.com/watch?v=Op25fUfvIl0
and this link:
https://www.pk-anexcelexpert.com/pdf-to-word-converter-macro-in-excel-vba/
but the problem is, that the example is based on the certain cells including the fixed file directory:
pdf_path = sh.Range("E4").Value
word_path = sh.Range("E5").Value
I would like to have always directory the same as my active workbook, which I am working on.
In this event I tried the following code:
Sub Wort_To_PDF()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.DisplayStatusBar = True
Dim pdf_path As String
Dim word_path As String
pdf_path = ThisWorkbook.Path & "\"
word_path = ThisWorkbook.Path & "\"
Dim fso As New FileSystemObject
Dim fo As Folder
Dim f As File
Set fo = fso.GetFolder(pdf_path)
Dim wa As Object
Dim doc As Object
Set wa = CreateObject("word.application")
wa.Visible = True
Dim file_Count As Integer
For Each f In fo.Files
Application.StatusBar = "Converting - " & file_Count + 1 & "/" & fo.Files.Count
Set doc = wa.Documents.Open(f.Path)
doc.SaveAs2 (word_path & "\" & Replace(f.Name, ".pdf", ".docx"))
doc.Close False
file_Count = file_Count + 1
Next
wa.Quit
MsgBox "All PDF files have been converted in to word", vbInformation
Application.StatusBar = ""
End Sub
I am getting an error "Type mismatch" pointing the following line:
Set fo = fso.GetFolder(pdf_path)
I found some hints regarding the usage of active workbook directory in VBA
How to get the path of current worksheet in VBA?
and tried to put it into my code:
pdf_path = Application.ActiveWorkbook.Path
word_path = Application.ActiveWorkbook.FullName
but the error is exactly the same.
Can anyone help me? I would like to convert the PDF file to docx in the same directory, where my active workbook is stored.
UPDATE:
When I change Dim fo as Folder
to Dim fo As Object
or Dim fo as Scripting.Folder
I am getting another error, informing me, that file is corrupted. Debugger shows the following line:
Set doc = wa.Documents.Open(f.Path)
I think, that problem might be somewhere with my excel document, which is already opened and used. In general, the code executes the first sheet only instead of all of them.