Import PDF into power query using vba

CLINT WILLIAMS

Board Regular
Joined
Nov 8, 2011
Messages
69
Office Version
  1. 365
Hi all

I am trying into import a pdf file into power query using vba. the format of the pdf table is the same just the data changes. I want to try to make it generic so that I can import any pdf. I found some code trying to modify the code in blue but getting stuck :(

Sub Macro1()

Dim queryName As String, sourceFullName As String
Dim pqDestinationCell As Range

With ActiveSheet
' queryName = Replace(.Range("A1").Value, " ", "_")
' sourceFullName = .Range("A2").Value
' Set pqDestinationCell = .Range("A3")
queryName = Replace(.Range("A1").Value, " ", "_")
sourceFullName = .Range("A2").Value
Set pqDestinationCell = .Range("A3")
End With

ActiveWorkbook.Queries.Add Name:=queryName, Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""C:\Users\om05099\Desktop\RPS EASYFILE\2021\UPDATES\30082021\RPS SOA 2021 30082021.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table003 = Source{[Id=""[COLOR=rgb(44, 130, 201)]Table003[/COLOR]""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table003,{{""Column1"", type date}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", typ" & _
"e text}, {""Column5"", type text}, {""Column6"", type text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""

'
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""" & queryName & """;Extended Properties=""""" _
, Destination:=pqDestinationCell).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [" & queryName & "]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.Name = queryName 'also assigns same value to .ListObject.DisplayName
.Refresh BackgroundQuery:=False
End With

End Sub

Any help would be appreciated.

Thanks
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

alansidman

Well-known Member
Joined
Feb 26, 2007
Messages
6,668
Office Version
  1. 365
Platform
  1. Windows
To start, instead of trying to make someone else code yours, I would turn on the macro recorder and run the steps manually. Then post your code to this site and explain what you wish to do with variables that need to replace recorded hard codes.
 

CLINT WILLIAMS

Board Regular
Joined
Nov 8, 2011
Messages
69
Office Version
  1. 365
Hi

I did what you suggested and here is the code below. What I am attempting to do is create the path a variable to read from cell "A2" and so to the name of the pdf doc from "A1". The table name should also be variable and not a specific table name as below eg Table003 as other pdfs will different table names.

Sub Macro1()
'
' Macro1 Macro
'

'
ActiveWorkbook.Queries.Add Name:="Table003 (Page 1-5) (2)", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""C:\Users\om05099\Desktop\RPS EASYFILE\2021\UPDATES\30082021\RPS SOA 2021 30082021.pdf""), [Implementation=""1.2""])," & Chr(13) & "" & Chr(10) & " Table003 = Source{[Id=""Table003""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table003,{{""Column1"", type date}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", typ" & _
"e text}, {""Column5"", type text}, {""Column6"", type text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""


With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Table003 (Page 1-5) (2)"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Table003 (Page 1-5) (2)]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table003__Page_1_5___2"
.Refresh BackgroundQuery:=False
End With
Application.CommandBars("Queries and Connections").Visible = False
End Sub
 

Forum statistics

Threads
1,148,257
Messages
5,745,707
Members
423,968
Latest member
CHHeights

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
Top