BalloutMoe
Board Regular
- Joined
- Jun 4, 2021
- Messages
- 137
- Office Version
- 365
- Platform
- Windows
Hello all,
I have a code that I have used for several months with no issues at all. However today I tried it and its not combining the pdf files whatsoever. I tried a few things but with no result. I even tried repairing the installation from inside Adobe Acrobat DC, also no help. Can anyone please help? Below is the code. Thank you
I have a code that I have used for several months with no issues at all. However today I tried it and its not combining the pdf files whatsoever. I tried a few things but with no result. I even tried repairing the installation from inside Adobe Acrobat DC, also no help. Can anyone please help? Below is the code. Thank you
VBA Code:
Sub SaveActiveSheetsAsPDF()
Dim excelfilename As String, shopfilename As String, shopname3 As String
Dim filename2 As String, shopname As String, shopname2 As String, shopname4 As String
filename2 = Application.ActiveWorkbook.FullName
shopname2 = Mid(filename2, 27)
ary = Split(shopname2, "\")
shopname3 = ary(UBound(ary))
Debug.Print shopname3
ary = Split(shopname3, ".xlsx")
'Create and assign variables
shopname4 = ary(LBound(ary))
Debug.Print shopname4
Dim saveLocation As String, savelocation2 As String
saveLocation = ActiveWorkbook.ActiveSheet.Range("AJ1")
savelocation2 = ActiveWorkbook.ActiveSheet.Range("AJ2")
'Write PDF Names
Dim objFSO As Scripting.FileSystemObject
Set location = ActiveWorkbook.ActiveSheet.Range("AA:AA")
Range("AA1:AE55").ClearContents
Dim objfile As Scripting.File
Dim objFolder As Scripting.Folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(saveLocation)
Dim i As Integer
i = 2
For Each objfile In objFolder.Files
Debug.Print objfile.Name
location.Cells(i, 1) = objfile.Path
i = i + 1
Next
'Save Active Sheet(s) as PDF
Dim FileExt As String
FileExt = ".pdf"
ActiveWorkbook.ActiveSheet.Range("AA1").value = savelocation2 & shopname4 & FileExt
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
FileName:=savelocation2 & shopname4 & FileExt
'Write PDF Names
'Combine PDF
Dim objCAcroPDDocDestination As Acrobat.CAcroPDDoc
Dim objCAcroPDDocSource As Acrobat.CAcroPDDoc
Dim PDFfiles As Range, PDFfile As Range
Dim n As Long
With ActiveWorkbook.ActiveSheet
Set PDFfiles = .Range("AA1", .Cells(.rows.Count, "AA").End(xlUp))
End With
'Create Acrobat API objects
Set objCAcroPDDocDestination = CreateObject("AcroExch.PDDoc")
Set objCAcroPDDocSource = CreateObject("AcroExch.PDDoc")
'Open first PDF file and merge other PDF files into it
n = 0
For Each PDFfile In PDFfiles
n = n + 1
If n = 1 Then
objCAcroPDDocDestination.Open PDFfile.value
Else
objCAcroPDDocSource.Open PDFfile.value
If Not objCAcroPDDocDestination.InsertPages(objCAcroPDDocDestination.GetNumPages - 1, objCAcroPDDocSource, 0, objCAcroPDDocSource.GetNumPages, 0) Then
MsgBox "Error merging " & PDFfile.value
End If
objCAcroPDDocSource.Close
End If
Next
Debug.Print FileName
'Save merged PDF files as a new file
objCAcroPDDocDestination.Save 1, ActiveWorkbook.ActiveSheet.Range("AA1")
objCAcroPDDocDestination.Close
Set objCAcroPDDocSource = Nothing
Set objCAcroPDDocDestination = Nothing
MsgBox "Created " & ActiveWorkbook.ActiveSheet.Range("AA1")
End Sub