VBA - open PDF file into Excel -

sara121

New Member
Joined
Jul 7, 2022
Messages
23
Office Version
  1. 365
Platform
  1. Windows
Hi Everyone,

I recorded a macro for opening PDF file into Excel using the "Data --> get data --> from PDF file.

The macro works great with the file path hardcoded.
VBA Code:
 ActiveWorkbook.Queries.Add Name:="Table001", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Source = Pdf.Tables(File.Contents(""[COLOR=rgb(226, 80, 65)]C:\PDF Folder\Invoices.pdf[/COLOR]""), [Implementation=""1.3""])," & Chr(13) & "" & Chr(10) & "    Table001 = Source{[Id=""Table001""]}[Data]," & Chr(13) & "" & Chr(10) & "    #""Promoted Headers"" = Table.PromoteHeaders(Table001, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(#""Promoted Headers"",{{""Expected" & _
        " arrival"", type date}, {""Advice date"", type text}, {""Customer name"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Account"", Int64.Type}, {""Customer"", type text}, {""Amount"", Currency.Type}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
    ActiveWorkbook.Worksheets.Add

I tried to add script before this to either prompt user to browse for file location or reference to a cell for the PDF file. It stopped working.

For example: I placed the same file path in the Control sheet in cell A1 and have the macro call the "file_path". It returned an error saying: "the supplied file path must be a valid absolute path"

VBA Code:
Dim Control_sh As Worksheet
Set Control_sh = ThisWorkbook.Sheets("CONTROL")

Dim pdf_path As String
pdf_path = Control_sh.Range("A1").Value

    ActiveWorkbook.Queries.Add Name:="Table001", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Source = Pdf.Tables(File.Contents(""[COLOR=rgb(209, 72, 65)]pdf_path[/COLOR]""), 
[Implementation=""1.3""])," & Chr(13) & "" & Chr(10) & "    Table001 = Source{[Id=""Table001""]}[Data]," & Chr(13) & "" & Chr(10) & "    #""Promoted Headers"" = Table.PromoteHeaders(Table001, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(#""Promoted Headers"",{{""Expected" & _
        " arrival"", type date}, {""Advice date"", type text}, {""Customer name"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Account"", Int64.Type}, {""Customer"", type text}, {""Amount"", Currency.Type}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
    ActiveWorkbook.Worksheets.Add

I tried to use the GetOpenFileName function to prompt user to browse for the file and pass the file location to the File.Content(" ") . It did not work either.

VBA Code:
Dim FileLocation As String    
FileLocation = Application.GetOpenFilename
If FileLocation = "False" Then
Beep
Exit Sub
End If

Can someone shed some lights as to how to pass the file path?

Thanks.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Does this help:

Rich (BB code):
    ActiveWorkbook.Queries.Add Name:="Page001", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Source = Pdf.Tables(File.Contents(""" & pdf_path & """), [Implementation=""1.3""])," & Chr(13) & "" & Chr(10) & "    Page1 = Source{[Id=""Page001""]}[Data]," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(Page1,{{""Column1"", type text}, {""Column2"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
 
Upvote 0
Solution

Forum statistics

Threads
1,223,244
Messages
6,170,976
Members
452,372
Latest member
Natalie18

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