質問編集履歴

1

コードを記載させていただきました。 参照させて頂いたページは補足情報欄に記載しています

2021/10/04 08:58

投稿

takahiro_y2j
takahiro_y2j

スコア0

test CHANGED
File without changes
test CHANGED
@@ -30,26 +30,646 @@
30
30
 
31
31
 
32
32
 
33
+ Option Explicit
34
+
35
+
36
+
37
+ Const MAINSHEETNAME As String = "メイン"
38
+
39
+ Const SEARCHCELLRNG As String = "H3"
40
+
41
+ Const HEADERROW As Integer = 5
42
+
43
+ Const FOLDERCOL As String = "H"
44
+
45
+ Const FILENMCOL As String = "I"
46
+
47
+ Const RESULTCOL As String = "J"
48
+
49
+ Const CHECK_OK As String = "パスワード保護OK"
50
+
51
+ Const CHECK_NG As String = "パスワード保護NG"
52
+
53
+ Const NO_CHECK As String = "チェック対象外"
54
+
55
+ Const CHECK_ERROR As String = "チェックエラー"
56
+
57
+ Const MSG_EXCEL As String = "入力したパスワードが間違っています。"
58
+
59
+ Const MSG_PPT As String = "読み取りパスワードをもう一度入力してください"
60
+
61
+ Const MSG_WORD As String = "パスワードが正しくありません。"
62
+
63
+ Const MSG_PDF As String = "パスワードが正しくありません。"
64
+
65
+ Const MSG_ZIP As String = "入力したパスワードが間違っています。"
66
+
67
+
68
+
69
+ ' パスワードチェック
70
+
71
+ Sub passCheck()
72
+
73
+
74
+
75
+ Dim mainSheet As Worksheet
76
+
77
+ Set mainSheet = Worksheets(MAINSHEETNAME)
78
+
79
+ Dim objFSO As Object
80
+
81
+ Set objFSO = CreateObject("Scripting.FileSystemObject")
82
+
83
+
84
+
85
+ ' チェック対象フォルダパス取得
86
+
87
+ Dim folderPath As String, folderExist As String
88
+
89
+
90
+
91
+ folderPath = mainSheet.Range(SEARCHCELLRNG).Value
92
+
93
+ folderExist = Dir(folderPath, vbDirectory)
94
+
95
+
96
+
97
+ ' フォルダ存在チェック
98
+
99
+ If folderExist = "" Then
100
+
101
+ MsgBox "チェック対象のフォルダが存在しません。" & vbCrLf & _
102
+
103
+ "処理を終了します。", vbExclamation
104
+
105
+ GoTo passCheckErr1
106
+
107
+ End If
108
+
109
+
110
+
111
+ ' ファイル一覧初期化
112
+
113
+ Call listClear(mainSheet)
114
+
115
+ ' ファイル一覧取得
116
+
117
+ Call FileSearch(objFSO.GetFolder(folderPath))
118
+
119
+
120
+
121
+ ' 最下行取得
122
+
123
+ Dim maxRow As Integer
124
+
125
+ If mainSheet.Range(FOLDERCOL & (HEADERROW + 1)).Value = "" Then
126
+
127
+ MsgBox "ファイルなしエラー"
128
+
129
+ GoTo passCheckErr1
130
+
131
+ Else
132
+
133
+ maxRow = mainSheet.Range(FOLDERCOL & HEADERROW).End(xlDown).Row
134
+
135
+ End If
136
+
137
+
138
+
139
+ ' パスワードチェック
140
+
141
+ Dim i As Integer
142
+
143
+ For i = HEADERROW + 1 To maxRow
144
+
145
+ ' チェック結果格納用
146
+
147
+ ' 1:チェックOK, 2:チェックNG, 3:チェック対象外ファイル
148
+
149
+ Dim checkResult As Integer
150
+
151
+
152
+
153
+ With mainSheet
154
+
155
+ ' ファイルパス取得
156
+
157
+ Dim f As String
158
+
159
+ f = .Range(FOLDERCOL & i).Value & "\" & .Range(FILENMCOL & i).Value
160
+
161
+
162
+
163
+ ' パスワードチェック
164
+
165
+ checkResult = IsLockedFile(f)
166
+
167
+
168
+
169
+ ' 結果記入
170
+
171
+ Select Case checkResult
172
+
173
+ Case 1
174
+
175
+ .Range(RESULTCOL & i).Value = CHECK_OK
176
+
177
+ Case 2
178
+
179
+ .Range(RESULTCOL & i).Value = CHECK_NG
180
+
181
+ .Range(RESULTCOL & i).Interior.Color = RGB(255, 0, 0)
182
+
183
+ Case 3
184
+
185
+ .Range(RESULTCOL & i).Value = NO_CHECK
186
+
187
+ .Range(RESULTCOL & i).Interior.Color = RGB(255, 255, 0)
188
+
189
+ Case Else
190
+
191
+ .Range(RESULTCOL & i).Value = CHECK_ERROR
192
+
193
+ .Range(RESULTCOL & i).Interior.Color = RGB(243, 152, 0)
194
+
195
+ End Select
196
+
197
+
198
+
199
+ End With
200
+
201
+
202
+
203
+ Next
204
+
205
+
206
+
207
+ passCheckErr1:
208
+
209
+ Set mainSheet = Nothing
210
+
211
+ Set objFSO = Nothing
212
+
213
+
214
+
215
+ MsgBox "パスワードチェックが完了しました。"
216
+
217
+
218
+
219
+ End Sub
220
+
221
+
222
+
223
+ ' ファイル一覧取得&記入
224
+
225
+ Sub FileSearch(ByVal folderPath As String)
226
+
227
+
228
+
229
+ Dim objFSO As Object
230
+
231
+ Set objFSO = CreateObject("Scripting.FileSystemObject")
232
+
233
+ Dim mainSheet As Worksheet
234
+
235
+ Set mainSheet = Worksheets(MAINSHEETNAME)
236
+
237
+
238
+
239
+ Dim objFolder, objSubFolders As Object
240
+
241
+ Set objFolder = objFSO.GetFolder(folderPath)
242
+
243
+ Set objSubFolders = objFolder.SubFolders
244
+
245
+
246
+
247
+ On Error Resume Next
248
+
249
+
250
+
251
+ Dim sf As Object
252
+
253
+ For Each sf In objSubFolders
254
+
255
+ FileSearch sf
256
+
257
+ Next
258
+
259
+ Set sf = Nothing
260
+
261
+
262
+
263
+ Dim f As Object
264
+
265
+ Dim rowNum, maxRow As Integer
266
+
267
+
268
+
269
+ ' 最下行取得
270
+
271
+ If mainSheet.Range(FOLDERCOL & (HEADERROW + 1)).Value = "" Then
272
+
273
+ maxRow = HEADERROW
274
+
275
+ Else
276
+
277
+ maxRow = mainSheet.Range(FOLDERCOL & HEADERROW).End(xlDown).Row
278
+
279
+ End If
280
+
281
+ rowNum = maxRow + 1
282
+
283
+
284
+
285
+ For Each f In objFolder.Files
286
+
287
+ With mainSheet
288
+
289
+ .Hyperlinks.Add Anchor:=.Range(FOLDERCOL & rowNum), _
290
+
291
+ Address:=objFSO.GetParentFolderName(f.Path), _
292
+
293
+ TextToDisplay:=objFSO.GetParentFolderName(f.Path)
294
+
295
+ .Hyperlinks.Add Anchor:=.Range(FILENMCOL & rowNum), _
296
+
297
+ Address:=f.Path, _
298
+
299
+ TextToDisplay:=objFSO.GetFileName(f.Path)
300
+
301
+ End With
302
+
303
+ rowNum = rowNum + 1
304
+
305
+ Next
306
+
307
+ Set f = Nothing
308
+
309
+
310
+
311
+ Set objSubFolders = Nothing
312
+
313
+ Set objFolder = Nothing
314
+
315
+ Set mainSheet = Nothing
316
+
317
+ Set objFSO = Nothing
318
+
319
+
320
+
321
+ End Sub
322
+
323
+
324
+
325
+
326
+
327
+ Private Sub listClear(ByVal sh As Worksheet)
328
+
329
+
330
+
331
+ ' セル一覧の最下行を取得し、セルをクリア
332
+
333
+ Dim maxRow As Integer
334
+
335
+
336
+
337
+ maxRow = sh.Range(FOLDERCOL & HEADERROW).End(xlDown).Row
338
+
339
+ sh.Range(FOLDERCOL & (HEADERROW + 1) & ":" & RESULTCOL & maxRow).Clear
340
+
341
+ sh.Range(FOLDERCOL & (HEADERROW + 1) & ":" & RESULTCOL & maxRow).Font.Name = "メイリオ"
342
+
343
+ sh.Range(FOLDERCOL & (HEADERROW + 1) & ":" & RESULTCOL & maxRow).Font.Size = 10
344
+
345
+
346
+
347
+ End Sub
348
+
349
+
350
+
351
+
352
+
353
+ 'パスワード保護されているブックで TRUE を返す
354
+
355
+ Function IsLockedFile(ByVal tgtPath As String) As Integer
356
+
357
+
358
+
359
+ Dim errDescription As String
360
+
361
+ Dim errNum As Long
362
+
363
+
364
+
365
+ Dim objFSO, objShell As Object
366
+
367
+ Set objFSO = CreateObject("Scripting.FileSystemObject")
368
+
369
+ Set objShell = CreateObject("Shell.Application")
370
+
371
+
372
+
373
+ Dim cnsMsg As String, ext As String, skipFlg As Boolean: skipFlg = False
374
+
375
+ ext = objFSO.GetExtensionName(tgtPath)
376
+
377
+
378
+
379
+ On Error Resume Next
380
+
381
+
382
+
383
+ Select Case ext
384
+
385
+ Case "xls", "xlsx", "xlsm"
386
+
387
+ cnsMsg = MSG_EXCEL
388
+
389
+
390
+
391
+ Dim objExcel, wb As Object
392
+
393
+ Set objExcel = CreateObject("Excel.Application")
394
+
395
+ Set wb = objExcel.Workbooks.Open(tgtPath, Password:=vbNullString)
396
+
397
+
398
+
399
+ errDescription = Err.Description
400
+
401
+ errNum = Err.Number
402
+
403
+
404
+
405
+ objExcel.DisplayAlart = False
406
+
407
+ wb.Close (False)
408
+
409
+ objExcel.DisplayAlart = True
410
+
411
+
412
+
413
+ Set wb = Nothing
414
+
415
+ objExcel.Quit
416
+
417
+ Set objExcel = Nothing
418
+
419
+
420
+
421
+ Case "ppt", "pptx", "pptm"
422
+
423
+ cnsMsg = MSG_PPT
424
+
425
+
426
+
427
+ Dim p, ppt As Object
428
+
429
+ Set p = CreateObject("PowerPoint.Application")
430
+
431
+ Set ppt = p.Presentations.Open(tgtPath & "::unknown", WithWindow:=msoFalse)
432
+
433
+
434
+
435
+ errDescription = Err.Description
436
+
437
+ errNum = Err.Number
438
+
439
+
440
+
441
+ ppt.Close
442
+
443
+ Set ppt = Nothing
444
+
445
+ p.Quit
446
+
447
+ Set p = Nothing
448
+
449
+
450
+
451
+ Case "doc", "docx", "docm"
452
+
453
+ cnsMsg = MSG_WORD
454
+
455
+
456
+
457
+ Dim wd, doc As Object
458
+
459
+ Set wd = CreateObject("Word.Application")
460
+
461
+ Set doc = wd.Documents.Open(tgtPath, passworddocument:="unknown", Visible:=False)
462
+
463
+
464
+
465
+ errDescription = Err.Description
466
+
467
+ errNum = Err.Number
468
+
469
+
470
+
471
+ doc.Close
472
+
473
+ Set doc = Nothing
474
+
475
+ wd.Quit
476
+
477
+ Set wd = Nothing
478
+
479
+
480
+
481
+ Case "pdf"
482
+
483
+ ' Excelのハイパーリンク押下⇒開いて確認で回避?
484
+
485
+
486
+
487
+ Case "zip"
488
+
489
+ Dim folderPath As String
490
+
491
+ folderPath = objFSO.GetParentFolderName(tgtPath)
492
+
493
+
494
+
495
+ ' 作業用フォルダ作成
496
+
497
+ Dim mkDirPath As String
498
+
499
+ Dim cnt As Integer: cnt = 0
500
+
501
+ While cnt < 10
502
+
503
+ mkDirPath = folderPath & "\" & "workfolder_" & Rnd
504
+
505
+ If Dir(mkDirPath, vbDirectory) = "" Then
506
+
507
+ MkDir (mkDirPath)
508
+
509
+ cnt = 10
510
+
511
+ End If
512
+
513
+ Wend
514
+
515
+
516
+
517
+ Dim objZip As Object
518
+
519
+ Dim result As Integer
520
+
521
+ 'なぜか二重カッコが必要
522
+
523
+ '進捗ダイアログを表示しない
524
+
525
+ objShell.Namespace((mkDirPath)).CopyHere objShell.Namespace((tgtPath)).Items, &H4 + &H40 + &H400
526
+
527
+
528
+
529
+ ' 一時フォルダ内のファイル数カウント
530
+
531
+
532
+
533
+
534
+
535
+
536
+
537
+ Dim buf As String, fileCount As Long
538
+
539
+
540
+
541
+ buf = Dir(mkDirPath & "*", vbDirectory)
542
+
543
+ Do While buf <> ""
544
+
545
+ If buf <> "." And buf <> ".." Then
546
+
547
+ fileCount = fileCount + 1
548
+
549
+ End If
550
+
551
+ buf = Dir()
552
+
553
+ Loop
554
+
555
+
556
+
557
+ Select Case fileCount
558
+
559
+ Case Is > 0
560
+
561
+ ' パスワードチェックNG
562
+
563
+ IsLockedFile = 2
564
+
565
+ Case Is = 0
566
+
567
+ ' パスワードチェックOK
568
+
569
+ IsLockedFile = 1
570
+
571
+ Case Else
572
+
573
+ ' パスワードチェックエラー
574
+
575
+ IsLockedFile = 4
576
+
577
+ End Select
578
+
579
+
580
+
581
+ ' 一時フォルダ削除
582
+
583
+ objFSO.DeleteFolder (mkDirPath)
584
+
585
+
586
+
587
+ skipFlg = True
588
+
589
+
590
+
591
+ Case Else
592
+
593
+ ' チェック対象外ファイルの場合
594
+
595
+
596
+
597
+ End Select
598
+
599
+
600
+
601
+ On Error GoTo 0
602
+
603
+
604
+
605
+ ' zipファイルチェック以外の場合のみ実行
606
+
607
+ If skipFlg = False Then
608
+
609
+ ' 対象外フォイルの場合
610
+
611
+ If cnsMsg = "" Then
612
+
613
+ IsLockedFile = 3
614
+
615
+ GoTo IsLockedFileClose
616
+
617
+ End If
618
+
619
+
620
+
621
+ If InStr(errDescription, cnsMsg) > 0 Then
622
+
623
+ ' パスワードチェックOK
624
+
625
+ IsLockedFile = 1
626
+
627
+
628
+
629
+ ElseIf Err.Number = 0 Then
630
+
631
+
632
+
633
+ ' パスワードチェックNG
634
+
635
+ IsLockedFile = 2
636
+
637
+
638
+
639
+ Else
640
+
641
+ Err.Raise errNum, , errDescription
642
+
643
+ End If
644
+
645
+ End If
646
+
647
+
648
+
649
+ IsLockedFileClose:
650
+
651
+ Set objShell = Nothing
652
+
653
+ Set objFSO = Nothing
654
+
655
+
656
+
657
+ End Function
658
+
659
+
660
+
661
+
662
+
663
+ ### 補足情報(FW/ツールのバージョンなど)
664
+
665
+
666
+
667
+ 参照させていただいたのは下記サイト様です。
668
+
669
+
670
+
33
671
  https://qiita.com/irohamaru/items/6021327a5c39422fa2f4
34
672
 
35
673
 
36
674
 
37
675
  https://teratail.com/questions/240188
38
-
39
-
40
-
41
- ```
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
- ### 補足情報(FW/ツールのバージョンなど)
52
-
53
-
54
-
55
- ここにより詳