Results 1 to 26 of 26

Thread: Run Files [Resolved]

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2004
    Posts
    60

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

  2. #2
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424
    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 ?

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2004
    Posts
    60
    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?

  4. #4
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    here u go

    VB Code:
    1. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    2. (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
    3. ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    4.  
    5. Private Const SW_HIDE = 0
    6. Private Const SW_SHOWNORMAL = 1
    7.  
    8. Private Sub Command1_Click()
    9.     ShellExecute vbNull, vbNullString, "c:\SayHello.vbs", vbNullString, vbNullString, SW_HIDE
    10. End Sub

    SayHello.vbs:

    MsgBox "Hello"

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2004
    Posts
    60
    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.

  6. #6
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424
    bcoz it runs in hidden mode. use this
    VB Code:
    1. ShellExecute vbNull, vbNullString, "c:\SayHello.vbs", vbNullString, vbNullString, SW_SHOWNORMAL

  7. #7

    Thread Starter
    Member
    Join Date
    Sep 2004
    Posts
    60
    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?

  8. #8
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424
    VB Code:
    1. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    2. (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
    3. ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    4.  
    5. Private Const SW_HIDE = 0
    6. Private Const SW_SHOWNORMAL = 1
    7.  
    8. Private Sub Form_Load()
    9.     cdlOpen.Flags = cdlOFNFileMustExist
    10.     cdlOpen.CancelError = False
    11.     cdlOpen.Filter = "Text (*.vbs)|*.vbs"
    12.     cdlOpen.ShowOpen
    13.     If Len(cdlOpen.FileName) = 0 Then Exit Sub
    14.     ShellExecute vbNull, vbNullString, cdlOpen.FileName, vbNullString, vbNullString, SW_SHOWNORMAL
    15. End Sub

  9. #9

    Thread Starter
    Member
    Join Date
    Sep 2004
    Posts
    60
    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?

  10. #10
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424
    i don't get it

  11. #11

    Thread Starter
    Member
    Join Date
    Sep 2004
    Posts
    60
    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.

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    This will run your vbs file in DOS.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    4. (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
    5. ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    6.  
    7. Private Const SW_HIDE = 0
    8. Private Const SW_SHOWNORMAL = 1
    9.  
    10. Private Sub Command1_Click()
    11.     On Error GoTo No_Bugs
    12.     With cdlRun
    13.         .Flags = cdlOFNFileMustExist Or cdlOFNHideReadOnly Or cdlOFNPathMustExist
    14.         .CancelError = True
    15.         .Filter = "VB Script files only (*.vbs)|*.vbs"
    16.         .DialogTitle = "Select a script to run"
    17.         .ShowOpen
    18.     End With
    19.     ShellExecute Me.hwnd, "OPEN", "C:\Winnt\System32\cmd.exe", " /C " & cdlRun.FileName, "C:\", SW_SHOWNORMAL
    20.     Exit Sub
    21. No_Bugs:
    22.     MsgBox "Operation canceled by user!',vbokonly+vbexclamation"
    23. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  13. #13

    Thread Starter
    Member
    Join Date
    Sep 2004
    Posts
    60
    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?

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Its an error from copying the code. The flags code line should
    read...

    VB Code:
    1. .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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  15. #15

    Thread Starter
    Member
    Join Date
    Sep 2004
    Posts
    60
    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?

  16. #16
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    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:
    1. Option Explicit
    2.  
    3. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    4. (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
    5. ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    6.  
    7. Private Const SW_HIDE = 0
    8. Private Const SW_SHOWNORMAL = 1
    9.  
    10. Private Sub Command1_Click()
    11.     On Error GoTo No_Bugs
    12.     With dlgCommonDialog
    13.         .Flags = cdlOFNFileMustExist Or cdlOFNHideReadOnly Or cdlOFNPathMustExist
    14.         .CancelError = True
    15.         .Filter = "VB Script files only (*.vbs)|*.vbs"
    16.         .DialogTitle = "Select a script to run"
    17.         .ShowOpen
    18.     End With
    19.     ShellExecute Me.hwnd, "OPEN", "C:\Winnt\System32\cmd.exe", " /C " & dlgCommonDialog.FileName, "C:\", SW_SHOWNORMAL
    20.     Exit Sub
    21. No_Bugs:
    22.     MsgBox "Operation canceled by user!',vbokonly+vbexclamation"
    23. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  17. #17

    Thread Starter
    Member
    Join Date
    Sep 2004
    Posts
    60
    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?

  18. #18
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    All you must do is comment out the MsgBox line, or delete it all together:
    VB Code:
    1. 'Solution A
    2. Option Explicit
    3.  
    4. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    5. (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
    6. ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    7.  
    8. Private Const SW_HIDE = 0
    9. Private Const SW_SHOWNORMAL = 1
    10.  
    11. Private Sub Command1_Click()
    12.     On Error GoTo No_Bugs
    13.     With dlgCommonDialog
    14.         .Flags = cdlOFNFileMustExist Or cdlOFNHideReadOnly Or cdlOFNPathMustExist
    15.         .CancelError = True
    16.         .Filter = "VB Script files only (*.vbs)|*.vbs"
    17.         .DialogTitle = "Select a script to run"
    18.         .ShowOpen
    19.     End With
    20.     ShellExecute Me.hwnd, "OPEN", "C:\Winnt\System32\cmd.exe", " /C " & dlgCommonDialog.FileName, "C:\", SW_SHOWNORMAL
    21.     Exit Sub
    22. No_Bugs:
    23.     'MsgBox "Operation canceled by user!',vbokonly+vbexclamation"
    24. End Sub
    VB Code:
    1. 'Solution B
    2. Option Explicit
    3.  
    4. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    5. (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
    6. ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    7.  
    8. Private Const SW_HIDE = 0
    9. Private Const SW_SHOWNORMAL = 1
    10.  
    11. Private Sub Command1_Click()
    12.     On Error [b]Resume Next[/b]
    13.     With dlgCommonDialog
    14.         .Flags = cdlOFNFileMustExist Or cdlOFNHideReadOnly Or cdlOFNPathMustExist
    15.         .CancelError = True
    16.         .Filter = "VB Script files only (*.vbs)|*.vbs"
    17.         .DialogTitle = "Select a script to run"
    18.         .ShowOpen
    19.     End With
    20.     ShellExecute Me.hwnd, "OPEN", "C:\Winnt\System32\cmd.exe", " /C " & dlgCommonDialog.FileName, "C:\", SW_SHOWNORMAL
    21. End Sub
    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

  19. #19

    Thread Starter
    Member
    Join Date
    Sep 2004
    Posts
    60
    Alrite, thanks man, i got it up and working. Thanks for all ur help and time debuggin wif me, I realli appreciate it alot.

  20. #20

    Thread Starter
    Member
    Join Date
    Sep 2004
    Posts
    60
    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?

  21. #21
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424
    VB Code:
    1. 'Exits program
    2. Private Sub cmdExit_Click()
    3.     'Prompt to quit
    4.     Dim exitResponse As Integer
    5.     exitResponse = MsgBox("Quit program?", vbYesNo, "Exit program")
    6.    
    7.     'Check to see if response was a yes
    8.     If exitResponse = 6 Then
    9.         'Close program
    10.         End
    11.     End If
    12. End Sub

  22. #22
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    never use End

    use this:
    VB Code:
    1. Dim iCtr As Integer
    2. For iCtr = 0 To Forms.Count - 1
    3.     Unload Forms(iCtr)
    4. Next

  23. #23
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    This is easier than knowing the values off by heart:
    VB Code:
    1. 'Check to see if response was a yes
    2. If exitResponse = vbYes Then
    Just so you know, check the Object Browser.

    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

  24. #24
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349
    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.

  25. #25
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424
    yeh..the .vbs extention is for VBScript.

  26. #26

    Thread Starter
    Member
    Join Date
    Sep 2004
    Posts
    60
    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
  •  



Click Here to Expand Forum to Full Width