Office関連

ZIP形式で圧縮・解凍を行うVBAマクロ

この記事のように、処理の中でZIP形式のファイルを扱うことはありましたが、圧縮・解凍処理だけを記事にすることは無かったので、簡単にコードをまとめてみました。

Option Explicit

Public Sub ZipSample()
  ZipFileOrFolder "C:\Test\Files" 'フォルダ圧縮
  MsgBox "処理が終了しました。", vbInformation + vbSystemModal
End Sub

Public Sub UnZipSample()
  UnZipFile "C:\Test\Files\Test.zip"
  MsgBox "処理が終了しました。", vbInformation + vbSystemModal
End Sub

Public Sub ZipFileOrFolder(ByVal SrcPath As Variant, _
                           Optional ByVal DestFolderPath As Variant = "")
'ファイル・フォルダをZIP形式で圧縮
'SrcPath:元ファイル・フォルダ
'DestFolderPath:出力先、指定しない場合は元ファイル・フォルダと同じ場所
  Dim DestFilePath As Variant
  
  With CreateObject("Scripting.FileSystemObject")
    If IsFolder(DestFolderPath) = False Then
      If IsFolder(SrcPath) = True Then
        DestFolderPath = SrcPath
      ElseIf IsFile(SrcPath) = True Then
        DestFolderPath = .GetFile(SrcPath).ParentFolder.Path
      Else: Exit Sub
      End If
    End If
    DestFilePath = AddPathSeparator(DestFolderPath) & _
                     .GetBaseName(SrcPath) & ".zip"
    '空のZIPファイル作成
    With .CreateTextFile(DestFilePath, True)
      .Write ChrW(&H50) & ChrW(&H4B) & ChrW(&H5) & ChrW(&H6) & String(18, ChrW(0))
      .Close
    End With
  End With
  
  With CreateObject("Shell.Application")
    With .NameSpace(DestFilePath)
      .CopyHere SrcPath
      While .Items.Count < 1
        DoEvents
      Wend
    End With
  End With
End Sub

Public Sub UnZipFile(ByVal SrcPath As Variant, _
                     Optional ByVal DestFolderPath As Variant = "")
'ZIPファイルを解凍
'SrcPath:元ファイル
'DestFolderPath:出力先、指定しない場合は元ファイルと同じ場所
'※出力先に同名ファイルがあった場合はユーザー判断で処理
  With CreateObject("Scripting.FileSystemObject")
    If .FileExists(SrcPath) = False Then Exit Sub
    If LCase(.GetExtensionName(SrcPath)) <> "zip" Then Exit Sub
    If IsFolder(DestFolderPath) = False Then
      DestFolderPath = .GetFile(SrcPath).ParentFolder.Path
    End If
  End With
  
  With CreateObject("Shell.Application")
    .NameSpace(DestFolderPath).CopyHere .NameSpace(SrcPath).Items
  End With
End Sub

Private Function IsFolder(ByVal SrcPath As String) As Boolean
  IsFolder = CreateObject("Scripting.FileSystemObject").FolderExists(SrcPath)
End Function

Private Function IsFile(ByVal SrcPath As String) As Boolean
  IsFile = CreateObject("Scripting.FileSystemObject").FileExists(SrcPath)
End Function

Private Function AddPathSeparator(ByVal SrcPath As String) As String
  If Right(SrcPath, 1) <> ChrW(92) Then SrcPath = SrcPath & ChrW(92)
  AddPathSeparator = SrcPath
End Function

Shellで処理するため、外部のアプリケーションやDLLが不要な点はメリットとして挙げられるのですが、上記コードの処理は“サポート対象外”となっているので、場合によっては上手く処理できないかもしれません。

関連記事

2016年10月の人気記事前のページ

【感想】なんと染め抜く次のページ

関連記事

  1. アイコン一覧

    Office 2013 アイコン一覧(O)

    ・Office 2013 アイコン一覧 NUM…

  2. Excel

    Excelで地理データや株価情報を取得する方法

    下記記事にある、今年3月に追加された「データの種類」機能を使って、Ex…

  3. Office関連

    代替テキストを削除するPowerPointマクロ

    PowerPointの図やSmartArt、グループやグラフといった視…

  4. Office アドイン

    Office アドインの概要と開発方法を学ぶための自習書

    2018年10月27日(土)、品川の日本マイクロソフト本社で「2018…

  5. Office関連

    [Office]アイコンの検索機能が超便利!

    ※ 下記情報はInsider版のOfficeを元にしています。バージョ…

  6. アイコン一覧

    Office 2013 アイコン一覧(C)

    ・Office 2013 アイコン一覧 NUM…

コメント

  • コメント (0)

  • トラックバックは利用できません。

  1. この記事へのコメントはありません。

Time limit is exhausted. Please reload CAPTCHA.

※本ページはプロモーションが含まれています。

Translate

最近の記事

アーカイブ

PAGE TOP