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.
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:
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_SHOWNORMAL As Long = 1
Private Const SW_HIDE As Long = 0
Private Sub Command1_Click()
ShellExecute Me.hwnd, "Open", "C:\MyVBScriptFile.vbs", vbNullString, "C:\", SW_SHOWNORMAL
End Sub
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'
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..
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
Re: Running a vbscript from a VB Button
lol @
Quote:
hmm i think you have to declare it first..
Re: Running a vbscript from a VB Button
you dont dim hwnd.
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_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
your whole code
Re: Running a vbscript from a VB Button
But what do I do about the error:
'hwnd' is not a member of 'vb.form1'
?
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:
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
Re: Running a vbscript from a VB Button
The ( ) 's get re-inserted automatically.
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:
dim result as integer
result = shellexecute (...)
if result = 0 then
msgbox "Sucess!"
endif
otherwise, just use it like this
VB Code:
shellexecute Me.hwnd, ...
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.
Re: Running a vbscript from a VB Button
Oh. Didn't realize that Net added brackets to your code. Sorry.
Re: Running a vbscript from a VB Button
No need to appologize dg, we didnt know that until post #10. :)
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