How do I delimit a pathname containing spaces in VB6? - vb6

I'm trying to invoke the CreateProcess function and am having trouble with the path name for the application, which contains spaces. The documentation here instructs me to 'use quoted strings' to specify a path such as X:\My Directory\Myexe.exe but is silent on how to do this, which is a shame as I haven't managed it yet.
"""X:\My Directory\Myexe.exe"""
gets error 123 (syntax incorrect), and
"X:\""My Directory""\Myexe.exe"
gets error 3 (path not found).
Does anyone know how to do this?
Edit as asked, more code. I'm using this, with various attempts at the exe path name. This doesn't actually work but fails for other reasons (yet to be discovered).
Dim our_process_information As PROCESS_INFORMATION
Dim process_attributes As SECURITY_ATTRIBUTES
Dim thread_attributes As SECURITY_ATTRIBUTES
create_result = CreateProcess("X:\Myexe.exe", _
vbNull, _
process_attributes, _
thread_attributes, _
0, _
0, _
0, _
"X:\", _
startup_information, _
our_process_information)

This may be failing because you are passing the application name to the command line. The documentation describe them as more or less interchangable (except you can put command line arguments in the second parameter). The second argument is for the command line so you could do something like this:
create_result = CreateProcess(vbNullString, _
Chr(34) & "X:\Myexe.exe" & Chr(34), _
process_attributes, _
thread_attributes, _
0, _
0, _
0, _
"X:\", _
startup_information, _
our_process_information)
To wrap the command line in quotes and pass it to the command line argument of the function call. I've had success with doing it this way around in the past.
Edit
Fairly nice example here.

It's been a while since I've used VB6, but I think the syntax I used was:
Chr(34) & "X:\My Directory\Myexe.exe" & Chr(34)
So your code snipet would become:
create_result = CreateProcess(Chr(34) & "X:\My Directory\Myexe.exe" & Chr(34), _
vbNull, _
process_attributes, _
thread_attributes, _
0, _
0, _
0, _
"X:\", _
startup_information, _
our_process_information)

Related

Bereak single line code into multiple lines in QTP

_ works to split a long line into multiple line in VBSCript. Wondering if it works for QTP as well
Use & _ for break your long lines.
sString = "Something very long " & _
"More on that" & _
"A little more"
Reference is here

how to call exe file in vb6 with multiple parameters

I tried With below code
Dim pthName As String
Dim Parms As String
Dim RpNo As Integer
Dim glngbr As Long
Dim PrtVw As String
pthName = "D:\Sample.exe"
RpNo = 1
PrtVw = "V"
glngbr = 84003
Shell pthName & Parms
I am getting error "Run time error 53 ".
i tried without parameter its working
Shell pthName
You can use like this,
Shell "D:\Sample.exe" & " " & Param_1, Param_2
Try this:
Shell "D:\Sample.exe" & " " & RpNo & " " & PrtVw & " " & glngbr
Use the api shellexecute http://support.microsoft.com/kb/238245
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Private Const SW_HIDE As Long = 0
Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMAXIMIZED As Long = 3
Private Const SW_SHOWMINIMIZED As Long = 2
private Sub exec_program()
ShellExecute App.hInstance, "Open", "D:\Sample.exe", "Parms", "C:\", SW_SHOWNORMAL
End Sub
You don't need to use the Shell command, or ShellExecute either for that matter. There's a simpler solution: the global VBA.Command object contains whatever string you added when you called your exe file. For example, if you enter myproject.exe hello world on the command line, VBA.Command will contain the string hello world.
If you want multiple command line arguments, you can just put them all on the command line separated by a known delimiter such as /. Then you can use the Split function to break them up.
Read This. It will tell you all about it, including how to use it in the IDE without having to test against a compiled version.

Is it possible to create an error and assign it to an error subtype variable in VBScript?

Theoretically, if VarType(foo) returns 10 or vbError, it means that foo is an error. Am I wrong?
So how do I force this to happen? Is it even possible to force foo to hold an error? If it's not, then I don't understand why Error is a variable subtype.
VBScript doesn't support all of the Variant subtypes. For example, try to create a variable of subtype vbDecimal (VBScript doesn't even have the CDec() function to allow you to type-convert to Decimal) or try to create an array of anything other than the vbVariant subtype.
These constants are part of the Variant specification defined by the OLE Automation libraries. VB/VBA supports all of these subtypes and it seems VBScript adopted the same constants, even though it has no native support for many of them.
In VBScript hold:
If an object has a default property, VarType (object) returns the type of its default property.
The default property of the Err object is Number. Err.Number contains an integer.
Thus, VarType(Err) returns 3 as well as VarType(foo) in next example:
On Error Resume Next
Dim foo
Set foo = Err
se tr 'this line raises an error
Wscript.Echo CBool( IsObject( foo)) _
& vbNewLine & VarType( foo) _
& vbNewLine & foo.Number _
& vbNewLine & foo.HelpFile _
& vbNewLine & foo.HelpContext _
& vbNewLine & foo.Source _
& vbNewLine & foo.Description

Editing Text File In vb6

I Have Displayed text file in richtextbox.
and onclick on command button value of textbox1 is being replaced in text file.
but How to keep both data . previous one and another which is entered new in textbox
I HAVE USE THIS CODE BUT IT REPLACES ALL THE TEXT :
Open "D:\chat.txt" For Output As #1
a = Text1.Text
Print #1, a
Close #1
Change For Output to For Append, and it will add the new text to the end of the file instead of overwriting it.
Additional note
Since I'm not able to add a comment to Boann's answer (the one marked as accepted).
The Append access mode used with the Print statement automatically appends a new line at the end of the file. This is fine in almost all cases, but for anyone reading this that wants to avoid this behavior, just add a semicolon at the end of the Print statement (this is the only instance I've seen the semicolon used in VB6).
a = Text1.Text
intHandle = FreeFile
Open "D:\chat.txt" For Append As intHandle
Print #intHandle, a; ' Notice the semicolon; prevents a new line after this output.
Close #intHandle
I'm sure the code you posted originally was just for the sake of getting an answer and is not what your code actually looks like. Otherwise:
To you or any future readers, here's a simple AppendToFile() function which will make repeated calls easier, ensures the file gets closed even if a run-time error is encountered, and shows useful debug information upon failure (i.e. with an invalid filename):
How your original code would be written when putting my below function in your code:
AppendToFile "D:\chat.txt", Text1.Text
And here's the function:
Private Function AppendToFile( _
ByRef FilePath As String, _
ByRef Text As String, _
Optional ByVal AppendNewLine As Boolean = True _
) As Boolean
On Error GoTo ErrorHandler
Dim intHandle As Integer
' Get an available file handle to use.
intHandle = FreeFile
Open FilePath For Append As intHandle
' Only use semicolon at end if we do NOT want to append a new line.
If AppendNewLine Then
Print intHandle, Text
Else
Print intHandle, Text;
End If
Close intHandle
intHandle = 0
AppendToFile = True
Exit Function
ErrorHandler:
' Ensure that file is indeed closed.
If intHandle <> 0 Then
Close intHandle
End If
' Show error in debug window (CTRL+G)
Debug.Print _
"Error (#" & CStr(Err.Number) & ") in " & _
"TextToFile( _" & vbCrLf & _
"`" & FilePath & "`, _" & vbCrLf & _
"`" & Text & "`, _" & vbCrLf & _
IIf(AppendNewLine, "`True`", "`False`") & vbCrLf & _
"): " & Err.Description & IIf("." = Right$(Err.Description, 1), "", ".") & vbCrLf
Exit Function
End Function

Centralized error handling in VB6

I have the following method that all the error handlers call:
Public Function ToError(strClass As String, strMethod As String) As String
On Error GoTo errHandle
ToError = "Err " & Err.Number & _
", Src: " & Err.Source & _
", Dsc: " & Err.Description & _
", Project: " & App.Title & _
", Class: " & strClass & _
", Method: " & strMethod & _
", Line: " & Erl
Err.Clear
exitPoint:
Exit Function
errHandle:
oLog.AddToLog "Error in ToError Method: " & Err.Description, False
Resume exitPoint
End Function
It turns out that because I declare an error handler in this function On Error GoTo errHandle, VB6 clears the error before I am able to record it.
Is there a way to prevent the 'On Error GoTo errHandle' statement from clearing the error?
An On Error statement will always clear the Err variable (Erl will also be reset to 0). In theory this means you could fix the problem by moving the On Error statement below the ToString = ... line (or removing the error handler in the ToError function altogether), but unfortunately that won't necessarily always work either.
Each component (DLL, ActiveX EXE, etc.) referenced by your project essentially gets its own Err instance in memory. So, if your MainApp.exe raises an error which gets passed to ToError (residing in a separate ErrorHandling.dll for example), the DLL won't see the Err variable that your EXE sees. They each have their own private Err variables.
There are a at least two ways around the problem that I can think of:
Method 1
As Zian Choy mentions, you could add additional parameters to your ToError function, one for each property of the Err object that you need access to.
Code
Public Function ToError( _
ByVal strErrSource As String, _
ByVal nErrNumber As Long, _
ByVal sErrDescription As String, _
ByVal nLineNumber As Long) As String
Example usage
You would then have to call like this from your error handlers like so, passing it all the relevant values from the current Err object, along with Erl:
ToError Err.Source, Err.Number, Err.Description, Erl
If you also want App.Title, you're going to have to add an additional parameter to ToError for that as well, since App.Title will be equal to the App.Title of the project where the ToError method is defined, not the component where the error was raised. This is important if ToError is in a different project.
Method 2
You can make your ToError calls a little less verbose by passing the Err object itself as a parameter to the function, however the first thing your ToError function should do in this case is immediately store a copy of all the relevant properties you need since a subsequent On Error statement will clear the variable.
Code
Public Function ToError(ByVal oError As ErrObject, ByVal nLineNumber As Long) As String
'Copy the important Err properties first, '
'before doing anything else... '
Dim strErrSource As String
Dim nErrNumber As Long
Dim strErrDescription As String
strErrSource = oError.Source
nErrNumber = oError.Number
strErrDescription = oError.Description
On Error Goto errHandle
'More code here
'...
Example Usage
ToError Err, Erl
You may be able to solve the problem by passing the values of the Err object into ToError as parameters.
There's no way to prevent On Error clearing the error.
You could just remove the error handling from ToError. It's so short and bland it's unlikely to ever experience an error.
It might be better to refactor the error handling so that this ToError code is inline in a general purpose error reporting routine, which performs logging or whatever is needed. Then use the techniques in Mike's answer.
BTW If anyone reading this is adding their error handlers manually, stop whatever you are doing and immediately get the free MZ-Tools package.
You can create a User Defined Type as below
Private Type TempErrObj
ErrNumber As Integer
ErrSource As String
ErrDescription As String
End Type
Later modify ToError function as below :
Public Function ToError() As String
Dim iTempErr As TempErrObj
iTempErr.ErrNumber = Err.Number
iTempErr.ErrSource = Err.Number
iTempErr.ErrDescription = Err.Description
On Error GoTo errHandle
ToError = "Err " & iTempErr.ErrNumber & _
", Src: " & iTempErr.ErrSource & _
", Dsc: " & iTempErr.ErrDescription & _
", Project: " & App.Title & _
", Line: " & Erl
Err.Clear
exitPoint:
Exit Function
errHandle:
oLog.AddToLog "Error in ToError Method: " & Err.Description, False
Resume exitPoint
End Function

Resources