I am using VBA to try and hopefully automate my PowerQuery needs.
I've recorded a macro from the import data from PDF in the data tab.
I have then tried creating a user definable source path within the code in the macro (See below)
Sub PDF_PQ()
'
' PDF_PQ Macro
'
Dim q As WorkbookQuery
Dim fName As String
Dim Dir As String
Dir = Worksheets("Settings").Cells(10, 3).Value
fName = Worksheets("Settings").Cells(11, 3).Value
' Load PDF Data
'
ActiveWorkbook.Queries.Add Name:="Page002", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(Dir & fName), [Implementation=""1.3""])," & Chr(13) & "" & Chr(10) & " Page1 = Source{[Id=""Page002""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Page1,{{""Column1"", type text}, {""Column2"", type text}, {""Co" & _
"lumn3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6"", type text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}, {""Column11"", type text}, {""Column12"", type text}, {""Column13"", type text}, {""Column14"", type text}, {""Column15"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Page002;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Page002]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Page002"
'.Refresh BackgroundQuery:=False
End With
End Sub
The original has the PDF filepath baked into the source section of the code (See Below)
Original
Source = Pdf.Tables(File.Contents(""D:\OneDrive\OneDrive - Bendtech\The Laser Factory\6. General Documents\bus build spec\JN 1393 Braybon Industries VIC Scania SB.pdf"")
My Changes
Source = Pdf.Tables(File.Contents(Dir & fName)
I am trying to make the Source user definable by being able to select a different PDF and import that.
My tries at rectifying the problem at hand has been uneventful and results in an error of not importing the data.
All I have changed from the original code is the Source path and it seems to have broken the entire thing.
As stated above I had only made the change to the source of the PDF Source.
Importing different PDF's all utilise the same layout, column names etc. but I am having trouble importing the query into the worksheet.
I am at a loss to what I have done to the code X-D
Create a Dynamic File Path
in PowerQuery without using VBA. youtube.com/watch?v=T5aWvqf9SnM