estrunfina_amarela
New Member
- Joined
- Feb 7, 2022
- Messages
- 7
- Office Version
- 2016
- Platform
- Windows
Hi,
I know little of this, wondering if someone could help me.
I 'm trying to create something like this thread Append to pdf VBA
I've been searching for something like this, but just for one subfolder.
How can i use this routine, but calling it inside another routine (let's say main routine) and imputing the folder path, where the pdfs are, from the main routine.
I have a main routine that makes individual pdf files out of multiple excel files, into a subfolder.
That routine start's by asking the user:
-path1 - of excel files
-path2 - for resulting pdf files
I think this could work just like I need if I input path2 inside pdftkMerge
Tried to use it, but didn't succeed
The code in the older thread:
'PDFtk Server Examples
Sub MergeToPDFtk2()
Dim a, f, i As Long, p As String
Dim p2 As String, r As String, fso As Object
Dim s As String, k As String
'Parent folder
p = ThisWorkbook.Path & "\"
p = "C:\Users\lenovo1\Dropbox\_Excel\pdf\Acrobat\"
'Folder to copy merged pdfs in subfolders to, p2 initional, and r actual.
p2 = p & "MergedPDFs"
If Dir(p2, vbDirectory) = "" Then MkDir p2
'Make a new folder in p2 to store this run's merged pdf files.
Do
i = i + 1
r = p2 & "\Run" & i & "\"
Loop Until Dir(r, vbDirectory) = ""
MkDir r
Set fso = CreateObject("Scripting.FileSystemObject")
'SubFolders Array
f = Split(CreateObject("Wscript.Shell").Exec("cmd /c dir " & _
"""" & p & """" & " /ad/b/s").StdOut.ReadAll, vbCrLf)
'Add parent folder to f:
f(UBound(f)) = Left(p, Len(p) - 1)
'Debug.Print Join(f, vbCrLf), "done"
'Merge pdfs in subfolders, save merged file in r folder with subfolder's name.pdf.
For i = 0 To UBound(f)
k = f(i) & "\" & Dir(f(i) & "\*.pdf")
If InStr(f(i), p2 & "\") = 0 And Dir(f(i) & "\*.pdf") <> "" Then
'Need 2 pdfs to merge or pdfill slows and may error
If Dir <> "" Then 'at least 2 pdfs files exist
s = "pdftk " & _
"""" & f(i) & "\*.pdf" & """" & _
" cat output " & """" & _
"""" & r & fso.GetFolder(f(i)).Name & ".pdf" & """"
Shell s, vbNormal
Else
FileCopy k, (r & fso.GetFolder(f(i)).Name & ".pdf")
End If
End If
Next i
Set fso = Nothing
MsgBox "PDF files merged to folder: " & r
End Sub
The code I'm using to export excel files as pdf:
Sub ExcelSaveAsPDF()
Dim StrPath As String
Dim xStrFile1, xStrFile2 As String
Dim xWbk As Workbook
Dim xSFD, xRFD As FileDialog
Dim xSPath As String
Dim xRPath, xWBName As String
Dim xBol As Boolean
Set xSFD = Application.FileDialog(msoFileDialogFolderPicker)
With xSFD
.Title = "Please select the folder contains the Excel files you want to convert:"
.InitialFileName = "C:\"
End With
If xSFD.Show <> -1 Then Exit Sub
xSPath = xSFD.SelectedItems.Item(1)
Set xRFD = Application.FileDialog(msoFileDialogFolderPicker)
With xRFD
.Title = "Please select a destination folder to save the converted files:"
.InitialFileName = "C:\"
End With
If xRFD.Show <> -1 Then Exit Sub
xRPath = xRFD.SelectedItems.Item(1) & "\"
StrPath = xSPath & "\"
xStrFile1 = Dir(StrPath & "*.*")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Do While xStrFile1 <> ""
xBol = False
If Right(xStrFile1, 3) = "xls" Then
Set xWbk = Workbooks.Open(FileName:=StrPath & xStrFile1)
xbwname = Replace(xStrFile1, ".xls", "_pdf")
xBol = True
ElseIf Right(xStrFile1, 3) = "XLS" Then
Set xWbk = Workbooks.Open(FileName:=StrPath & xStrFile1)
xbwname = Replace(xStrFile1, ".XLS", "_pdf")
xBol = True
ElseIf Right(xStrFile1, 4) = "xlsx" Then
Set xWbk = Workbooks.Open(FileName:=StrPath & xStrFile1)
xbwname = Replace(xStrFile1, ".xlsx", "_pdf")
xBol = True
ElseIf Right(xStrFile1, 4) = "xlsm" Then
Set xWbk = Workbooks.Open(FileName:=StrPath & xStrFile1)
xbwname = Replace(xStrFile1, ".xlsm", "_pdf")
xBol = True
End If
If xBol Then
xWbk.ExportAsFixedFormat Type:=xlTypePDF, FileName:=xRPath & xbwname & ".pdf"
xWbk.Close SaveChanges:=False
End If
xStrFile1 = Dir
Loop
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
I know little of this, wondering if someone could help me.
I 'm trying to create something like this thread Append to pdf VBA
I've been searching for something like this, but just for one subfolder.
How can i use this routine, but calling it inside another routine (let's say main routine) and imputing the folder path, where the pdfs are, from the main routine.
I have a main routine that makes individual pdf files out of multiple excel files, into a subfolder.
That routine start's by asking the user:
-path1 - of excel files
-path2 - for resulting pdf files
I think this could work just like I need if I input path2 inside pdftkMerge
Tried to use it, but didn't succeed
The code in the older thread:
'PDFtk Server Examples
Sub MergeToPDFtk2()
Dim a, f, i As Long, p As String
Dim p2 As String, r As String, fso As Object
Dim s As String, k As String
'Parent folder
p = ThisWorkbook.Path & "\"
p = "C:\Users\lenovo1\Dropbox\_Excel\pdf\Acrobat\"
'Folder to copy merged pdfs in subfolders to, p2 initional, and r actual.
p2 = p & "MergedPDFs"
If Dir(p2, vbDirectory) = "" Then MkDir p2
'Make a new folder in p2 to store this run's merged pdf files.
Do
i = i + 1
r = p2 & "\Run" & i & "\"
Loop Until Dir(r, vbDirectory) = ""
MkDir r
Set fso = CreateObject("Scripting.FileSystemObject")
'SubFolders Array
f = Split(CreateObject("Wscript.Shell").Exec("cmd /c dir " & _
"""" & p & """" & " /ad/b/s").StdOut.ReadAll, vbCrLf)
'Add parent folder to f:
f(UBound(f)) = Left(p, Len(p) - 1)
'Debug.Print Join(f, vbCrLf), "done"
'Merge pdfs in subfolders, save merged file in r folder with subfolder's name.pdf.
For i = 0 To UBound(f)
k = f(i) & "\" & Dir(f(i) & "\*.pdf")
If InStr(f(i), p2 & "\") = 0 And Dir(f(i) & "\*.pdf") <> "" Then
'Need 2 pdfs to merge or pdfill slows and may error
If Dir <> "" Then 'at least 2 pdfs files exist
s = "pdftk " & _
"""" & f(i) & "\*.pdf" & """" & _
" cat output " & """" & _
"""" & r & fso.GetFolder(f(i)).Name & ".pdf" & """"
Shell s, vbNormal
Else
FileCopy k, (r & fso.GetFolder(f(i)).Name & ".pdf")
End If
End If
Next i
Set fso = Nothing
MsgBox "PDF files merged to folder: " & r
End Sub
The code I'm using to export excel files as pdf:
Sub ExcelSaveAsPDF()
Dim StrPath As String
Dim xStrFile1, xStrFile2 As String
Dim xWbk As Workbook
Dim xSFD, xRFD As FileDialog
Dim xSPath As String
Dim xRPath, xWBName As String
Dim xBol As Boolean
Set xSFD = Application.FileDialog(msoFileDialogFolderPicker)
With xSFD
.Title = "Please select the folder contains the Excel files you want to convert:"
.InitialFileName = "C:\"
End With
If xSFD.Show <> -1 Then Exit Sub
xSPath = xSFD.SelectedItems.Item(1)
Set xRFD = Application.FileDialog(msoFileDialogFolderPicker)
With xRFD
.Title = "Please select a destination folder to save the converted files:"
.InitialFileName = "C:\"
End With
If xRFD.Show <> -1 Then Exit Sub
xRPath = xRFD.SelectedItems.Item(1) & "\"
StrPath = xSPath & "\"
xStrFile1 = Dir(StrPath & "*.*")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Do While xStrFile1 <> ""
xBol = False
If Right(xStrFile1, 3) = "xls" Then
Set xWbk = Workbooks.Open(FileName:=StrPath & xStrFile1)
xbwname = Replace(xStrFile1, ".xls", "_pdf")
xBol = True
ElseIf Right(xStrFile1, 3) = "XLS" Then
Set xWbk = Workbooks.Open(FileName:=StrPath & xStrFile1)
xbwname = Replace(xStrFile1, ".XLS", "_pdf")
xBol = True
ElseIf Right(xStrFile1, 4) = "xlsx" Then
Set xWbk = Workbooks.Open(FileName:=StrPath & xStrFile1)
xbwname = Replace(xStrFile1, ".xlsx", "_pdf")
xBol = True
ElseIf Right(xStrFile1, 4) = "xlsm" Then
Set xWbk = Workbooks.Open(FileName:=StrPath & xStrFile1)
xbwname = Replace(xStrFile1, ".xlsm", "_pdf")
xBol = True
End If
If xBol Then
xWbk.ExportAsFixedFormat Type:=xlTypePDF, FileName:=xRPath & xbwname & ".pdf"
xWbk.Close SaveChanges:=False
End If
xStrFile1 = Dir
Loop
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub