title: How can I add files to a cab-file from VBA, using cabinet.dll API calls?
body: <p>Cabinet.dll provides a collection of functions to compress/extract files and manage CAB files. I'd like to use it with VBA. There are few examples online using these features in VBA.</p>
  <p>I want to add individual files to an existing CAB. That might be possible with the fx Compress or FCIAddFile. But I haven't found any VBA examples with FCIAddFile.</p>
  <p>I attempted the following declarations. Inline comments describe the errors:</p>
  <pre><code>Enum CompressionType
            MSZIP = 2
            XPRESS = 3
            XPRESS_HUFF = 4
            LZMS = 5
  End Enum
  
  
  Private Declare PtrSafe Function CreateCompressor&amp; Lib &quot;cabinet&quot; (ByVal CompressAlgorithm&amp;, ByVal pAllocationRoutines&amp;, hCompressor&amp;)
  Private Declare PtrSafe Function Compress&amp; Lib &quot;cabinet&quot; (ByVal hCompressor&amp;, ByVal pUncompressedData As String, ByVal sizeUncompressedData&amp;, ByVal pCompressedDataBuffer As String, ByVal sizeCompressedBuffer&amp;, bytesOut&amp;)
  Private Declare PtrSafe Function CreateDecompressor&amp; Lib &quot;cabinet&quot; (ByVal CompressAlgorithm&amp;, ByVal pAllocationRoutines&amp;, hDecompressor&amp;)
  Private Declare PtrSafe Function Decompress&amp; Lib &quot;cabinet&quot; (ByVal hCompressor&amp;, ByVal pCompressedData&amp;, ByVal sizeCompressedData&amp;, ByVal pUncompressedDataBuffer&amp;, ByVal sizeOfUncompressedBuffer&amp;, bytesOut&amp;)
  Private Declare PtrSafe Function CloseCompressor&amp; Lib &quot;cabinet&quot; (ByVal hCompressor&amp;)
  Private Declare PtrSafe Function CloseDecompressor&amp; Lib &quot;cabinet&quot; (ByVal hDecompressor&amp;)
  
  Private Declare PtrSafe Function FCIAddFile&amp; Lib &quot;cabinet&quot; (ByVal hfci&amp;, ByVal pszSourceFile&amp;, ByVal pszFileName&amp;, ByVal fExecute&amp;, ByVal pfnfcignc&amp;, ByVal pfnfcis&amp;, ByVal pfnfcigoi&amp;, ByVal typeCompress&amp;)
  
  
  Function CompressString$(s$, Optional algorithm&amp; = 5)
            Dim h&amp;, max&amp;, bytesOut&amp;, b$
            CreateCompressor algorithm, 0&amp;, h
            max = LenB(s): b = Space$(max)
  
            FCIAddFile h, &quot;C:\Data\Network info.txt&quot;, &quot;Network info.txt&quot;, False, 0&amp;, 0&amp;, 0&amp;, XPRESS     ' &lt;-- TYPE MISMATCH ERROR
  
            Compress h, StrPtr(s), max, StrPtr(b), max, bytesOut
            If bytesOut Then CompressString = Left$(b, bytesOut \ 2)
            CloseCompressor h     ' &lt;-- EXCEL CRASHES IF THIS LINE IS ALLOWED TO EXECUTE
  End Function
  </code></pre>
  <p>Borrowed from <a href="https://newbedev.com/excel-vba-compression-code-example" rel="nofollow noreferrer">https://newbedev.com/excel-vba-compression-code-example</a></p>
  <hr />
  <p>I don't understand why the constants appear to be different than the <a href="https://docs.microsoft.com/en-us/windows/win32/api/compressapi/nf-compressapi-createcompressor" rel="nofollow noreferrer">CreateCompressor</a> function.</p>
  <p><strong>CreateCompressor:</strong></p>
  <pre><code>MSZIP = 2
  XPRESS = 3
  XPRESS_HUFF = 4
  LZMS = 5
  </code></pre>
  <p><strong><a href="https://docs.microsoft.com/en-us/windows/win32/api/fci/nf-fci-fciaddfile" rel="nofollow noreferrer">FCIAddFile</a>:</strong></p>
  <pre><code>tcompTYPE_NONE: 0x0000
  tcompTYPE_MSZIP: 0x0001
  </code></pre>