Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default PrintOut Method, Set ActivePrinter with a variable

I would like to have an Excel macro print to a printer defined in a
variable so I dont have to hard code the printer name in the code.

I have been successful with the following code:
ThisWorkbook.PrintOut ActivePrinter:="Printer Name"

What I would like to do is something like:
Set PrinterNameVariable = "Printer Name"
ThisWorkbook.PrintOut ActivePrinter:=PrinterNameVariable

Any ideas?

Thanks

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default PrintOut Method, Set ActivePrinter with a variable


goto http://groups-beta.google.com
then type or copy:
list-of-printers group:*excel* author:keepitcool

you should get a reply from me to Tom dated jan 22nd 2005

it has a function that does it all very nicely (xl2000 or newer)
if you have xl97 search posts from me in this group on topic
"Printers List"




--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Neuraxis wrote :

I would like to have an Excel macro print to a printer defined in a
variable so I dont have to hard code the printer name in the code.

I have been successful with the following code:
ThisWorkbook.PrintOut ActivePrinter:="Printer Name"

What I would like to do is something like:
Set PrinterNameVariable = "Printer Name"
ThisWorkbook.PrintOut ActivePrinter:=PrinterNameVariable

Any ideas?

Thanks

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default PrintOut Method, Set ActivePrinter with a variable

Thank you for your information. I have modified your code below to
create a function called SetActivePrinter which you can pass a string
to in order to set the printer to a string.

Option Explicit


Private Declare Function GetProfileString Lib "kernel32" _
Alias "GetProfileStringA" (ByVal lpAppName As String, _
ByVal lpKeyName As String, ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long) As Long


Sub SetActivePrinter(TargetPrinter As String)

Dim vaList
Dim i As Integer

'Get all printers
vaList = PrinterFind

'Get all printers
vaList = PrinterFind()

MsgBox UBound(vaList)

'Find array index of printer
For i = 0 To UBound(vaList)
If vaList(i) = TargetPrinter Then
Application.ActivePrinter = vaList(i)
End If
Next

End Sub


Public Function PrinterFind() As String()
Dim n%, lRet&, sBuf$, sCon$, aPrn$()
Const lLen& = 1024, sKey$ = "devices"

'Read all installed printers (1k bytes s/b enough)
sBuf = Space(lLen)
lRet = GetProfileString(sKey, vbNullString, vbNullString, sBuf,
lLen)
If lRet = 0 Then
Err.Raise vbObjectError + 513, , "Can't read Profile"
Exit Function
End If

'Split buffer string
aPrn = Split(Left(sBuf, lRet - 1), vbNullChar)

'Return printer array
PrinterFind = aPrn

End Function

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default PrintOut Method, Set ActivePrinter with a variable

Neuraxis..


I think you completely missed the point of my code.
Worse: your code doesnt work.

By removing the localized LOCATION string and the PORT to which the
printer is attached, you've fatally crippled it.

Excel's Activeprinter needs a complete (localized) string to reliably
work, and my (original) function provides it.
For versatility it can return an array for use in comboboxes,
or a filtered list to directly set the printer.


Assuming the target printer string is:

"HP LaserJet 5000 Series PCL6 on NE00:"


The calling code for my function would be:

Application.activeprinter = PrinterFind("laserjet 5000")(0)


it is case insentive
it accepts partial printer names...
it works in all excel language versions.
it works for xl2000+ (adapting it for xl97 is not difficult)


Your adaptation should work like:
SetActivePrinter("HP Laserjet 5000 Series PCL6")
(Case sensitive, complete name required)


I'd stick to my ORIGINAL printerFind, but if you want a wrapper then I
suggest:

Sub SetActivePrinter(sName as string)
dim sOri$
sOri = application.activeprinter
on error resume next
application.activeprinter = PrinterFind(sName)(0)
on error goto 0
if application.activeprinter < sOri then
MsgBox "Couldnt set the activeprinter"
end if

End sub




--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Neuraxis wrote :
Thank you for your information. I have modified your code below to
create a function called SetActivePrinter which you can pass a string
to in order to set the printer to a string.


Option Explicit


Private Declare Function GetProfileString Lib "kernel32" _
Alias "GetProfileStringA" (ByVal lpAppName As String, _
ByVal lpKeyName As String, ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long) As Long


Sub SetActivePrinter(TargetPrinter As String)

Dim vaList
Dim i As Integer

'Get all printers
vaList = PrinterFind

'Get all printers
vaList = PrinterFind()

MsgBox UBound(vaList)

'Find array index of printer
For i = 0 To UBound(vaList)
If vaList(i) = TargetPrinter Then
Application.ActivePrinter = vaList(i)
End If
Next

End Sub


Public Function PrinterFind() As String()
Dim n%, lRet&, sBuf$, sCon$, aPrn$()
Const lLen& = 1024, sKey$ = "devices"

'Read all installed printers (1k bytes s/b enough)
sBuf = Space(lLen)
lRet = GetProfileString(sKey, vbNullString, vbNullString, sBuf,
lLen)
If lRet = 0 Then
Err.Raise vbObjectError + 513, , "Can't read Profile"
Exit Function
End If

'Split buffer string
aPrn = Split(Left(sBuf, lRet - 1), vbNullChar)

'Return printer array
PrinterFind = aPrn

End Function

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA ActivePrinter POM Excel Programming 3 February 23rd 05 10:08 AM
Printout method: in color, duplex [email protected] Excel Programming 3 January 4th 05 08:55 PM
Problem with the PrToFileName parameter when using the Printout method on a worksheet Prasad Vanka Excel Programming 6 May 14th 04 10:56 AM
Excel vba printout method Curtis[_4_] Excel Programming 11 October 28th 03 01:15 PM
Method 'ActivePrinter' of object '_Application' failed kiat Excel Programming 0 August 18th 03 03:45 PM


All times are GMT +1. The time now is 11:29 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"