Shell method

O

ondaWall

I am trying to write a routine that will zip a .htm document, but
running into difficulty using the Shell method. The following will not
compile. It errors on the shell statement. I cant even get the shell
to open an instance of notepad. Many thanks in advance for replies.

Private Sub cmdTest_Click()

Dim pathWinZip As String, fileZipName As String
Dim fileToZip As String
Dim shellStr As String

On Error GoTo ErrorHandler
fileZipName = "C:\imhere.zip"
fileToZip = "C:\yadda.htm"
pathWinZip = "C:\program files\winzip\"

If Dir(pathWinZip & "winzip32.exe") = "" Then
MsgBox "Please install winzip and try again"
Exit Sub
End If

shellStr = pathWinZip & "winzip32.exe -min -a" _
& " " & Chr(34) & fileZipName & Chr(34) _
& " " & Chr(34) & fileToZip & Chr(34)

Shell(shellStr, 0) 'the compile stops here

Exit Sub
 
D

Douglas J. Steele

Saying what the error message is when it fails to compile would have helped.
I was just about to send this message saying I couldn't see a reason for it
to fail compilation when I noticed that you've got On Error GoTo
ErrorHandler, yet ErrorHandler isn't defined.

However, once you've fixed that, since pathWinZip contains an embedded
blank, you need to enclose the path to the executable in quotes:

shellStr = Chr(34) & pathWinZip & "winzip32.exe" _
& Chr(34) & " -min -a " _
& Chr(34) & fileZipName & Chr(34) _
& " " & Chr(34) & fileToZip & Chr(34)

Just out of curiosity, why haven't you set pathWinZip to the complete path
to the executable, rather than just the folder?

Private Sub cmdTest_Click()

Dim pathWinZip As String, fileZipName As String
Dim fileToZip As String
Dim shellStr As String

On Error GoTo ErrorHandler
fileZipName = "C:\imhere.zip"
fileToZip = "C:\yadda.htm"
pathWinZip = "C:\program files\winzip\winzip32.exe"

If Len(Dir(pathWinZip)) = 0 Then
MsgBox "Please install winzip and try again"
Exit Sub
End If

shellStr = Chr(34) & pathWinZip & Chr(34) _
& " -min -a " _
& Chr(34) & fileZipName & Chr(34) _
& " " & Chr(34) & fileToZip & Chr(34)

Shell(shellStr, 0)

Exit Sub
 
D

Douglas J. Steele

Just realized another reason why your compilation would fail.

Shell is a function, not a sub.

You either need to assign its value to a variable, or use the Call keyword:

Call Shell(shellStr, 0)
 
O

ondaWall

My apologies Doug. Thanks for the response.

The error message: Compile error: expected variable or procedure, not
module
I made the change to enclose the executable in quotes. The errhandler
is there, I just didnt pick it up int eh copy and paste.
I am still getting the error message as above. Pasting the existing
code, in its entirety.

Private Sub cmdTestZip_Click()
Dim pathWinZip As String, fileZipName As String
Dim fileToZip As String
Dim shellStr As String
Dim x

On Error GoTo ErrorHandler
fileZipName = "C:\imhere.zip"
fileToZip = "C:\yadda.htm"
pathWinZip = "C:\program files\winzip\winzip32.exe"

If Dir(pathWinZip & "winzip32.exe") = "" Then
MsgBox "Please install winzip and try again"
Exit Sub
End If

shellStr = Chr(34) & pathWinZip _
& Chr(34) & " -min -a " _
& Chr(34) & fileZipName & Chr(34) _
& " " & Chr(34) & fileToZip & Chr(34)

x = Shell(shellStr, 1)

Exit Sub

ErrorHandler:
' Display error information.
MsgBox "Error number " & Err.Number & ": " & Err.Description
' Resume with statement following occurrence of error.
Resume Next

End Sub
 
O

ondaWall

Doug,

I added Call as follows:

Call Shell(shellStr, 1)

Still receive the error message: expected variable or procedure, not
module
 
D

Douglas J. Steele

This compiles fine for me.

Have you got a module named Shell in your application, or have you used
Shell for anything: a variable, a table, a query, etc.?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top