mi presento qui per chiedere un vostro aiuto in un problemino che ho con vba su excel 365 professional. spiego brevemente, devo importare in una cartella delle tabelle da uno o due file pdf. il problema che ho, consiste nel fatto che i pdf hanno numeri di pagine variabili, da 5 sino anche 500. quello che riesco a fare è caricare i file i query, e per unirli ho dovuto ripetere la formula accoda in base a quante pagine ci sono. non riesco a dirgli di capire da solo quante sono le pagine e di accodarle....
Sub Macro_carica_accorpa_1()
'
Dim wbk3 As Workbook
Dim Indirizzo As String
Dim Indirizzo1 As String
Dim COUNT As Integer
Dim COUNT1 As Integer
Dim DATA As String
Dim sh As Worksheet
Dim ListObj As ListObject
Dim pq As Object
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Indirizzo = Range("u1").Value
PageN = Range("w1").Value
Pages = Range("w2").Value
Indirizzo1 = Range("u2").Value
PageT = Range("w3").Value
Do
COUNT = COUNT + 1
PageRif = "Page" & Format(COUNT, "000")
ActiveWorkbook.Queries.Add Name:="Page" & Format(COUNT, "000"), Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Origine = Pdf.Tables(File.Contents(""" & Indirizzo & """), [Implementation=""1.3""])," & Chr(13) & "" & Chr(10) & " Page1 = Origine{[Id=""" & PageRif & """]}[Data]," & Chr(13) & "" & Chr(10) & " #""Modificato tipo"" = Table.TransformColumnTypes(Page1,{{""Column1"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " " & _
" #""Modificato tipo"""
Loop While COUNT < PageN
If Pages = 0 Then
Else
Do
COUNT = COUNT + 1
COUNT1 = COUNT1 + 1
PageRif = "Page" & Format(COUNT1, "000")
ActiveWorkbook.Queries.Add Name:="Page" & Format(COUNT, "000"), Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Origine = Pdf.Tables(File.Contents(""" & Indirizzo1 & """), [Implementation=""1.3""])," & Chr(13) & "" & Chr(10) & " Page1 = Origine{[Id=""" & PageRif & """]}[Data]," & Chr(13) & "" & Chr(10) & " #""Modificato tipo"" = Table.TransformColumnTypes(Page1,{{""Column1"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " " & _
" #""Modificato tipo"""
Loop While COUNT < PageT
End If
If Sheets("Comandi").Range("w3").Value = 1 Then
Sheets("Comandi").Range("w4").Value = 1
Else
End If
If Sheets("Comandi").Range("w3").Value = 2 Then
ActiveWorkbook.Queries.Add Name:="Accoda1", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Origine = Table.Combine({Page001, Page002})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Origine"
Sheets("Comandi").Range("w4").Value = 2
Else
End If
If Sheets("Comandi").Range("w3").Value = 3 Then
ActiveWorkbook.Queries.Add Name:="Accoda1", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Origine = Table.Combine({Page001, Page002, Page003})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Origine"
Sheets("Comandi").Range("w4").Value = 3
Else
End If
If Sheets("Comandi").Range("w3").Value = 4 Then
ActiveWorkbook.Queries.Add Name:="Accoda1", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Origine = Table.Combine({Page001, Page002, Page003, Page004})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Origine"
Sheets("Comandi").Range("w4").Value = 4
Else
End If
If Sheets("Comandi").Range("w3").Value = 5 Then
ActiveWorkbook.Queries.Add Name:="Accoda1", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Origine = Table.Combine({Page001, Page002, Page003, Page004, Page005})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Origine"
Sheets("Comandi").Range("w4").Value = 5
Else
End If
If Sheets("Comandi").Range("w3").Value = 6 Then
ActiveWorkbook.Queries.Add Name:="Accoda1", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Origine = Table.Combine({Page001, Page002, Page003, Page004, Page005, Page006})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Origine"
Sheets("Comandi").Range("w4").Value = 6
Else
End If
If Sheets("Comandi").Range("w3").Value = 7 Then
ActiveWorkbook.Queries.Add Name:="Accoda1", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Origine = Table.Combine({Page001, Page002, Page003, Page004, Page005, Page006, Page007})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Origine"
Sheets("Comandi").Range("w4").Value = 7
Else
End If
ecc...(io sono arrivato a scrivere sino a 70 con le possibilità di accodamento)
If Sheets("Comandi").Range("w4").Value = 1 Then
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=" & PageRif & ";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Page" & Format(COUNT, "000") & "]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Page" & Format(COUNT, "000")
.Refresh BackgroundQuery:=False
Application.DisplayAlerts = False
End With
Else
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Accoda1;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Accoda1]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Accoda1"
.Refresh BackgroundQuery:=False
End With
End If
End Sub
la formula funziona(sino alle 70 pagine fa egregiamente il suo lavoro) , ma siccome posso avere il pdf che può arrivare sino a 500 pagine e oltre, o trovo un modo per caricare tutto il pdf in una unica tabella(visto che le pagine hanno la stessa intestazione) o un modo per automatizzare magari con do e loop la parte che riguarda l'accodamento....
ActiveWorkbook.Queries.Add Name:="Accoda1", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Origine = Table.Combine({Page001, Page002, Page003, Page004, Page005})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Origine"
ma non riesco a capire come dargli i riferimenti.
Se la tua intenzione fosse quella di utilizzare Power Query per creare una query basata su un file pdf con un numero di pagine indeterminato, forse prova qualcosa del genere:
Const sPercorso As String = "C:\Users\Fabio)\Documents\myPDF.pdf" '<<=== Modifica
Const sQuery_Name As String = "Pippo" '<<=== Modifica