Andreas
Andreas

Reputation: 23968

Add printer to system using Powershell within VBA?

I can make it work with a batch file using:

powershell.exe "Add-printer -ConnectionName \\PS02.samba.net\HELPS006"

But if I try to run the bat file above with VBA, it does not add the printer:

Shell ("c:\Fraktsedlar\HELPS006.bat")

If I run powershell directly from VBA, none of these works either:

Shell ("powershell.exe -Command " & Chr(34) & "Add-printer -ConnectionName \\PS02.samba.net\HELPS006" & Chr(34))

Shell ("powershell.exe -Command {" & Chr(34) & "Add-printer -ConnectionName \\PS02.samba.net\HELPS006" & Chr(34) & "}")

Shell ("powershell.exe -Command " & Chr(34) & "{Add-printer -ConnectionName \\PS02.samba.net\HELPS006}" & Chr(34))

What is it I'm doing wrong?

Upvotes: 0

Views: 318

Answers (2)

CatCat
CatCat

Reputation: 491

If powershell can do it so VBA.

From Help

http://download.microsoft.com/download/winscript56/Install/5.6/W982KMeXP/EN-US/scrdoc56en.exe This contains some libraries for system admin. You can use all of Windows Scripting Host features except for the root wscript object (used by a script to communicate with the host). 1000 times as much admin/info can be used with WMI https://msdn.microsoft.com/en-us/library/windows/desktop/aa393262(v=vs.85).aspx.

WSH has a second add printer function. Look at that one.

Adds a Windows-based printer connection to your computer system.

object.AddWindowsPrinterConnection strPrinterPath

object

WshNetwork object.

strPrinterPath

String value indicating the path to the printer connection.

Example

The following code uses the AddWindowsPrinterConnection method to connect a network printer to a Windows NT/2000 computer system.

Set WshNetwork = CreateObject("WScript.Network")
PrinterPath = "\\printserv\DefaultPrinter"
WshNetwork.AddWindowsPrinterConnection PrinterPath

Upvotes: 2

Kirill Pashkov
Kirill Pashkov

Reputation: 3236

Try this:

Shell ("powershell.exe -Command { Add-Printer -ConnectionName \\PS02.samba.net\HELPS006 }")

Or this:

Shell ("powershell.exe -Command " & Chr(34) & "& { Add-Printer -ConnectionName \\PS02.samba.net\HELPS006 }" & Chr(34))

You can also try this which is what Add-Printer does:

$p = Get-WmiObject -List Win32_Printer -EnableAllPrivileges
$p.AddPrinterConnection ("\\PS02.samba.net\HELPS006")

Upvotes: 0

Related Questions