Valhalla Legends Forums Archive | Visual Basic Programming | GoTo Error

AuthorMessageTime
Yegg
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?
October 21, 2004, 10:51 PM
drivehappy
[code]
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
[/code]
EDIT: code tags
October 21, 2004, 11:09 PM
Yegg
ok, wel theres a problem with that code, no matter wut happens the message box pops up.
October 21, 2004, 11:15 PM
CrAz3D
Did you notice the directory that drivehappy used?... C:\...unless your application's directory is C:\ also it will not work.
October 21, 2004, 11:39 PM
Yegg
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?
October 21, 2004, 11:43 PM
Dyndrilliac
[code]If Dir$(App.Path & "\users.txt") = vbNullString Then[/code]
October 21, 2004, 11:46 PM
UserLoser.
[quote author=Yegg link=topic=9272.msg85569#msg85569 date=1098399092]
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?
[/quote]

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!"
October 21, 2004, 11:48 PM
Dyndrilliac
[quote author=UserLoser link=topic=9272.msg85583#msg85583 date=1098402514]
[quote author=Yegg link=topic=9272.msg85569#msg85569 date=1098399092]
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?
[/quote]

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!"
[/quote]

Goto is evil though.
October 21, 2004, 11:50 PM
Yegg
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
October 21, 2004, 11:56 PM
UserLoser.
[quote author=Yegg link=topic=9272.msg85588#msg85588 date=1098403002]
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
[/quote]

If it doesn't exist, you can't open it.
October 22, 2004, 12:26 AM
Yegg
i know this, i want the msgbox to come up when it doesn't exist and some1 tries to open it.
October 22, 2004, 12:38 AM
Dyndrilliac
Most non void type API functions return a value of zero when they fail. Try:

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

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.
October 22, 2004, 1:07 AM
UserLoser.
Example:

[code]
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
[/code]

ShellExecute
October 22, 2004, 1:52 AM
Grok
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 ...

[code]
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
[/code]
October 22, 2004, 2:29 AM
phvckmeh
this is what i do to open txt files
[code]
Dim strPath as string
Shell "notepad " & strPath, vbNormalFocus
[/code]

it always works.
October 22, 2004, 4:48 AM
Grok
[quote author=phvckmeh link=topic=9272.msg85641#msg85641 date=1098420512]
this is what i do to open txt files
[code]
Dim strPath as string
Shell "notepad " & strPath, vbNormalFocus
[/code]

it always works.
[/quote]

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.
October 22, 2004, 11:24 AM
Adron
[quote author=Grok link=topic=9272.msg85633#msg85633 date=1098412159]
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 ...
[/quote]

What about If Dir(filename) > "" Then?
October 22, 2004, 7:15 PM
LivedKrad
Do you mean Len()?
October 22, 2004, 9:38 PM
Grok
[quote author=Adron link=topic=9272.msg85676#msg85676 date=1098472501]
[quote author=Grok link=topic=9272.msg85633#msg85633 date=1098412159]
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 ...
[/quote]

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

[/quote]

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.
October 23, 2004, 3:08 AM
Adron
[quote author=Grok link=topic=9272.msg85745#msg85745 date=1098500881]
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.
[/quote]

It should handle UNC paths. It just seemed confusing to call something external to do what VB could do itself.
October 23, 2004, 12:54 PM

Search