|
-
Sep 12th, 2011, 10:27 AM
#1
Thread Starter
New Member
Shell and wait File not found error WHY???[RESOLVED]
Can anybody enlighten me as to why my shell and wait function returns file not found error?
Code:
Option Explicit
''' *************************************************************************
''' Module Constant Declaractions Follow
''' *************************************************************************
''' Constant for the dwDesiredAccess parameter of the OpenProcess API function.
Private Const PROCESS_QUERY_INFORMATION As Long = &H400
''' Constant for the lpExitCode parameter of the GetExitCodeProcess API function.
Private Const STILL_ACTIVE As Long = &H103
''' *************************************************************************
''' Module Variable Declaractions Follow
''' *************************************************************************
''' It's critical for the shell and wait procedure to trap for errors, but I
''' This variable is used to pass error
''' messages between procedures.
Public gszErrMsg As String
''' *************************************************************************
''' Module DLL Declaractions Follow
''' *************************************************************************
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Public Sub DemoShellAndWait()
On Error GoTo ErrorHandler
''' Clear the error mesaage variable.
gszErrMsg = vbNullString
''' Notification that the Integration is about to begin.
MsgBox "You are about to Send This Confirmation Via Echosign To the Desired Recipient.", vbInformation, "Echosign API Integration"
''' Shell out to the Echosign.
If Not bShellAndWait("C:\Documents and Settings\ben barnes\Desktop\CSharp\demo.bat http://www.example.com/services/Echo...umentService11 IQKL5CXI6V2C4W send C:\Documents and Settings\ben barnes\Desktop\Echo.bat [email protected] ") Then Err.Raise 9999
''' This message box will not display until you have dismissed the shell.
MsgBox "You have finished sending this confirmation via Echosign.", vbInformation, "Shell and Wait Demo"
Exit Sub
ErrorHandler:
''' If we ran into any errors this will explain what they are.
MsgBox gszErrMsg, vbCritical, "Shell and Wait Demo"
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' Comments: Shells out to the specified command line and waits for it to
''' complete. The Shell function runs asynchronously, so you must
''' run it using this function if you need to do something with
''' its output or wait for it to finish before continuing.
'''
''' Arguments: szCommandLine [in] The command line to execute using Shell.
''' iWindowState [in] (Optional) The window state parameter to
''' pass to the Shell function. Default = vbHide.
'''
''' Returns: Boolean True on success, False on error.
'''
''' Date Developer Action
''' --------------------------------------------------------------------------
''' 05/19/05 Ben Barnes Created
'''
Private Function bShellAndWait(ByVal szCommandLine As String, Optional ByVal iWindowState As Integer = vbHide) As Boolean
Dim lTaskID As Long
Dim lProcess As Long
Dim lExitCode As Long
Dim lResult As Long
On Error GoTo ErrorHandler
''' Run the Shell function.
lTaskID = Shell(szCommandLine, iWindowState)
''' Check for errors.
If lTaskID = 0 Then Err.Raise 9999, , "Shell function error."
''' Get the process handle from the task ID returned by Shell.
lProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0&, lTaskID)
''' Check for errors.
If lProcess = 0 Then Err.Raise 9999, , "Unable to open Shell process handle."
''' Loop while the shelled process is still running.
Do
''' lExitCode will be set to STILL_ACTIVE as long as the shelled process is running.
lResult = GetExitCodeProcess(lProcess, lExitCode)
DoEvents
Loop While lExitCode = STILL_ACTIVE
bShellAndWait = True
Exit Function
ErrorHandler:
gszErrMsg = Err.Description
bShellAndWait = False
End Function
Last edited by Barnzee; Sep 15th, 2011 at 06:09 AM.
Reason: added Code tags, altered web and email addresses to example.com
-
Sep 13th, 2011, 05:40 AM
#2
Re: Shell and wait File not found error WHY???
i would believe the error is caused by passing file paths with space to the shell command
vb Code:
lTaskID = Shell(szCommandLine, iWindowState)
vb Code:
bShellAndWait("C:\Documents and Settings\ben barnes\Desktop\CSharp\demo.bat http://www.example.com/services/Echo...umentService11 IQKL5CXI6V2C4W send C:\Documents and Settings\ben barnes\Desktop\Echo.bat [email protected] ")
shell sees c:\documents as a command all the rest is seen as parameters
to resolve, enclose file paths in quotes
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Sep 14th, 2011, 11:00 AM
#3
Thread Starter
New Member
Re: Shell and wait File not found error WHY???
Thanks Westconn - I have sorted the problem
- I needed to put a line in my Batch file to change the path in dos, as it was not working even after using quotes!
THIS IS MY WORKING CODE:
Code:
Option Explicit
''' *************************************************************************
''' Module Constant Declaractions Follow (SOME BORING JARGON BASICALLY)
''' *************************************************************************
''' Constant for the dwDesiredAccess parameter of the OpenProcess API function.
Private Const PROCESS_QUERY_INFORMATION As Long = &H400
''' Constant for the lpExitCode parameter of the GetExitCodeProcess API function.
Private Const STILL_ACTIVE As Long = &H103
''' *************************************************************************
''' Module Variable Declaractions Follow (SOME MoRE BORING JARGON BASICALLY)
''' *************************************************************************
''' It's critical for the shell and wait procedure to trap for errors, but I
''' This variable is used to pass error
''' messages between procedures.
Public gszErrMsg As String
''' *************************************************************************
''' Module DLL Declaractions Follow (SOME BORING JARGON BASICALLY)
''' *************************************************************************
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Public Sub DemoShellAndWait()
''''WOHHHHH SLOW DOWN WE WANNA SAVE THIS FIRST DUDE
'' Code Saves to specified directory
''''C:\xe\Confirmation of Booking - every user will using this directory to store the echosign api mods.
ChDir "C:\xe\"
ActiveDocument.SaveAs FileName:="C:\xe\ConfirmationofBooking", _
FileFormat:=wdFormatDocument97
''' Including this close script ensures the file will be found by the batch script - if its not open it will definately send.
ActiveDocument.Close
''' Now we can start the shelling - error handler first though rudeboy. Gotta be robust.
On Error GoTo ErrorHandler
''' Clear the error mesaage variable.
gszErrMsg = vbNullString
''' Notification that the Integration is about to begin.
MsgBox "You are about to Send This Confirmation Via Echosign To the Desired Recipient.", vbInformation, "Echosign API Integration"
''' Shell out to the Echosign.
If Not bShellAndWait("C:\xe\echo2.bat http://www.example.com/services/Echo...umentService11 IQKL5CXI6V2C4W send C:\xe\ConfirmationofBooking.Doc [email protected]") Then Err.Raise 9999
''' This message box will not display until you have dismissed the shell.
MsgBox "You have finished sending this confirmation via Echosign.", vbInformation, "Shell and Wait Demo"
Exit Sub
ErrorHandler:
''' If we ran into any errors this will explain what they are.
MsgBox gszErrMsg, vbCritical, "Shell and Wait Demo"
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' Comments: Shells out to the specified command line and waits for it to
''' complete.
'''
''' Arguments: szCommandLine
''' iWindowState
'''
'''
''' Returns: Boolean True on success, False on error.
'''
''' Date Developer Action
''' --------------------------------------------------------------------------
''' 05/19/05 Ben Barnes Mangled together with some other nice functions to make this sexy script
'''
'Private
Public Function bShellAndWait(ByVal szCommandLine As String, Optional ByVal iWindowState As Integer = vbHide) As Boolean
Dim lTaskID As Long
Dim lProcess As Long
Dim lExitCode As Long
Dim lResult As Long
On Error GoTo ErrorHandler
''' Run the Shell function.
lTaskID = Shell(szCommandLine, iWindowState)
''' Check for errors.
If lTaskID = 0 Then Err.Raise 9999, , "Shell function error."
''' Get the process handle from the task ID returned by Shell.
lProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0&, lTaskID)
''' Check for errors.
If lProcess = 0 Then Err.Raise 9999, , "Unable to open Shell process handle."
''' Loop while the shelled process is still running.
Do
''' lExitCode will be set to STILL_ACTIVE as long as the shelled process is running.
lResult = GetExitCodeProcess(lProcess, lExitCode)
DoEvents
Loop While lExitCode = STILL_ACTIVE
bShellAndWait = True
Exit Function
ErrorHandler:
gszErrMsg = Err.Description
bShellAndWait = True
End Function
But Now I have another issue...
I need to combine these two in a call routine which i can attach to a command button click routine (called cmd)
So far i have:
Public Sub Cmd1()
DemoShellAndWait
Dim s As String
s = Application.Run(bShellAndWait)
end sub.
This is clearly wrong - i know there are arguments to be passed to Run(bshellandwait(varg1,varg2,etc)
but i cant seem to get it to work...
Anybody got any further tips - love to get this wrapped up ASAP Thanks again.
Last edited by Barnzee; Sep 15th, 2011 at 02:41 AM.
Reason: altered web and email addresses to example.com
-
Sep 14th, 2011, 01:31 PM
#4
Fanatic Member
Re: Shell and wait File not found error WHY???
You could try the library in my signature.
-
Sep 14th, 2011, 04:35 PM
#5
Re: Shell and wait File not found error WHY???
if you put all your code in code or highlight (vbcode) tags people are more likely to check it out
Now I have another issue...
and the issue is?
what is happening now?
wrong result?
error?
nothing?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Sep 15th, 2011, 02:41 AM
#6
Thread Starter
New Member
Re: Shell and wait File not found error WHY???
 Originally Posted by westconn1
if you put all your code in code or highlight (vbcode) tags people are more likely to check it out
and the issue is?
what is happening now?
wrong result?
error?
nothing?
Sorry, I didnt even see the tag functionality!
The code works fine, but I now need the full routine to be called from a Commandbutton sub (See edited original post )
Thanks mate.
-
Sep 15th, 2011, 04:11 AM
#7
Re: Shell and wait File not found error WHY???
This is clearly wrong - i know there are arguments to be passed to Run(bshellandwait(varg1,varg2,etc)
but those arguments are hard coded in DemoShellAndWait, when you call this from your cmd it should just work with the hard coded parameters
alternatively you could just move all the code from DemoShellAndWait to cmd
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Sep 15th, 2011, 04:15 AM
#8
Thread Starter
New Member
Re: Shell and wait File not found error WHY???
It doesnt work...
Sub Cmd ()
Call Demoshellandwait
Call bshellandwait
End Sub
I get argument not optional returned as error?
How would you structure the Cmd Sub based upon the original Code provided above? Im a novice, and it took me weeks to structure the shell and wait code within the normal module...
-
Sep 15th, 2011, 04:41 AM
#9
Re: Shell and wait File not found error WHY???
as bshellandwait is called from the demo, there is no need to call it again
if you do want to call bshellandwait directly, you must pass the commandline argument as is not optional
How would you structure the Cmd Sub based upon the original Code provided above
vb Code:
Sub Cmd () Call Demoshellandwait End Sub
unless you want to pass variables from the form, like textbox contents, then i would move the entire demo procedure into the cmd procedure, change the commandline argument to be passed to the textboxes, or whatever, then they would be passed correctly to bshellandwait
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Sep 15th, 2011, 05:52 AM
#10
Thread Starter
New Member
Re: Shell and wait File not found error WHY???
Perfect...Thanks mate... nearly there now...
the next Thing will be more tricky I presume:
I have now hooked up a userinput box to obtain an email address.
Code:
Dim bText As String, bNumber As Integer
' here is the INPUTBOX-function :
bText = InputBox("Insert in a text", "This accepts any input")
MsgBox "You have inserted :" & Chr(13) & _
bText, , "Result from INPUT-boxes"
From here, im guessing the value returned would be btext.
I want to place the user generated input btext inton this line:
Code:
'' Shell out to the Echosign.
If Not bShellAndWait("C:\xe\echo2.bat http://www.echosign.com/services/EchoSignDocumentService11 IQKL5CXI6V2C4W send C:\xe\ConfirmationofBooking.Doc btext") Then Err.Raise 9999
This line is passing parameters to my batch file/exe - however, the webservice that we are passing these parameters to only accepts email address as the final Argument - btext gets passed as btext? and thus flaws the process?
Is there a way I can get VBA to trasncribe the user generated input into this line and ensure it is recognised as an email address?
Thanks...
-
Sep 15th, 2011, 06:01 AM
#11
Re: Shell and wait File not found error WHY???
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Sep 15th, 2011, 06:07 AM
#12
Thread Starter
New Member
Re: Shell and wait File not found error WHY???
My friend, You are an absolute legend...
Im getting the hang of VBA, just little things like the way things have to be 'exactly' written like & btext is throwing me off. You have taught me a lot, and helped me write something that is going to save my company a lot of time, so i salute you!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|