VBA help - Automation error. The system cannot find the file specified

Js Smith

Board Regular
Joined
Jul 24, 2020
Messages
51
Office Version
  1. 2010
Platform
  1. Windows
Hi all!

I feel like this code is VERY close but cannot see the error I'm making

VBA Code:
Sub ZipThis()

   Dim src  As Variant, dst As Variant, file As Variant
   Dim f As String, Path As String, FileType As String
   Dim wsh As Object, strCommand As String

   Const PATH_TO_7Z = "C:\Program Files\7-Zip\7z.exe"

   src = Application.GetOpenFilename(filefilter:="PDF Files (*.pdf), *.pdf", Title:="Select the file you want to zip")

    f = src

    ary = Split(f, "\")
    bry = Split(ary(UBound(ary)), ".")
    ary(UBound(ary)) = ""
    ThePath = Join(ary, "\")
    Filename = bry(0)
    FileType = bry(1)


      dst = ThePath

    Set wsh = CreateObject("WScript.Shell")
    
                strCommand = PATH_TO_7Z & " a -tzip """ & dst & Filename & ".zip"" """ & dst & """"
              
                lngErrorCode = wsh.Run(strCommand, WindowStyle:=1, WaitOnReturn:=1)       '  <<<<  Automation error. The system cannot find the file specified

   
   MsgBox "Done!"
   
End Sub

It nearly finishes then the automation error. Feeling like it's something small but cant figure out the solution.
It's supposed to let the user browse to a pdf then compress it with 7Zip. How would you remedy this?

Thanks, in advance, as always!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I would put in a message box to show the strCommand to check the paths/syntax are correct as that is the reported problem
 
Upvote 0
Also, you need quotes around the program path because it contains a space:
VBA Code:
Const PATH_TO_7Z = """C:\Program Files\7-Zip\7z.exe"""
 
Upvote 0
Solution
I knew it was something small!!! Added the quotes and it works perfectly.
Many thanks to you both for the extra sets of eyes! Hope you have a great weekend.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,206
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top