|
-
Sep 7th, 2004, 01:52 AM
#1
Thread Starter
Member
Run Files [Resolved]
Hi,
I am having problems running a file. What I have now is a textbox which shows some batch commands. I want to run the commands in the textbox upon pressing a run button. So how do I program the run button to make it capture the text from the textbox and run it? I couldn't even seem to cature the text within the textbox. Is some1 familiar with this?
Last edited by iori85z; Sep 23rd, 2004 at 10:50 PM.
-
Sep 7th, 2004, 02:08 AM
#2
u access textbox's text like this;
'put this code in Run button click
Msgbox Text1.Text
Batch commands ? u mean dos commands ? want to execute dos commands from vb ?
-
Sep 7th, 2004, 02:14 AM
#3
Thread Starter
Member
Actually, the text the user input into the textbox will be first saved to .vbs (vb script) format using a save button, and i have already got tat working. When the user press the run button, it will run the vbs file. I think i post wrongly juz now, it should be tat the run button will run the vbs file which has been saved. So is it possible?
-
Sep 7th, 2004, 02:28 AM
#4
here u go
VB Code:
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 = 0
Private Const SW_SHOWNORMAL = 1
Private Sub Command1_Click()
ShellExecute vbNull, vbNullString, "c:\SayHello.vbs", vbNullString, vbNullString, SW_HIDE
End Sub
SayHello.vbs:
MsgBox "Hello"
-
Sep 7th, 2004, 02:49 AM
#5
Thread Starter
Member
I tried your code, when I press the run button, there is no error msg, however, the vbs file did not run. I tried different path and even replaced it to try open a txt file but nothing opened or run as well?? Muz I declare more variables or I mus add some more code to it?
Last edited by iori85z; Oct 11th, 2004 at 09:11 PM.
-
Sep 7th, 2004, 03:42 AM
#6
bcoz it runs in hidden mode. use this
VB Code:
ShellExecute vbNull, vbNullString, "c:\SayHello.vbs", vbNullString, vbNullString, SW_SHOWNORMAL
-
Sep 8th, 2004, 02:09 AM
#7
Thread Starter
Member
Ok. I get wat u mean. What if I don't want to set the file to be open to be always the same? Can it like when I press the run button, it will open out a selection window to let me select from several vbs files and let me choose 1 to run? It uses the common dialog rite? How do I modify the code?
-
Sep 8th, 2004, 02:33 AM
#8
VB Code:
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 = 0
Private Const SW_SHOWNORMAL = 1
Private Sub Form_Load()
cdlOpen.Flags = cdlOFNFileMustExist
cdlOpen.CancelError = False
cdlOpen.Filter = "Text (*.vbs)|*.vbs"
cdlOpen.ShowOpen
If Len(cdlOpen.FileName) = 0 Then Exit Sub
ShellExecute vbNull, vbNullString, cdlOpen.FileName, vbNullString, vbNullString, SW_SHOWNORMAL
End Sub
-
Sep 8th, 2004, 04:05 AM
#9
Thread Starter
Member
Thanks man, ur code works but now I have another problem. While the vbs file is running, the codes will pop up with the common dialog screen and display the commands on the common dialog textfield. I tried to open a textfile by modifying the extension and it works fine, seems like it will have such error when running vbs file because of the sleep time and the multiple commands. Is there a way to let the commands show in a console window while it is running then close automatically when it finish?
-
Sep 8th, 2004, 04:09 AM
#10
i don't get it
-
Sep 8th, 2004, 11:39 AM
#11
Thread Starter
Member
I mean tat for example, if i use ur code to run a textfile, it will open it in a notepad and everything is smooth. But if I try to run a vbs file, a vbs file has alot of commands which is suppose to be input in DOS or telnet mode, therefore cannot run smoothly and will generate many error messages. The error messages will keep on popping out with the common dialog window.
-
Sep 8th, 2004, 12:01 PM
#12
This will run your vbs file in DOS.
VB Code:
Option Explicit
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 = 0
Private Const SW_SHOWNORMAL = 1
Private Sub Command1_Click()
On Error GoTo No_Bugs
With cdlRun
.Flags = cdlOFNFileMustExist Or cdlOFNHideReadOnly Or cdlOFNPathMustExist
.CancelError = True
.Filter = "VB Script files only (*.vbs)|*.vbs"
.DialogTitle = "Select a script to run"
.ShowOpen
End With
ShellExecute Me.hwnd, "OPEN", "C:\Winnt\System32\cmd.exe", " /C " & cdlRun.FileName, "C:\", SW_SHOWNORMAL
Exit Sub
No_Bugs:
MsgBox "Operation canceled by user!',vbokonly+vbexclamation"
End Sub
If you change the /C to /K then it will keep the command window
open until you close it. /K closes the command window.
You can change the program so it will work on different os
version because XP's location of the cmd.exe is in Windows\system32.
VB/Outlook Guru
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Sep 8th, 2004, 08:10 PM
#13
Thread Starter
Member
Hi RobDog888, i tried to run the code u gave me, I changed the cd1 to the name of my Common Dialog name and run it, but it generate a error at the
.Flags = dlgCommonDialogOFNFileMustExist
line.
The compile error is variable not define. Why is it so? Is it because of the OFN? Btw wat does OFN do and wat is it?
-
Sep 8th, 2004, 08:21 PM
#14
Its an error from copying the code. The flags code line should
read...
VB Code:
.Flags = cdlOFNFileMustExist Or cdlOFNHideReadOnly Or cdlOFNPathMustExist
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Sep 8th, 2004, 08:25 PM
#15
Thread Starter
Member
OK, but my Common Dialog's Name is dlgCommonDialog not cd1, if I change it back to cd1 it also got the same compile error. The other codes are all left unchange. I onli change the name of the common dialog tats all.
Or do i have to change my common dialog's name to cd1 because the code for the flag must have a cd1 name?
-
Sep 8th, 2004, 09:00 PM
#16
I think we are not on the same page so here is the code modified
for your app. Just change the Command1_Click event to the same
name as your button. Do NOT change anything in the .Flags line.
Those are vb constants and not commondialog names.
VB Code:
Option Explicit
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 = 0
Private Const SW_SHOWNORMAL = 1
Private Sub Command1_Click()
On Error GoTo No_Bugs
With dlgCommonDialog
.Flags = cdlOFNFileMustExist Or cdlOFNHideReadOnly Or cdlOFNPathMustExist
.CancelError = True
.Filter = "VB Script files only (*.vbs)|*.vbs"
.DialogTitle = "Select a script to run"
.ShowOpen
End With
ShellExecute Me.hwnd, "OPEN", "C:\Winnt\System32\cmd.exe", " /C " & dlgCommonDialog.FileName, "C:\", SW_SHOWNORMAL
Exit Sub
No_Bugs:
MsgBox "Operation canceled by user!',vbokonly+vbexclamation"
End Sub
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Sep 9th, 2004, 02:21 AM
#17
Thread Starter
Member
Thanks man, I manage to get it working but everytime I cancel it, I figured tat it goes to the On_bug and start a msgbox to show operation canceled. I tried commentin out the On Error Code but it will generate another error. Is there a way tat when I cancel it, it will just cancel away and show no msgbox or error msg?
-
Sep 9th, 2004, 03:20 AM
#18
All you must do is comment out the MsgBox line, or delete it all together:
VB Code:
'Solution A
Option Explicit
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 = 0
Private Const SW_SHOWNORMAL = 1
Private Sub Command1_Click()
On Error GoTo No_Bugs
With dlgCommonDialog
.Flags = cdlOFNFileMustExist Or cdlOFNHideReadOnly Or cdlOFNPathMustExist
.CancelError = True
.Filter = "VB Script files only (*.vbs)|*.vbs"
.DialogTitle = "Select a script to run"
.ShowOpen
End With
ShellExecute Me.hwnd, "OPEN", "C:\Winnt\System32\cmd.exe", " /C " & dlgCommonDialog.FileName, "C:\", SW_SHOWNORMAL
Exit Sub
No_Bugs:
'MsgBox "Operation canceled by user!',vbokonly+vbexclamation"
End Sub
VB Code:
'Solution B
Option Explicit
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 = 0
Private Const SW_SHOWNORMAL = 1
Private Sub Command1_Click()
On Error [b]Resume Next[/b]
With dlgCommonDialog
.Flags = cdlOFNFileMustExist Or cdlOFNHideReadOnly Or cdlOFNPathMustExist
.CancelError = True
.Filter = "VB Script files only (*.vbs)|*.vbs"
.DialogTitle = "Select a script to run"
.ShowOpen
End With
ShellExecute Me.hwnd, "OPEN", "C:\Winnt\System32\cmd.exe", " /C " & dlgCommonDialog.FileName, "C:\", SW_SHOWNORMAL
End Sub
Phreak
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Sep 9th, 2004, 03:38 AM
#19
Thread Starter
Member
Alrite, thanks man, i got it up and working. Thanks for all ur help and time debuggin wif me, I realli appreciate it alot.
-
Sep 9th, 2004, 03:53 AM
#20
Thread Starter
Member
Oh, now tat the code works, the exit button which I have programmed begin to hav an error. It works fine b4 tat but didn't noe y now it give an error when pressed. It says variable not define. The code is:
'Exits program
Private Sub cmdExit_Click()
'Prompt to quit
exitResponse = MsgBox("Quit program?", vbYesNo, "Exit program")
'Check to see if response was a yes
If exitResponse = 6 Then
'Close program
End
End If
End Sub
Is there any problems wif the code but I run it b4 and it works fine. Don't noe y now it generate an error. Is there any problems or bugs? Or is there another way to quit the program in this way (have a msgbox to confirm quiting b4 realli exiting) w/o any error?
-
Sep 9th, 2004, 03:58 AM
#21
VB Code:
'Exits program
Private Sub cmdExit_Click()
'Prompt to quit
Dim exitResponse As Integer
exitResponse = MsgBox("Quit program?", vbYesNo, "Exit program")
'Check to see if response was a yes
If exitResponse = 6 Then
'Close program
End
End If
End Sub
-
Sep 9th, 2004, 04:00 AM
#22
never use End
use this:
VB Code:
Dim iCtr As Integer
For iCtr = 0 To Forms.Count - 1
Unload Forms(iCtr)
Next
-
Sep 9th, 2004, 04:08 AM
#23
This is easier than knowing the values off by heart:
VB Code:
'Check to see if response was a yes
If exitResponse = vbYes Then
Just so you know, check the Object Browser.
Phreak
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Sep 9th, 2004, 05:57 AM
#24
Correct me if I'm wrong, but I thought batch commands were for batch files with the .bat extention. The .vbs extention is for VBScript.
-
Sep 9th, 2004, 06:00 AM
#25
yeh..the .vbs extention is for VBScript.
-
Sep 9th, 2004, 08:17 PM
#26
Thread Starter
Member
Alritey, thanks every1, i got it working.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|