Hi I am trying to make my array more dynamic to include all file paths in column G. However I have trouble doing that as I am getting subscript out of range whenever I try something like arrayFilePaths = Range("G4:G5") or any solution from here, Put entire column (each value in column) in an array?
I get type mismatch or subscript out of range on the OK = primaryDoc.Open(arrayFilePaths(0)) line.
My code:
Sub main()
Dim arrayFilePaths() As Variant
Set app = CreateObject("Acroexch.app")
arrayFilePaths = Array(Range("G4"), Range("G5"))
Set primaryDoc = CreateObject("AcroExch.PDDoc")
OK = primaryDoc.Open(arrayFilePaths(0))
Debug.Print "PRIMARY DOC OPENED & PDDOC SET: " & OK
For arrayIndex = 1 To UBound(arrayFilePaths)
numPages = primaryDoc.GetNumPages() - 1
Set sourceDoc = CreateObject("AcroExch.PDDoc")
OK = sourceDoc.Open(arrayFilePaths(arrayIndex))
Debug.Print "SOURCE DOC OPENED & PDDOC SET: " & OK
numberOfPagesToInsert = sourceDoc.GetNumPages
OK = primaryDoc.InsertPages(numPages, sourceDoc, 0, numberOfPagesToInsert, False)
Debug.Print "PAGES INSERTED SUCCESSFULLY: " & OK
OK = primaryDoc.Save(PDSaveFull, arrayFilePaths(0))
Debug.Print "PRIMARYDOC SAVED PROPERLY: " & OK
Set sourceDoc = Nothing
Next arrayIndex
Set primaryDoc = Nothing
app.Exit
Set app = Nothing
MsgBox "DONE"
End Sub
.Valueso vba does not try to put the range itself, but the values in the array.arrayFilePaths = Range("G4:G5").ValueSetkeyword, it's always the class' default property if no member is specified :)arrayFilePaths = Range("G4:G5").Valuethe first location is probably1not0arrayFilePaths = Range("G4:G5").Valueproduces a 2-dimensional array so the first element is actuallyarrayFilePaths(1, 1)or you need to transpose it.