|
-
Mar 25th, 2005, 05:45 PM
#1
Thread Starter
New Member
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.
-
Mar 25th, 2005, 06:18 PM
#2
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
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 
-
Mar 25th, 2005, 07:11 PM
#3
Thread Starter
New Member
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'
-
Mar 25th, 2005, 07:20 PM
#4
Lively Member
Re: Running a vbscript from a VB Button
 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..
-
Mar 25th, 2005, 07:33 PM
#5
Thread Starter
New Member
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
-
Mar 25th, 2005, 07:33 PM
#6
Re: Running a vbscript from a VB Button
lol @
hmm i think you have to declare it first..
-
Mar 25th, 2005, 07:34 PM
#7
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
-
Mar 25th, 2005, 07:40 PM
#8
Thread Starter
New Member
Re: Running a vbscript from a VB Button
But what do I do about the error:
'hwnd' is not a member of 'vb.form1'
?
-
Mar 25th, 2005, 07:40 PM
#9
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
-
Mar 25th, 2005, 07:48 PM
#10
Thread Starter
New Member
Re: Running a vbscript from a VB Button
The ( ) 's get re-inserted automatically.
-
Mar 25th, 2005, 07:58 PM
#11
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, ...
-
Mar 25th, 2005, 10:42 PM
#12
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 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 
-
Mar 26th, 2005, 01:16 AM
#13
Re: Running a vbscript from a VB Button
Oh. Didn't realize that Net added brackets to your code. Sorry.
-
Mar 26th, 2005, 11:32 AM
#14
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 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 
-
Mar 26th, 2005, 12:25 PM
#15
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|