Excel VBA – パスワード付きZIPファイルを解凍する

  • このエントリーをはてなブックマークに追加

パスワード付きのZIPファイルを解凍するサンプルコードです。

以下のサンプルコードは、
指定のフォルダの全てのZIPファイル(パスワードaで圧縮)に対して
自動で”a”というパスワードを入力して、
指定のフォルダに解凍するものです。

Windows標準の状態で動作します。

Sub main()

    'ファイルシステムオブジェクトの作成
    Dim FSO As Object
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    'ファイルオブジェクトの作成
    Dim fileObj As Object
    
    'シェルオブジェクトの作成
    Dim shellObj As Object
    Set shellObj = CreateObject("Shell.Application")
    
    Dim zipObj As Variant
    Dim ret As Long
    
    'パスワードの指定
    Dim strPass As String
    strPass = "a"
    
    'ファイル内の全てのファイルを調べる
    For Each fileObj In FSO.GetFolder("C:\temp\01_解凍前").Files

         '拡張子のチェック
         If FSO.GetextensionName(fileObj) = "zip" Then

            ' 解凍
            Set zipObj = shellObj.Namespace(fileObj.Path).Items
            Application.SendKeys strPass & "{Enter}"
            ret = shellObj.Namespace("C:\temp\02_解凍後").CopyHere(zipObj)
          
          End If
    
    Next fileObj
    
    '後処理
    Set FSO = Nothing
    Set shellObj = Nothing
    Set fileObj = Nothing
    Set zipObj = Nothing

    If ret > 0 Then
        MsgBox "失敗しました。", 48
    End If

End Sub

VBA初心者の方は以下の本から勉強してみてください。

─────────────── ★
中小企業診断士 かとう
専門は、IT活用による業務改善です。
★───────────────

お勧め:

  • このエントリーをはてなブックマークに追加

関連記事

  1. Excel VBA – 必見!プログラム関数の探し方のコツ
  2. Excel VBA – 必見!関数の引数をコンパクト…
  3. Excel VBA – CSVファイルをxlsxファ…
  4. Excel VBA – 2010 2013 マクロの…
  5. Excel VBA – 最終行の取得 その2
  6. Excel VBA – VBAでHashMapを実現…
  7. Excel VBA – 最終行の取得 その1
  8. Excel VBA – ブックを開く・閉じる(ope…
PAGE TOP