Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: Yegg on October 21, 2004, 05:51 PM

Title: GoTo Error
Post by: Yegg on October 21, 2004, 05:51 PM
im trying to make it so that in my bot you can open a text document through a menu, like mnutxtChan should open up channels.txt, hers my code:

Private Sub mnuUserstxt_Click()
On Error GoTo Error
  ShellExecute Me.hWnd, vbNullString, App.Path & "\users.txt", vbNullString, "C:\", SW_SHOWNORMAL
Error:
MsgBox "Could not find users.txt!"
End Sub

i want this to work so that if the txt document is not found the message box wil pop up. so far, when the txt document isn't found it comes up, but it also comes up when the txt document is found. can ne1 help me?
Title: Re: GoTo Error
Post by: drivehappy on October 21, 2004, 06:09 PM

Private Sub mnuUserstxt_Click()
If Dir$("C:\users.txt") = "" Then
        MsgBox "Could not find users.txt!"
Else
        ShellExecute Me.hWnd, vbNullString, App.Path & "\users.txt", vbNullString, "C:\",  SW_SHOWNORMAL
End If
End Sub

EDIT: code tags
Title: Re: GoTo Error
Post by: Yegg on October 21, 2004, 06:15 PM
ok, wel theres a problem with that code, no matter wut happens the message box pops up.
Title: Re: GoTo Error
Post by: CrAz3D on October 21, 2004, 06:39 PM
Did you notice the directory that drivehappy used?... C:\...unless your application's directory is C:\ also it will not work.
Title: Re: GoTo Error
Post by: Yegg on October 21, 2004, 06:43 PM
then how would i get the bot to look for that file in its current directory?
Is there a way i could use App.Path?
Title: Re: GoTo Error
Post by: Dyndrilliac on October 21, 2004, 06:46 PM
If Dir$(App.Path & "\users.txt") = vbNullString Then
Title: Re: GoTo Error
Post by: UserLoser. on October 21, 2004, 06:48 PM
Quote from: Yegg on October 21, 2004, 05:51 PM
im trying to make it so that in my bot you can open a text document through a menu, like mnutxtChan should open up channels.txt, hers my code:

Private Sub mnuUserstxt_Click()
On Error GoTo Error
  ShellExecute Me.hWnd, vbNullString, App.Path & "\users.txt", vbNullString, "C:\", SW_SHOWNORMAL
Error:
MsgBox "Could not find users.txt!"
End Sub

i want this to work so that if the txt document is not found the message box wil pop up. so far, when the txt document isn't found it comes up, but it also comes up when the txt document is found. can ne1 help me?

It's coming up because the next routine is at Error.  If you put Exit Sub after the ShellExecute, it wouldn't.  But on an error, it'll still say "Could not find users.txt!"
Title: Re: GoTo Error
Post by: Dyndrilliac on October 21, 2004, 06:50 PM
Quote from: UserLoser on October 21, 2004, 06:48 PM
Quote from: Yegg on October 21, 2004, 05:51 PM
im trying to make it so that in my bot you can open a text document through a menu, like mnutxtChan should open up channels.txt, hers my code:

Private Sub mnuUserstxt_Click()
On Error GoTo Error
  ShellExecute Me.hWnd, vbNullString, App.Path & "\users.txt", vbNullString, "C:\", SW_SHOWNORMAL
Error:
MsgBox "Could not find users.txt!"
End Sub

i want this to work so that if the txt document is not found the message box wil pop up. so far, when the txt document isn't found it comes up, but it also comes up when the txt document is found. can ne1 help me?

It's coming up because the next routine is at Error.  If you put Exit Sub after the ShellExecute, it wouldn't.  But on an error, it'll still say "Could not find users.txt!"

Goto is evil though.
Title: Re: GoTo Error
Post by: Yegg on October 21, 2004, 06:56 PM
ok, now i have:

On Error GoTo Error
  ShellExecute Me.hWnd, vbNullString, App.Path & "\users.txt", vbNullString, "C:\", SW_SHOWNORMAL
Exit Sub
Error:
MsgBox "Could not find users.txt!"

this opens the txt document but when its not found, nothing happens
Title: Re: GoTo Error
Post by: UserLoser. on October 21, 2004, 07:26 PM
Quote from: Yegg on October 21, 2004, 06:56 PM
ok, now i have:

On Error GoTo Error
  ShellExecute Me.hWnd, vbNullString, App.Path & "\users.txt", vbNullString, "C:\", SW_SHOWNORMAL
Exit Sub
Error:
MsgBox "Could not find users.txt!"

this opens the txt document but when its not found, nothing happens

If it doesn't exist, you can't open it.
Title: Re: GoTo Error
Post by: Yegg on October 21, 2004, 07:38 PM
i know this, i want the msgbox to come up when it doesn't exist and some1 tries to open it.
Title: Re: GoTo Error
Post by: Dyndrilliac on October 21, 2004, 08:07 PM
Most non void type API functions return a value of zero when they fail. Try:

If ShellExecute(Me.hWnd, vbNullString, App.Path & "\users.txt", vbNullString, "C:\", SW_SHOWNORMAL) = 0 Then MsgBox "There is no file dumbass." Else Exit Sub

Of course that works on the assumption this API works the same as most others. You could search MSDN to see if it returns anything in the case that it errors.
Title: Re: GoTo Error
Post by: UserLoser. on October 21, 2004, 08:52 PM
Example:


Dim ErrorCode As Long

ErrorCode = ShellExecute(hWnd, vbNullString, App.Path & "\Users.txt", vbNullString, vbNullString, SW_SHOWNORMAL)

Select Case ErrorCode
    Case Is > 32:
        'Successfully executed
    Case 0, SE_ERR_OOM:
        'We're out of memory
    Case ERROR_FILE_NOT_FOUND, SE_ERR_FNF:
        'File isn't found
    Case SE_ERR_ACCESSDENIED:
        'Access denied
    Case SE_ERR_SHARE:
        'Sharing violation

    '*Todo: _YOU_ finish handling, see MSDN*

    Case Else:
        'Unrecognized error, this shouldn't happen
End Select


ShellExecute (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/shellexecute.asp)
Title: Re: GoTo Error
Post by: Grok on October 21, 2004, 09:29 PM
Just check if the file exists before trying to launch it?  I prefer FileSystemObject.  Go to project references, check the box beside "Microsoft Scripting Runtime".  Then ...


Private fso as New Scripting.FileSystemObject
'....
If fso.FileExists(fso.BuildPath(App.Path, "users.txt")) = False Then
    MsgBox "File not found!", vbExclamation, "Missing file, duh."
Else
    ErrorCode = ShellExecute(hWnd, vbNullString, App.Path & "\Users.txt", vbNullString, vbNullString, SW_SHOWNORMAL)
End If
Title: Re: GoTo Error
Post by: phvckmeh on October 21, 2004, 11:48 PM
this is what i do to open txt files

Dim strPath as string
Shell "notepad " & strPath, vbNormalFocus


it always works.
Title: Re: GoTo Error
Post by: Grok on October 22, 2004, 06:24 AM
Quote from: phvckmeh on October 21, 2004, 11:48 PM
this is what i do to open txt files

Dim strPath as string
Shell "notepad " & strPath, vbNormalFocus


it always works.

Doesn't make it right.

Windows is document-centric.  You are not supposed to specify the application by which a document is to be opened for the user, but the other way around.  You ask the shell to find the appropriate server for the document type and open it.

Also, checking if the file exists prior to opening is just one way of writing good code.
Title: Re: GoTo Error
Post by: Adron on October 22, 2004, 02:15 PM
Quote from: Grok on October 21, 2004, 09:29 PM
Just check if the file exists before trying to launch it?  I prefer FileSystemObject.  Go to project references, check the box beside "Microsoft Scripting Runtime".  Then ...

What about If Dir(filename) > "" Then?
Title: Re: GoTo Error
Post by: LivedKrad on October 22, 2004, 04:38 PM
Do you mean Len()?
Title: Re: GoTo Error
Post by: Grok on October 22, 2004, 10:08 PM
Quote from: Adron on October 22, 2004, 02:15 PM
Quote from: Grok on October 21, 2004, 09:29 PM
Just check if the file exists before trying to launch it?  I prefer FileSystemObject.  Go to project references, check the box beside "Microsoft Scripting Runtime".  Then ...

What about If Dir(filename) > "" Then?


That works too.  Any check is better than no check.  Does Dir(filename) handle UNC paths?  I use FileSystemObject for so much else that calling FileExists is not a costly addition.
Title: Re: GoTo Error
Post by: Adron on October 23, 2004, 07:54 AM
Quote from: Grok on October 22, 2004, 10:08 PM
That works too.  Any check is better than no check.  Does Dir(filename) handle UNC paths?  I use FileSystemObject for so much else that calling FileExists is not a costly addition.

It should handle UNC paths. It just seemed confusing to call something external to do what VB could do itself.