前提・実現したいこと
プログラミング初心者です。質問に誤り・不足等あればご指摘ください。
PDFファイルをVBAで結合したいと考えています。インストールされているソフトはJustPDFです。
結合までの手順は以下のとおりです。
(前提)
「サンプル」というフォルダに次のようなファイル名のPDFファイルが複数格納されている
・サンプルA_○○(例 サンプルA_りんご、サンプルA_みかん…)
・サンプルB_○○(例 サンプルB_りんご、サンプルB_みかん…)
ただし、「サンプル」フォルダの格納場所は任意
・VBAを入れているExcel(「差込み」シート)に「○○」に該当する部分のリストが入力してあり、結合したい名前の隣の列に「1」が入力してある
①「サンプル」フォルダのファイルパスを取得する
②結合したファイルを格納するフォルダのファイルパスを取得する
③「サンプル」フォルダ内から「○○」部分の一致しているPDFを探し、結合、②で指定したフォルダに結合結果を保存する
(例 サンプルA_りんご、サンプルB_りんご → サンプル_りんご)
ExcelのVBAで作成しようとしていましたが、Excelでなくとも構いません。改善点、そもそも根本的に間違っているようであれば
実現するためのアドバイス等ご教示ください。
知識・理解が全く足りていないことは自覚しています。ご容赦ください。
(追記)
コードを実行すると、エラーは出ませんが結合もされることなく終了してしまいます。
myShell.Run (strCmd) のところで一瞬だけ黒いウィンドウが表示されるので何かが実行されているのだとは思うのですが…
試したこと
以下に、試行錯誤の結果を記載しておきます(あれこれ調べながら作るうちにわけのわからなくなったもので、使っていない変数の宣言などが残っていますが…)。
コマンドラインを呼び出して実行する、という方向で試してみていました。
VBAを実行したところ、エラーは出ませんでしたが、PDFの結合は実行されませんでした。
VBA
1Option Explicit 2 3Sub サンプルAとサンプルBを結合() 4 5'Application.ScreenUpdating = False 6 7 '結合する 8 Dim StrScr As Boolean 9 Dim strCmd As String 10 Dim strWorkFolder As String 11 Dim IErrCount As Long 12 Dim IErrCode(4) As Long 13 Dim strOutFile(1) As String 14 Dim strErr As String 15 Dim IRetCode As Long 16 StrScr = RunCommandLineEX(strCmd, strWorkFolder, IErrCount, _ 17 IErrCode, strOutFile, strErr, IRetCode) 18 19'Application.ScreenUpdating = true 20 21End Sub 22 23Public Function RunCommandLineEX( _ 24 ByVal strincommand As String, _ 25 ByVal strInWorkFolder As String, _ 26 ByVal IInErrCount As Long, _ 27 ByRef IInErrCode() As Long, _ 28 ByRef strOutFile() As String, _ 29 ByRef strOutErrMsg As String, _ 30 ByRef IOutRetCode As Long) As Boolean 31 32'◆―◆―◆―◆―◆―◆―◆―◆―◆―◆―◆―◆ 33'サンプルAとサンプルBを格納してあるフォルダを選択する 34Dim dlg As FileDialog 35Dim fp As String 36 37Set dlg = Application.FileDialog(msoFileDialogFolderPicker) 38 39 If dlg.Show = False Then 40 Exit Function 41 End If 42 43 fp = dlg.SelectedItems(1) & "\" 44 45'◆―◆―◆―◆―◆―◆―◆―◆―◆―◆―◆―◆ 46'保存先を選択する 47Dim dlg2 As FileDialog 48Dim fp2 As String 49 50Set dlg2 = Application.FileDialog(msoFileDialogFolderPicker) 51 52 If dlg2.Show = False Then 53 Exit Function 54 End If 55 56 fp2 = dlg.SelectedItems(1) '& "\" 57 58'◆―◆―◆―◆―◆―◆―◆―◆―◆―◆―◆―◆ 59'Excelの設定 60Dim LastRow, i As Long 61Dim str, str2 As String 62Dim SK As Worksheet 63Set SK = ThisWorkbook.Worksheets("差込み") 64LastRow = SK.Cells(Rows.Count, "D").End(xlUp).Row 'フィルター列 65 66For i = 2 To LastRow 67If SK.Range("D" & i).Value <> "" Then 68 str = "サンプルA_" & SK.Range("B" & i).Value 'ファイル名 69 str2 = "サンプルB_" & SK.Range("B" & i).Value 'ファイル名 70 71 '◆―◆―◆―◆―◆―◆―◆―◆―◆―◆―◆―◆ 72 '結合するファイルを変数に格納する 73 'サンプルAを変数に格納する 74 Dim KT As String 75 If Dir(fp & str & ".pdf") <> "" Then 76 KT = fp & str & ".pdf" 77 Else 78 MsgBox (fp & str & ".pdf" & "が見つかりませんでした。") 79 Exit Function 80 End If 81 82 'サンプルBを変数に格納する 83 Dim KB As String 84 If Dir(fp & str2 & ".pdf") <> "" Then 85 KB = fp & str2 & ".pdf" 86 Else 87 MsgBox (fp & str2 & ".pdf" & "が見つかりませんでした。") 88 Exit Function 89 End If 90 91 92'引数で使う変数 93Dim StrScr As Boolean 94Dim strCmd As String 95Dim strWorkFolder As String 96Dim IErrCount As Long 97Dim IErrCode(4) As Long 98'Dim strOutFile(1) As String 99Dim strErr As String 100Dim IRetCode As Long 101 102strCmd = "cmd /c PdfCmdCreator.exe /combine " & Chr(34) & KT & Chr(34) & Chr(32) & Chr(34) & KB & Chr(34) & " /out " & Chr(34) & fp2 & Chr(34) 103 104Dim myShell As Object 105Set myShell = CreateObject("WScript.Shell") 106myShell.Run (strCmd) '実行 107 108End If 109Next 110 111End Function

回答1件
あなたの回答
tips
プレビュー