Results 1 to 15 of 15

Thread: Running a vbscript from a VB Button

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    10

    Running a vbscript from a VB Button

    I am new to Visual Basic. Here's what I want to do:

    I have several vbscripts that I have written. I want to use visual basic to create a form with several buttons. Each button would run one of my scripts. I can't figure out how to get a button to launch a vbscript. Any help is appreciated.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Running a vbscript from a VB Button

    Welcome to the Forums.

    You can use the ShellExecute API to run a vbs file.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
    4. ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    5.  
    6. Private Const SW_SHOWNORMAL As Long = 1
    7. Private Const SW_HIDE As Long = 0
    8.  
    9. Private Sub Command1_Click()
    10.     ShellExecute Me.hwnd, "Open", "C:\MyVBScriptFile.vbs", vbNullString, "C:\", SW_SHOWNORMAL
    11. 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

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    10

    Re: Running a vbscript from a VB Button

    Thanks for the response. I am getting an error:

    'hwnd' is not a member of 'vb.form1'

  4. #4
    Lively Member
    Join Date
    Mar 2005
    Posts
    109

    Re: Running a vbscript from a VB Button

    Quote Originally Posted by mattw555
    Thanks for the response. I am getting an error:

    'hwnd' is not a member of 'vb.form1'
    hmm i think you have to declare it first..

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    10

    Re: Running a vbscript from a VB Button

    OK so I declared it using dim. It compiles now but the button does nothing. Here is my code:


    Dim hwnd
    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_SHOWNORMAL As Long = 1
    Private Const SW_HIDE As Long = 0
    Private Sub Button1_Click()

    ShellExecute(Me.hwnd, "Open", "C:\vb\vb\replace.vbs", vbNullString, "C:\vb\vb\", SW_SHOWNORMAL)
    End Sub

  6. #6
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Running a vbscript from a VB Button

    lol @
    hmm i think you have to declare it first..

  7. #7
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Running a vbscript from a VB Button

    you dont dim hwnd.

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

    your whole code

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    10

    Re: Running a vbscript from a VB Button

    But what do I do about the error:

    'hwnd' is not a member of 'vb.form1'

    ?

  9. #9
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Running a vbscript from a VB Button

    Just take the ( ) 's off of the ShellExecute statement. You only need them if you have a return value to see if it worked (= 0)

    This will work.
    VB Code:
    1. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
    2. ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    3.  
    4. Private Const SW_SHOWNORMAL As Long = 1
    5. Private Const SW_HIDE As Long = 0
    6.  
    7. Private Sub Button1_Click()
    8.  
    9. ShellExecute Me.hwnd, "Open", "C:\vb\vb\replace.vbs", vbNullString, "C:\vb\vb\", SW_SHOWNORMAL
    10.  
    11. End Sub

  10. #10

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    10

    Re: Running a vbscript from a VB Button

    The ( ) 's get re-inserted automatically.

  11. #11
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Running a vbscript from a VB Button

    they do NOT. Paste in my code and press F5

    if you want to use the (), then you do it like this.

    VB Code:
    1. dim result as integer
    2. result = shellexecute (...)
    3. if result = 0 then
    4.   msgbox "Sucess!"
    5. endif

    otherwise, just use it like this

    VB Code:
    1. shellexecute Me.hwnd, ...

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Running a vbscript from a VB Button

    "The ( ) 's get re-inserted automatically." Then this is VB.NET.
    You need to create a process and pass the filepath and name to it.
    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
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Running a vbscript from a VB Button

    Oh. Didn't realize that Net added brackets to your code. Sorry.

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Running a vbscript from a VB Button

    No need to appologize dg, we didnt know that until post #10.
    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
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Running a vbscript from a VB Button

    The hWnd in VB.Net is now called Handle

    They renamed nearly all the properties in VB.NET

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