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. Excel

    Acrobat XIを操作してテキスト認識操作を行うVBAマクロ

    マクロからAcrobatを操作する場合「PDFファイル上のフィールドの…

  2. Office アドイン

    Office 365でVisio JavaScript APIsを試してみました。

    昨年末にVisio Onlineの機能をJavaScriptで拡張する…

  3. Excel

    IEを操作するVBAマクロ実行中にオートメーションエラーが発生する場合の対処法

    下記コードのような、Internet Explorerを操作する簡単な…

  4. Office関連

    Slackのカスタム絵文字を一括作成するVBAマクロ

    在宅勤務で使うビジネスチャットサービスとして注目されている「Slack…

  5. Microsoft Teams

    Microsoft Teamsでタブの構築を試してみました。

    下記記事の通り、Office 365に新たなコラボレーションツール「M…

  6. Office アドイン

    [Officeアドイン]アドイン コマンド(Add-In Commands)の紹介

    前回の記事は“Office アドイン”のAdvent calendar…

コメント

  • コメント (0)

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

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

Time limit is exhausted. Please reload CAPTCHA.

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

Translate

最近の記事

アーカイブ

PAGE TOP