回答編集履歴

1

処理修正

2015/06/10 05:09

投稿

bbs
bbs

スコア16

test CHANGED
@@ -136,203 +136,275 @@
136
136
 
137
137
 
138
138
 
139
- Dim OutPutFile As Boolean
140
-
141
139
  ' GetFolderでパスを取得しAdoに格納
142
140
 
141
+ Dim file As Object
142
+
143
143
  For Each file In Fso.GetFolder(strPathName).Files
144
144
 
145
-
146
-
147
- ' 除外ファイルチェック
145
+ ' 登録ファイルチェック
148
-
146
+
149
- OutPutFile = True
147
+ If IsOutPutFile(file) Then
148
+
150
-
149
+ ' ファイル名は以下の形式
150
+
151
- For i = 0 To ListBox1.ListCount - 1
151
+ ' (番号)_(○○○)_(×××).xls
152
+
152
-
153
+ ado.AddNew
154
+
153
- If Dir(file) Like ("*" & ListBox1.List(i) & "*") Then
155
+ ado.Fields(0) = padZero(CInt(Split(Dir(file), "_")(0)), 4) ' 番号(0埋)
154
-
156
+
155
- OutPutFile = Fasle ' 除外ァイ名が含まれていた場合は登録フラグOFF
157
+ ado.Fields(1) = file ' フルPATH
158
+
156
-
159
+ ado.Update
160
+
157
- Exit For
161
+ End If
162
+
163
+ Next
164
+
165
+
166
+
167
+ ado.Sort = "FILENUMBER ASC" ' FILENUMBERでソート
168
+
169
+ ado.MoveFirst
170
+
171
+
172
+
173
+ Do Until ado.EOF
174
+
175
+ xlApp.Workbooks.Open FileName:=CStr(ado.Fields(1)), UpdateLinks:=0 ' Excelを開く
176
+
177
+ DoEvents
178
+
179
+ If IsCancel Then
180
+
181
+ If IsOpenBook(CStr(ado.Fields(1))) And Not xlApp Is Nothing Then
182
+
183
+ xlApp.DisplayAlerts = False
184
+
185
+ xlApp.Workbooks.Close ' Excelを閉じる
186
+
187
+ xlApp.DisplayAlerts = True
158
188
 
159
189
  End If
160
190
 
161
- Next
162
-
163
- If OutPutFile Then
191
+ If Not Fso Is Nothing Then Set Fso = Nothing
164
-
165
- ado.AddNew
192
+
166
-
167
- ' ファイル名は以下の形式
193
+ If Not ado Is Nothing Then Set ado = Nothing
168
-
194
+
169
- ' (番号)_(○○○)_(×××).xls
195
+ If Not xlApp Is Nothing Then Set xlApp = Nothing
170
-
196
+
171
- ado.Fields(0) = padZero(CInt(Split(Dir(file), "_")(0)), 4) ' 番号(0埋)
197
+ MsgBox "処理を中断しました。", vbInformation, "処理中断"
172
-
173
- ado.Fields(1) = file ' フルPATH
198
+
174
-
175
- ado.Update
199
+ Exit Sub
176
200
 
177
201
  End If
178
202
 
203
+ xlApp.Visible = False ' 非表示
204
+
205
+ xlApp.ActiveWorkbook.PrintOut ' 印刷
206
+
207
+ xlApp.DisplayAlerts = False
208
+
209
+ xlApp.Workbooks.Close ' Excelを閉じる
210
+
211
+ xlApp.DisplayAlerts = True
212
+
213
+ Sleep 1 ' CPU使用率考慮
214
+
215
+ ado.MoveNext
216
+
217
+ Loop
218
+
219
+
220
+
221
+ ado.Close
222
+
223
+ Set xlApp = Nothing
224
+
225
+ Set ado = Nothing
226
+
227
+ Set Fso = Nothing
228
+
229
+
230
+
231
+ MsgBox "印刷ジョブに登録しました。", vbInformation, "処理終了"
232
+
233
+ Exit Sub
234
+
235
+ ErrorHandler:
236
+
237
+ If Not Fso Is Nothing Then Set Fso = Nothing
238
+
239
+ If Not ado Is Nothing Then Set ado = Nothing
240
+
241
+ If Not xlApp Is Nothing Then Set xlApp = Nothing
242
+
243
+ MsgBox Err.Number & ":" & Err.Description, vbCritical & vbOKOnly, "例外発生"
244
+
245
+ End Sub
246
+
247
+
248
+
249
+ '''
250
+
251
+ ''' キャンセルボタン
252
+
253
+ '''
254
+
255
+ Private Sub CommandButton4_Click()
256
+
257
+ IsCancel = True
258
+
259
+ End Sub
260
+
261
+
262
+
263
+ '''
264
+
265
+ ''' 閉じるボタン
266
+
267
+ '''
268
+
269
+ Private Sub CommandButton2_Click()
270
+
271
+ Unload UserForm1
272
+
273
+ End Sub
274
+
275
+
276
+
277
+ '''
278
+
279
+ ''' 追加ボタン
280
+
281
+ '''
282
+
283
+ Private Sub CommandButton3_Click()
284
+
285
+ Dim i As Integer
286
+
287
+ For i = 0 To ListBox1.ListCount - 1
288
+
289
+ If ListBox1.List(i) = TextBox1.Text Then
290
+
291
+ MsgBox "既に登録済みです。", vbCritical & vbOKOnly, "重複エラー"
292
+
293
+ Exit Sub
294
+
295
+ End If
296
+
179
297
  Next
180
298
 
181
-
182
-
183
- ado.Sort = "FILENUMBER ASC" ' FILENUMBERでソート
184
-
185
- ado.MoveFirst
186
-
187
-
188
-
189
- Do Until ado.EOF
190
-
191
- xlApp.Workbooks.Open FileName:=CStr(ado.Fields(1)), UpdateLinks:=0 ' Excelを開く
192
-
193
- DoEvents
194
-
195
- If IsCancel Then
196
-
197
- If IsOpenBook(CStr(ado.Fields(1))) And Not xlApp Is Nothing Then
198
-
199
- xlApp.DisplayAlerts = False
200
-
201
- xlApp.Workbooks.Close ' Excelを閉じる
202
-
203
- xlApp.DisplayAlerts = True
299
+ ListBox1.AddItem TextBox1.Text
300
+
204
-
301
+ TextBox1.Text = ""
302
+
303
+ SetListData
304
+
205
- End If
305
+ End Sub
306
+
307
+
308
+
206
-
309
+ '''
310
+
311
+ ''' 削除ボタン
312
+
313
+ '''
314
+
315
+ Private Sub CommandButton5_Click()
316
+
317
+ Dim i As Integer
318
+
207
- If Not ado Is Nothing Then Set ado = Nothing
319
+ For i = 0 To ListBox1.ListCount - 1
208
-
320
+
209
- If Not xlApp Is Nothing Then Set xlApp = Nothing
321
+ If ListBox1.Selected(i) Then
210
-
322
+
211
- MsgBox "処理を中断しました。", vbInformation, "処理中断"
323
+ ListBox1.RemoveItem (i)
212
-
324
+
213
- Exit Sub
325
+ Exit For
214
326
 
215
327
  End If
216
328
 
217
- xlApp.Visible = False ' 非表示
218
-
219
- xlApp.ActiveWorkbook.PrintOut ' 印刷
220
-
221
- xlApp.DisplayAlerts = False
222
-
223
- xlApp.Workbooks.Close ' Excelを閉じる
224
-
225
- xlApp.DisplayAlerts = True
226
-
227
- Sleep 1 ' CPU使用率考慮
228
-
229
- ado.MoveNext
230
-
231
- Loop
232
-
233
-
234
-
235
- ado.Close
236
-
237
- Set xlApp = Nothing
238
-
239
- Set ado = Nothing
240
-
241
- Set Fso = Nothing
242
-
243
-
244
-
245
- MsgBox "印刷ジョブに登録しました。", vbInformation, "処理終了"
246
-
247
- Exit Sub
248
-
249
- ErrorHandler:
250
-
251
- If Not ado Is Nothing Then Set ado = Nothing
252
-
253
- If Not xlApp Is Nothing Then Set xlApp = Nothing
254
-
255
- MsgBox Err.Number & ":" & Err.Description, vbCritical & vbOKOnly, "例外発生"
256
-
257
- End Sub
258
-
259
-
260
-
261
- '''
262
-
263
- ''' キャンセルボタン
264
-
265
- '''
266
-
267
- Private Sub CommandButton4_Click()
268
-
269
- IsCancel = True
270
-
271
- End Sub
272
-
273
-
274
-
275
- '''
276
-
277
- ''' 閉じるボタン
278
-
279
- '''
280
-
281
- Private Sub CommandButton2_Click()
282
-
283
- Unload UserForm1
284
-
285
- End Sub
286
-
287
-
288
-
289
- '''
290
-
291
- ''' 追加ボタン
292
-
293
- '''
294
-
295
- Private Sub CommandButton3_Click()
296
-
297
- Dim i As Integer
329
+ Next
330
+
331
+ SetListData
332
+
333
+ End Sub
334
+
335
+
336
+
337
+ '''
338
+
339
+ ''' ゼロ埋め処理
340
+
341
+ '''
342
+
343
+ Private Function padZero(n As Integer, keta As Integer)
344
+
345
+ padZero = Right(n + 10 ^ keta, keta)
346
+
347
+ End Function
348
+
349
+
350
+
351
+ '''
352
+
353
+ ''' Bookが開かれているか判別
354
+
355
+ '''
356
+
357
+ Private Function IsOpenBook(strFullPath As String) As Boolean
358
+
359
+ On Error Resume Next
360
+
361
+ Open strFullPath For Append As #1
362
+
363
+ Close #1
364
+
365
+ IsOpenBook = Err.Number > 0
366
+
367
+ End Function
368
+
369
+
370
+
371
+ '''
372
+
373
+ ''' 除外ファイル名登録
374
+
375
+ '''
376
+
377
+ Private Sub SetListData()
378
+
379
+ Worksheets(2).Columns("A").Delete
298
380
 
299
381
  For i = 0 To ListBox1.ListCount - 1
300
382
 
301
- If ListBox1.List(i) = TextBox1.Text Then
383
+ Worksheets(2).Cells(i + 1, 1).Value = ListBox1.List(i)
302
-
303
- MsgBox "既に登録済みです。", vbCritical & vbOKOnly, "重複エラー"
304
-
305
- Exit Sub
306
-
307
- End If
308
384
 
309
385
  Next
310
386
 
311
- ListBox1.AddItem TextBox1.Text
387
+ ThisWorkbook.Save
312
-
313
- TextBox1.Text = ""
388
+
314
-
315
- SetListData
316
-
317
- End Sub
389
+ End Sub
318
-
319
-
320
-
390
+
391
+
392
+
321
- '''
393
+ '''
322
-
394
+
323
- ''' 削除ボタン
395
+ ''' 登録ファイルチェック
324
-
396
+
325
- '''
397
+ '''
326
-
398
+
327
- Private Sub CommandButton5_Click()
399
+ Private Function IsOutPutFile(file As Object) As Boolean
328
-
400
+
329
- Dim i As Integer
401
+ IsOutPutFile = True
330
402
 
331
403
  For i = 0 To ListBox1.ListCount - 1
332
404
 
333
- If ListBox1.Selected(i) Then
405
+ If Dir(file) Like ("*" & ListBox1.List(i) & "*") Then
334
-
406
+
335
- ListBox1.RemoveItem (i)
407
+ IsOutPutFile = False ' 除外ファイル名が含まれていた場合は登録フラグOFF
336
408
 
337
409
  Exit For
338
410
 
@@ -340,68 +412,8 @@
340
412
 
341
413
  Next
342
414
 
343
- SetListData
344
-
345
- End Sub
346
-
347
-
348
-
349
- '''
350
-
351
- ''' ゼロ埋め処理
352
-
353
- '''
354
-
355
- Private Function padZero(n As Integer, keta As Integer)
356
-
357
- padZero = Right(n + 10 ^ keta, keta)
358
-
359
415
  End Function
360
416
 
361
417
 
362
418
 
363
- '''
364
-
365
- ''' Bookが開かれているか判別
366
-
367
- '''
368
-
369
- Private Function IsOpenBook(strFullPath As String) As Boolean
370
-
371
- On Error Resume Next
372
-
373
- Open strFullPath For Append As #1
374
-
375
- Close #1
376
-
377
- IsOpenBook = Err.Number > 0
378
-
379
- End Function
380
-
381
-
382
-
383
- '''
384
-
385
- ''' 除外文字列登録
386
-
387
- '''
388
-
389
- Private Sub SetListData()
390
-
391
- Worksheets(2).Columns("A").Delete
392
-
393
- For i = 0 To ListBox1.ListCount - 1
394
-
395
- Worksheets(2).Cells(i + 1, 1).Value = ListBox1.List(i)
396
-
397
- Next
398
-
399
- ThisWorkbook.Save
400
-
401
- End Sub
402
-
403
-
404
-
405
-
406
-
407
419
  ```