# Error In Merge Pdf File



## Look2020 (Dec 16, 2022)

Hello 
I have this error 
Compile Error:
variable Not Defined

when running this code


----------



## bferraz (Dec 16, 2022)

Look2020 said:


> Hello
> I have this error
> Compile Error:
> variable Not Defined
> ...


Can you please upload the code?


----------



## Look2020 (Dec 16, 2022)

Option Explicit
Sub MergePDFFilesBasedonRange()
Dim Acro_App As Acrobat.AcroApp
Dim Acro_DestDoc As Acrobat.AcroPDDoc
Dim Acro_TargDoc As Acrobat.AcroPDDoc
Dim cntr As Long
Set Acro_App = New Acrobat.AcroApp
Set Acro_DestDoc = New Acrobat.AcroPDDoc
Set Acro_TargDoc = New Acrobat.AcroPDDoc

'Lets validate file path first
For cntr = 2 To Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp).Row
If VBA.Dir(Sheet2.Range("A" & cntr).Value) = "" Then
MsgBox "File" & Sheet2.Range("A" & cntr).Value & "doesnt exists.."
Exit Sub
End If

Next cntr
Select_Files = Sheet2.Range("A2:A9").Value
Acro_DestDoc.Open (Select_Files(LBound(Select_Files), 1))
For cntr = LBound(Select_Files) + 1 To UBound(Select_Files)
'open target doc
Acro_TargDoc.Open (Select_Files(cntr, 1))
'Let merge
If Acro_DestDoc.InsertPages(Acro_DestDoc.GetNumPages() - 1, Acro_TargDoc, 0, Acro_TargDoc.GetNumPages(), True) = True Then
MsgBox "Merge in process..."
End If
Acro_TargDoc.Close
Next cntr
Acro_DestDoc.Save 1, ThisWorkbook.Path & "\" & "print" & "\" & "Merge.pdf"
End Sub


----------



## Look2020 (Dec 16, 2022)

Error related to this item  (Select_Files)


----------



## bferraz (Dec 16, 2022)

Can you please try this?


```
Option Explicit
Sub MergePDFFilesBasedonRange()
Dim Acro_App As Acrobat.AcroApp
Dim Acro_DestDoc As Acrobat.AcroPDDoc
Dim Acro_TargDoc As Acrobat.AcroPDDoc
Dim cntr As Long
Dim Select_Files as String
Set Acro_App = New Acrobat.AcroApp
Set Acro_DestDoc = New Acrobat.AcroPDDoc
Set Acro_TargDoc = New Acrobat.AcroPDDoc

'Lets validate file path first
For cntr = 2 To Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp).Row
If VBA.Dir(Sheet2.Range("A" & cntr).Value) = "" Then
MsgBox "File" & Sheet2.Range("A" & cntr).Value & "doesnt exists.."
Exit Sub
End If

Next cntr
Select_Files = Sheet2.Range("A2:A9").Value
Acro_DestDoc.Open (Select_Files(LBound(Select_Files), 1))
For cntr = LBound(Select_Files) + 1 To UBound(Select_Files)
'open target doc
Acro_TargDoc.Open (Select_Files(cntr, 1))
'Let merge
If Acro_DestDoc.InsertPages(Acro_DestDoc.GetNumPages() - 1, Acro_TargDoc, 0, Acro_TargDoc.GetNumPages(), True) = True Then
MsgBox "Merge in process..."
End If
Acro_TargDoc.Close
Next cntr
Acro_DestDoc.Save 1, ThisWorkbook.Path & "\" & "print" & "\" & "Merge.pdf"
End Sub
```


----------



## Look2020 (Dec 16, 2022)

thank but
The code gives an error in this part:
(LBound(Select_Files), 1))


----------



## bferraz (Dec 16, 2022)

Look2020 said:


> thank but
> The code gives an error in this part:
> (LBound(Select_Files), 1))



I think that was my mistake, can you please try this one out?


```
Option Explicit
Sub MergePDFFilesBasedonRange()
Dim Acro_App As Acrobat.AcroApp
Dim Acro_DestDoc As Acrobat.AcroPDDoc
Dim Acro_TargDoc As Acrobat.AcroPDDoc
Dim cntr As Long
Dim Select_Files as Variant
Set Acro_App = New Acrobat.AcroApp
Set Acro_DestDoc = New Acrobat.AcroPDDoc
Set Acro_TargDoc = New Acrobat.AcroPDDoc

'Lets validate file path first
For cntr = 2 To Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp).Row
If VBA.Dir(Sheet2.Range("A" & cntr).Value) = "" Then
MsgBox "File" & Sheet2.Range("A" & cntr).Value & "doesnt exists.."
Exit Sub
End If

Next cntr
Select_Files = Sheet2.Range("A2:A9").Value
Acro_DestDoc.Open (Select_Files(LBound(Select_Files), 1))
For cntr = LBound(Select_Files) + 1 To UBound(Select_Files)
'open target doc
Acro_TargDoc.Open (Select_Files(cntr, 1))
'Let merge
If Acro_DestDoc.InsertPages(Acro_DestDoc.GetNumPages() - 1, Acro_TargDoc, 0, Acro_TargDoc.GetNumPages(), True) = True Then
MsgBox "Merge in process..."
End If
Acro_TargDoc.Close
Next cntr
Acro_DestDoc.Save 1, ThisWorkbook.Path & "\" & "print" & "\" & "Merge.pdf"
End Sub
```


----------



## Look2020 (Dec 16, 2022)

thanks but


----------



## Look2020 (Dec 16, 2022)




----------

