Consulting

Results 1 to 3 of 3

Thread: Add table from pdf to excel macro

  1. #1
    VBAX Newbie
    Joined
    May 2022
    Posts
    2
    Location

    Add table from pdf to excel macro

    Hi,

    I have a query about a macro using the get from file function in excel. Specifically I want to extract information from a file, however this file path is depended on a cell information in the excel file. I create the path name based on the strings at the start of the macro. I then point the source of the PDF (highlighted in red in the VBA code see below, this is only an extract of the final code), however I doesn't seem to like that source and I get the error attached. I can't find an example VBA to see what I doing wrong. I think its because the code is actually inserting ''path'' into the excel query rather than the file extension. You can see screenshot from query attached. Any help would be greatly appreciated.

    Query shot.jpgerror.jpg

    ActiveWorkbook.Queries.Add Name:="Table004 (Page 1) (2)", Formula:= _
    "let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(path), [Implementation=""1.3""])," & Chr(13) & "" & Chr(10) & " Table004 = Source{[Id=""Table004""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table004,{{""Column1"", type text}, {""Column2"", type number}, {""Column3"", type text}, {""Colu" & _
    "mn4"", type text}, {""Column5"", type number}, {""Column6"", type text}, {""Column7"", type number}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    path isn't being recognised as a variable in your code because it's inside quotes. You can either do something like this:
    Path = "C:\Users\blah\Downloads\blah.pdf"
    ActiveWorkbook.Queries.Add Name:="Table004 (Page 1) (2)", Formula:= _
    "let" & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(""" & Path & """), [Implementation=""1.3""])," & Chr(13) & "" & Chr(10) & " Table004 = Source{[Id=""Table004""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table004,{{""Column1"", type text}, {""Column2"", type number}, {""Column3"", type text}, {""Colu" & _
    "mn4"", type text}, {""Column5"", type number}, {""Column6"", type text}, {""Column7"", type number}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
    or have the code include a path step so it looks like:
    2022-05-05_190256.jpg
    Where the code for that could be:
        ActiveWorkbook.Queries.Add Name:="Table004 (Page 1) (2)", Formula:= _
            "let" & Chr(13) & "" & Chr(10) & " path = ""C:\Users\blah\Downloads\blah.pdf""," & Chr(13) & "" & Chr(10) & " Source = Pdf.Tables(File.Contents(path), [Implementation=""1.3""])," & Chr(13) & "" & Chr(10) & " Table004 = Source{[Id=""Table004""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Table004,{{""Column1"", type text}, {""Column2"", type number}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type number}, {""Colum" & _
            "n6"", type text}, {""Column7"", type number}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
    or some combination of the above.

    BUT, you might be able to do without vba altogether since you can easily grab data from a sheet into Power Query; if you have a single cell named range, say fullPathAndName in the sheet, PQ can grab that data with the likes of:
    2022-05-05_204335.jpg
    Last edited by p45cal; 05-05-2022 at 12:45 PM.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Newbie
    Joined
    May 2022
    Posts
    2
    Location
    Thanks that worked great

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •