CLINT WILLIAMS
Board Regular
- Joined
- Nov 8, 2011
- Messages
- 69
- Office Version
- 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
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