• Welcome to Valhalla Legends Archive.
 

GoTo Error

Started by Yegg, October 21, 2004, 05:51 PM

Previous topic - Next topic

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?

drivehappy


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

Yegg

ok, wel theres a problem with that code, no matter wut happens the message box pops up.

CrAz3D

Did you notice the directory that drivehappy used?... C:\...unless your application's directory is C:\ also it will not work.
rebundance - having or being in excess of sheer stupidity
(ré-bun-dance)
Quote from: Spht on June 22, 2004, 07:32 PMSlap.
Quote from: Adron on January 28, 2005, 09:17 AMIn a way, I believe that religion is inherently evil, which includes Christianity. I'd also say Christianity is eviller than Buddhism (has more potential for evil).
Quote from: iago on April 19, 2005, 01:06 PM
CrAz3D's ... is too big vertically, at least, too big with ... iago ...

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?

Dyndrilliac

#5
If Dir$(App.Path & "\users.txt") = vbNullString Then
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

UserLoser.

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!"

Dyndrilliac

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.
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

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

UserLoser.

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.

Yegg

i know this, i want the msgbox to come up when it doesn't exist and some1 tries to open it.

Dyndrilliac

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.
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

UserLoser.

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

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 ...


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

phvckmeh

this is what i do to open txt files

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


it always works.