Installing printer in VBA, if not already installed

B

Beeawwb

Hi everybody,

I've got an addition I'm writing into an existing module that's been working
great. We got some new print queues though, and I need to cater for these in
my code.

Essentially, if a button is pushed, I want to force the printer to be it's
printer name + an extension based on which button was pushed. So "Printer1"
becomes "Printer1-Button1"

This all works great, and I'm not having any problems with it... as long as
the printer is already installed.

If the printer is not installed, it was throwing an error and for some
reason isn't any more. I was planning on trapping for the error, and then
using that to install the printer. Doesn't seem that this will be an option
now.

So my question is 2 fold.

How do I A) Check if a printer is installed (I can check if a local printer
is installed by reading the registry, but this doesn't seem to be working for
network printers) and B) install the printer if it is not.

I have checked Jonathan West's excellent code at LogicalExpressions (I
already use a portion of this to get printer bin names, thanks!) and it seems
like I may get the information I require at
http://pubs.logicalexpressions.com/pub0009/LPMArticle.asp?ID=183 and Astrid
Zeelenberg's code at
http://www.mvps.org/word/FAQs/MacrosVBA/AvailablePrinters.htm
....
but I'm not really understanding how I can use this to check the printer
name against the array. I suppose I'd be doing a For each X in the Array,
check myprinter against X, Next X. But I've never done a For loop in an
Array. (Or... I have, but I'm running on 2 short black's and 3 hours sleep
and simply can't remember. One of the two)

Any ideas?

Thanks in advance for your expertise as always,

Bob
 
B

Beeawwb

Sorry, a closer reading of the code has shown me how I can check if the
printer is installed, and that's working fine. So the only question that
remains is...

How do I install a printer (keeping in mind that I will always know the
server and printer name I need to install (eg \\sadlps002\psa071-s)) from VBA?

Cheers,

Bob
 
J

Jonathan West

Beeawwb said:
Sorry, a closer reading of the code has shown me how I can check if the
printer is installed, and that's working fine. So the only question that
remains is...

How do I install a printer (keeping in mind that I will always know the
server and printer name I need to install (eg \\sadlps002\psa071-s)) from
VBA?

Hi Bob

this article shows how to get the list of installed printers.

Getting names of available printers
http://www.word.mvps.org/FAQs/MacrosVBA/AvailablePrinters.htm
 
B

Beeawwb

Hi Jonathan,

Yes, I did use that section to determine if the printer was installed or
not. I've used your article on Printers from pubs.logicalexpressions many
many times, it's on my favorites list at work.

What I need to do now is, install the printer if it's not matched.

What I do is, if the array matches what I expect to find
(\\sadlps002\psa071-s) I mark a flag True. (bPrinterInstalled = True) Once
the array has finished, if bPrinterInstalled <> True, then I would want to
install the printer I expected to find. Is there any way to do this from VBA?
The only way I found was to call the install printers dialogue using the API
and do a series of SendKeys statements to install the needed printer. While
it works, it's an ugly solution and prone to failure.

Thanks again,

Bob
 
J

Jonathan West

Beeawwb said:
Hi Jonathan,

Yes, I did use that section to determine if the printer was installed or
not. I've used your article on Printers from pubs.logicalexpressions many
many times, it's on my favorites list at work.

What I need to do now is, install the printer if it's not matched.

What I do is, if the array matches what I expect to find
(\\sadlps002\psa071-s) I mark a flag True. (bPrinterInstalled = True) Once
the array has finished, if bPrinterInstalled <> True, then I would want to
install the printer I expected to find. Is there any way to do this from
VBA?
The only way I found was to call the install printers dialogue using the
API
and do a series of SendKeys statements to install the needed printer.
While
it works, it's an ugly solution and prone to failure.

You can probably adapt the code here

AddPrinter: Add/Delete Local/Remote Printers using Existing Drivers
http://vbnet.mvps.org/index.html?code/system/addprinter.htm

It is VB6 rather than VBA code, but if you take the code out of the Form
module and make a separate routine of it, or you adapt the routine names so
that you are using a UserForm instead of a VB Form, it should all work fine.
 
B

Beeawwb

Ok, so I'm back at work and testing out the code. It at least runs, which is
good. I guess the next step is to figure out why it's not installing.

What I've done is adapt the code so it's not form based. Command1_Click
becomes Private Sub InstallP(), for now. Removed references to forms and
changed captions to msg boxes.

Now, I can't be sure, but I assume it's one of the variables set in the
"With pi2" statement. I assume that one of these is incorrect, and it's
causing the AddPrinter to fail.

I've reproduced my code here, with some of my own comments added. Your
thoughts would be greatly appreciated trying to get this working. I should
note, most of this info I've gotten from installing the printer myself,
printing the driver settings and then deleting it again (so I'm not trying to
install a duplicate).

Private Sub InstallP()

Dim hPrinter As Long
Dim sServer As String
Dim pi2 As PRINTER_INFO_2

With pi2

'local machine
.pServerName = "\\sadlps002" <---I've put the print server, since
it's not a local printer?

'display name for new printer
.pPrinterName = "PSA071-SGIC on sadlps002" <--- Default display name

'name for share - must be upper-case
.pShareName = "PSA071-SGIC" <---Correct share name

'printer port (NT or later can
'have multiple separated with
'commas, e.g. "LPT1:,LPT2:")
.pPortName = "PSA071.AUIAG.CORP" <---As indicated by driver settings

'the name of an existing driver
.pDriverName = "fx6naeim.dll" <---As indicated from printout.

'a comment, if desired
.pComment = "PI (7.21 N/E) - SGIC Letterhead (Tray 2)" <---Comment
that exists on the network printer

'the location, if desired
.pLocation = "SA/80 Flinders St/07 Floor" <---As on the network printer.

'the print processor name - manditory
.pPrintProcessor = "WinPrint" <---<b>No idea...</b>

'type of data supported, e.g. RAW or EMF
'this must match type used by the driver
'- manditory
.pDatatype = "RAW" <---As indicated on driver settings.

'flags - local printer, with share created <---Not changed
.Attributes = PRINTER_ATTRIBUTE_LOCAL Or _
PRINTER_ATTRIBUTE_SHARED

End With


'install on the local machine by passing
'vbNullString as the server name. If
'installation on another machine is
'to be made, pass the machine name
'in the usual 'server name format',
'eg: sServer = "\\laptopxp"
sServer = "\\sadlps002" ---Is it wanting the print server, or are we
installing on "the local machine", I'm not able to determine from the
comments.

hPrinter = AddPrinter(sServer, PRINTER_LEVEL2, pi2)

Debug.Print hPrinter, Err.LastDllError

If hPrinter <> 0 Then

MsgBox ("Printer added successfully")
ClosePrinter hPrinter

End If

End Sub
 
B

Beeawwb

I wouldn't have beleived it if I didn't just see it myself, but I found some
code in the MS knowledge base whcih worked perfectly. I just need to specify
the driver name for each printer, which I can store in an Ini file and read
later.

Set WshNetwork = CreateObject("WScript.Network")
PrinterPath = "\\Server\Printer"
PrinterDriver = "PrinterDriver"
WshNetwork.AddWindowsPrinterConnection PrinterPath, PrinterDriver

Thanks again for your help Jonathan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top