質問編集履歴

2

見やすくしました。

2019/07/23 11:18

投稿

sedfjkhlaQ
sedfjkhlaQ

スコア13

test CHANGED
File without changes
test CHANGED
@@ -50,270 +50,240 @@
50
50
 
51
51
 
52
52
 
53
-
53
+ ### 試したこと
54
+
55
+
56
+
54
-
57
+ 以下に、試行錯誤の結果を記載しておきます(あれこれ調べながら作るうちにわけのわからなくなったもので、使っていない変数の宣言などが残っていますが…)。
58
+
59
+ コマンドラインを呼び出して実行する、という方向で試してみていました。
60
+
61
+ VBAを実行したところ、エラーは出ませんでしたが、PDFの結合は実行されませんでした。
62
+
63
+
64
+
65
+ ```VBA
66
+
67
+ Option Explicit
68
+
69
+
70
+
71
+ Sub サンプルAとサンプルBを結合()
72
+
73
+
74
+
75
+ 'Application.ScreenUpdating = False
76
+
77
+
78
+
79
+ '結合する
80
+
81
+ Dim StrScr As Boolean
82
+
83
+ Dim strCmd As String
84
+
85
+ Dim strWorkFolder As String
86
+
87
+ Dim IErrCount As Long
88
+
89
+ Dim IErrCode(4) As Long
90
+
91
+ Dim strOutFile(1) As String
92
+
93
+ Dim strErr As String
94
+
95
+ Dim IRetCode As Long
96
+
97
+ StrScr = RunCommandLineEX(strCmd, strWorkFolder, IErrCount, _
98
+
99
+ IErrCode, strOutFile, strErr, IRetCode)
100
+
101
+
102
+
103
+ 'Application.ScreenUpdating = true
104
+
105
+
106
+
107
+ End Sub
108
+
109
+
110
+
111
+ Public Function RunCommandLineEX( _
112
+
113
+ ByVal strincommand As String, _
114
+
115
+ ByVal strInWorkFolder As String, _
116
+
117
+ ByVal IInErrCount As Long, _
118
+
119
+ ByRef IInErrCode() As Long, _
120
+
121
+ ByRef strOutFile() As String, _
122
+
123
+ ByRef strOutErrMsg As String, _
124
+
125
+ ByRef IOutRetCode As Long) As Boolean
126
+
127
+
128
+
129
+ '◆―◆―◆―◆―◆―◆―◆―◆―◆―◆―◆―◆
130
+
131
+ 'サンプルAとサンプルBを格納してあるフォルダを選択する
132
+
133
+ Dim dlg As FileDialog
134
+
135
+ Dim fp As String
136
+
137
+
138
+
139
+ Set dlg = Application.FileDialog(msoFileDialogFolderPicker)
140
+
141
+
142
+
143
+ If dlg.Show = False Then
144
+
145
+ Exit Function
146
+
147
+ End If
148
+
149
+
150
+
151
+ fp = dlg.SelectedItems(1) & "\"
152
+
153
+
154
+
155
+ '◆―◆―◆―◆―◆―◆―◆―◆―◆―◆―◆―◆
156
+
157
+ '保存先を選択する
158
+
159
+ Dim dlg2 As FileDialog
160
+
161
+ Dim fp2 As String
162
+
163
+
164
+
165
+ Set dlg2 = Application.FileDialog(msoFileDialogFolderPicker)
166
+
167
+
168
+
169
+ If dlg2.Show = False Then
170
+
171
+ Exit Function
172
+
173
+ End If
174
+
175
+
176
+
177
+ fp2 = dlg.SelectedItems(1) '& "\"
178
+
179
+
180
+
181
+ '◆―◆―◆―◆―◆―◆―◆―◆―◆―◆―◆―◆
182
+
183
+ 'Excelの設定
184
+
185
+ Dim LastRow, i As Long
186
+
187
+ Dim str, str2 As String
188
+
189
+ Dim SK As Worksheet
190
+
191
+ Set SK = ThisWorkbook.Worksheets("差込み")
192
+
193
+ LastRow = SK.Cells(Rows.Count, "D").End(xlUp).Row 'フィルター列
194
+
195
+
196
+
197
+ For i = 2 To LastRow
198
+
199
+ If SK.Range("D" & i).Value <> "" Then
200
+
201
+ str = "サンプルA_" & SK.Range("B" & i).Value 'ファイル名
202
+
203
+ str2 = "サンプルB_" & SK.Range("B" & i).Value 'ファイル名
204
+
205
+
206
+
207
+ '◆―◆―◆―◆―◆―◆―◆―◆―◆―◆―◆―◆
208
+
55
- ### 発生してい問題・エラーメッセージ
209
+ '結合すファイルを変数に格納する
210
+
56
-
211
+ 'サンプルAを変数に格納する
212
+
57
-
213
+ Dim KT As String
214
+
215
+ If Dir(fp & str & ".pdf") <> "" Then
216
+
217
+ KT = fp & str & ".pdf"
218
+
219
+ Else
220
+
221
+ MsgBox (fp & str & ".pdf" & "が見つかりませんでした。")
222
+
223
+ Exit Function
224
+
225
+ End If
226
+
227
+
228
+
229
+ 'サンプルBを変数に格納する
230
+
231
+ Dim KB As String
232
+
233
+ If Dir(fp & str2 & ".pdf") <> "" Then
234
+
235
+ KB = fp & str2 & ".pdf"
236
+
237
+ Else
238
+
239
+ MsgBox (fp & str2 & ".pdf" & "が見つかりませんでした。")
240
+
241
+ Exit Function
242
+
243
+ End If
244
+
245
+
246
+
247
+
248
+
249
+ '引数で使う変数
250
+
251
+ Dim StrScr As Boolean
252
+
253
+ Dim strCmd As String
254
+
255
+ Dim strWorkFolder As String
256
+
257
+ Dim IErrCount As Long
258
+
259
+ Dim IErrCode(4) As Long
260
+
261
+ 'Dim strOutFile(1) As String
262
+
263
+ Dim strErr As String
264
+
265
+ Dim IRetCode As Long
266
+
267
+
268
+
269
+ strCmd = "cmd /c PdfCmdCreator.exe /combine " & Chr(34) & KT & Chr(34) & Chr(32) & Chr(34) & KB & Chr(34) & " /out " & Chr(34) & fp2 & Chr(34)
270
+
271
+
272
+
273
+ Dim myShell As Object
274
+
275
+ Set myShell = CreateObject("WScript.Shell")
276
+
277
+ myShell.Run (strCmd) '実行
278
+
279
+
280
+
281
+ End If
282
+
283
+ Next
284
+
285
+
286
+
287
+ End Function
58
288
 
59
289
  ```
60
-
61
- エラーメッセージ
62
-
63
- ```
64
-
65
-
66
-
67
- ### 該当のソースコード
68
-
69
-
70
-
71
- ```ここに言語名を入力
72
-
73
- ソースコード
74
-
75
- ```
76
-
77
-
78
-
79
- ### 試したこと
80
-
81
-
82
-
83
- 以下に、試行錯誤の結果を記載しておきます(あれこれ調べながら作るうちにわけのわからなくなったもので、使っていない変数の宣言などが残っていますが…)。
84
-
85
- コマンドラインを呼び出して実行する、という方向で試してみていました。
86
-
87
- VBAを実行したところ、エラーは出ませんでしたが、PDFの結合は実行されませんでした。
88
-
89
-
90
-
91
- Option Explicit
92
-
93
-
94
-
95
- Sub サンプルAとサンプルBを結合()
96
-
97
-
98
-
99
- 'Application.ScreenUpdating = False
100
-
101
-
102
-
103
- '結合する
104
-
105
- Dim StrScr As Boolean
106
-
107
- Dim strCmd As String
108
-
109
- Dim strWorkFolder As String
110
-
111
- Dim IErrCount As Long
112
-
113
- Dim IErrCode(4) As Long
114
-
115
- Dim strOutFile(1) As String
116
-
117
- Dim strErr As String
118
-
119
- Dim IRetCode As Long
120
-
121
- StrScr = RunCommandLineEX(strCmd, strWorkFolder, IErrCount, _
122
-
123
- IErrCode, strOutFile, strErr, IRetCode)
124
-
125
-
126
-
127
- 'Application.ScreenUpdating = true
128
-
129
-
130
-
131
- End Sub
132
-
133
-
134
-
135
- Public Function RunCommandLineEX( _
136
-
137
- ByVal strincommand As String, _
138
-
139
- ByVal strInWorkFolder As String, _
140
-
141
- ByVal IInErrCount As Long, _
142
-
143
- ByRef IInErrCode() As Long, _
144
-
145
- ByRef strOutFile() As String, _
146
-
147
- ByRef strOutErrMsg As String, _
148
-
149
- ByRef IOutRetCode As Long) As Boolean
150
-
151
-
152
-
153
- '◆―◆―◆―◆―◆―◆―◆―◆―◆―◆―◆―◆
154
-
155
- 'サンプルAとサンプルBを格納してあるフォルダを選択する
156
-
157
- Dim dlg As FileDialog
158
-
159
- Dim fp As String
160
-
161
-
162
-
163
- Set dlg = Application.FileDialog(msoFileDialogFolderPicker)
164
-
165
-
166
-
167
- If dlg.Show = False Then
168
-
169
- Exit Function
170
-
171
- End If
172
-
173
-
174
-
175
- fp = dlg.SelectedItems(1) & "\"
176
-
177
-
178
-
179
- '◆―◆―◆―◆―◆―◆―◆―◆―◆―◆―◆―◆
180
-
181
- '保存先を選択する
182
-
183
- Dim dlg2 As FileDialog
184
-
185
- Dim fp2 As String
186
-
187
-
188
-
189
- Set dlg2 = Application.FileDialog(msoFileDialogFolderPicker)
190
-
191
-
192
-
193
- If dlg2.Show = False Then
194
-
195
- Exit Function
196
-
197
- End If
198
-
199
-
200
-
201
- fp2 = dlg.SelectedItems(1) '& "\"
202
-
203
-
204
-
205
- '◆―◆―◆―◆―◆―◆―◆―◆―◆―◆―◆―◆
206
-
207
- 'Excelの設定
208
-
209
- Dim LastRow, i As Long
210
-
211
- Dim str, str2 As String
212
-
213
- Dim SK As Worksheet
214
-
215
- Set SK = ThisWorkbook.Worksheets("差込み")
216
-
217
- LastRow = SK.Cells(Rows.Count, "D").End(xlUp).Row 'フィルター列
218
-
219
-
220
-
221
- For i = 2 To LastRow
222
-
223
- If SK.Range("D" & i).Value <> "" Then
224
-
225
- str = "サンプルA_" & SK.Range("B" & i).Value 'ファイル名
226
-
227
- str2 = "サンプルB_" & SK.Range("B" & i).Value 'ファイル名
228
-
229
-
230
-
231
- '◆―◆―◆―◆―◆―◆―◆―◆―◆―◆―◆―◆
232
-
233
- '結合するファイルを変数に格納する
234
-
235
- 'サンプルAを変数に格納する
236
-
237
- Dim KT As String
238
-
239
- If Dir(fp & str & ".pdf") <> "" Then
240
-
241
- KT = fp & str & ".pdf"
242
-
243
- Else
244
-
245
- MsgBox (fp & str & ".pdf" & "が見つかりませんでした。")
246
-
247
- Exit Function
248
-
249
- End If
250
-
251
-
252
-
253
- 'サンプルBを変数に格納する
254
-
255
- Dim KB As String
256
-
257
- If Dir(fp & str2 & ".pdf") <> "" Then
258
-
259
- KB = fp & str2 & ".pdf"
260
-
261
- Else
262
-
263
- MsgBox (fp & str2 & ".pdf" & "が見つかりませんでした。")
264
-
265
- Exit Function
266
-
267
- End If
268
-
269
-
270
-
271
-
272
-
273
- '引数で使う変数
274
-
275
- Dim StrScr As Boolean
276
-
277
- Dim strCmd As String
278
-
279
- Dim strWorkFolder As String
280
-
281
- Dim IErrCount As Long
282
-
283
- Dim IErrCode(4) As Long
284
-
285
- 'Dim strOutFile(1) As String
286
-
287
- Dim strErr As String
288
-
289
- Dim IRetCode As Long
290
-
291
-
292
-
293
- strCmd = "cmd /c PdfCmdCreator.exe /combine " & Chr(34) & KT & Chr(34) & Chr(32) & Chr(34) & KB & Chr(34) & " /out " & Chr(34) & fp2 & Chr(34)
294
-
295
-
296
-
297
- Dim myShell As Object
298
-
299
- Set myShell = CreateObject("WScript.Shell")
300
-
301
- myShell.Run (strCmd) '実行
302
-
303
-
304
-
305
- End If
306
-
307
- Next
308
-
309
-
310
-
311
- End Function
312
-
313
-
314
-
315
- ### 補足情報(FW/ツールのバージョンなど)
316
-
317
-
318
-
319
- ここにより詳細な情報を記載してください。

1

strCmdの、結合対象ファイル間に空白Chr(32)を追記しました。

2019/07/23 11:18

投稿

sedfjkhlaQ
sedfjkhlaQ

スコア13

test CHANGED
File without changes
test CHANGED
@@ -42,6 +42,14 @@
42
42
 
43
43
 
44
44
 
45
+ (追記)
46
+
47
+ コードを実行すると、エラーは出ませんが結合もされることなく終了してしまいます。
48
+
49
+ myShell.Run (strCmd) のところで一瞬だけ黒いウィンドウが表示されるので何かが実行されているのだとは思うのですが…
50
+
51
+
52
+
45
53
 
46
54
 
47
55
  ### 発生している問題・エラーメッセージ
@@ -282,7 +290,7 @@
282
290
 
283
291
 
284
292
 
285
- strCmd = "cmd /c PdfCmdCreator.exe /combine " & Chr(34) & KT & Chr(34) & Chr(34) & KB & Chr(34) & " /out " & Chr(34) & fp2 & Chr(34)
293
+ strCmd = "cmd /c PdfCmdCreator.exe /combine " & Chr(34) & KT & Chr(34) & Chr(32) & Chr(34) & KB & Chr(34) & " /out " & Chr(34) & fp2 & Chr(34)
286
294
 
287
295
 
288
296