Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calling a .vbs file from excel 1

Status
Not open for further replies.

amberH

Programmer
Jun 10, 2002
36
CA
Hi all,
Is it possible to call a .vbs file by coding a button in
excel?
I have a .vbs file that installs a printer on the user's
computer. I have many of these files, each installing a
different printer. I was hoping to set it up in an excel
sheet, with buttons to call each file.
Possible?
Thanks,
amber

 
The following code should run a script called SelectPrinterA.vbs stored in a folder C:\Scripts\.

Dim oShell
Set oShell = CreateObject("WSCript.shell")
oShell.Run "WScript C:\Scripts\SelectPrinterA.vbs"
Set oShell = Nothing

It should be fairly easy to adapt you scripts to VBA so that you could run them directly from Excel.

A.C.
 
Thanks acron,
I was actually trying at first to put all the code in Excel, but I know very little about this.
I have the following VB code, but it won't run as VBA code. I run into trouble with the section "set WshNetwork = WScript.CreateObject("WScript.Network")" line.

Option Explicit

Sub addPrinter()

Const printer = "\\mkntsad1\ap_hp4050"

Dim Text, Title, icon
Dim WshNetwork ' Object variable

Title = "Mackenzie Information Technologies - Printer Mapping Script"

' Create a new WshNetwork object to access network properties.
Set WshNetwork = WScript.CreateObject("WScript.Network")

On Error Resume Next

WshNetwork.AddWindowsPrinterConnection printer

Select Case Err.Number
Case 0
Text = "Printer connected to """ & printer & """."
icon = vbInformation
Case -2147023688
Text = "Error: Network resource """ & _
printer & """ doesn't exist."
icon = vbCritical
Case -2147024811
Text = "Error: Mapping to """ & printer & """ already exists."
icon = vbCritical
Case Else
Text = "Error: Code " & Err.Number & " " & Err.Description
icon = vbCritical
End Select

On Error GoTo 0 ' Enable run-time error handling.

MsgBox Text, vbOKOnly + icon, Title

'*** End


End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top