どうせ誰も読んでない( *゚∀゚)v―.o.oo

だれも読んでないと思って勝手なことを!

cubePDFを使ってpdfファイルをマージするexcelvba

以下のVBAコードを使用して、ファイル選択ダイアログを表示してPDFファイルが格納されているフォルダを選択できるようにすることができます。このコードでは、Excelの標準ダイアログを使用してフォルダを選択しています。

 

 

Sub MergePDFsUsingCubePDF()

' cubePDFのパスを指定します。
' 例: "C:\Program Files\cubePDF\cubepdf.exe"
Const cubePDFPath As String = "C:\Program Files\cubePDF\cubepdf.exe"

' PDFファイルが格納されているフォルダを選択します。
Dim folderPath As String
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Title = "PDFファイルが格納されているフォルダを選択してください"
If .Show = -1 Then
folderPath = .SelectedItems(1)
Else
MsgBox "フォルダが選択されていません。", vbExclamation, "エラー"
Exit Sub
End If
End With

' 出力ファイルの名前を指定します。
Const outputFileName As String = "MergedPDFs.pdf"

' PDFファイル名を格納する配列を宣言します。
Dim pdfFiles() As String

' PDFファイルを検索し、配列に格納します。
pdfFiles = Filter(Split(CreateObject("WScript.Shell").Exec("CMD /C DIR """ & folderPath & "*.pdf"" /B /S").StdOut.ReadAll, vbCrLf), ".pdf", True)

' cubePDFのコマンドラインを構築します。
Dim commandLine As String
commandLine = """" & cubePDFPath & """ -merge"

' PDFファイルのパスをコマンドラインに追加します。
Dim i As Integer
For i = 0 To UBound(pdfFiles)
commandLine = commandLine & " """ & pdfFiles(i) & """"
Next i

' 出力ファイルのパスをコマンドラインに追加します。
commandLine = commandLine & " """ & folderPath & "\" & outputFileName & """"

' cubePDFを実行してPDFファイルをマージします。
CreateObject("WScript.Shell").Run commandLine, 0, True

' 出力完了メッセージを表示します。
MsgBox "PDFファイルのマージが完了しました。", vbInformation, "処理完了"

End Sub