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?
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
ok, wel theres a problem with that code, no matter wut happens the message box pops up.
Did you notice the directory that drivehappy used?... C:\...unless your application's directory is C:\ also it will not work.
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?
If Dir$(App.Path & "\users.txt") = vbNullString Then
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!"
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.
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 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.
i know this, i want the msgbox to come up when it doesn't exist and some1 tries to open it.
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.
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)
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
this is what i do to open txt files
Dim strPath as string
Shell "notepad " & strPath, vbNormalFocus
it always works.
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.
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?
Do you mean Len()?
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.
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.