Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
PO PO is offline
external usenet poster
 
Posts: 66
Default Setting default printer using wsh

Hi

Excel 2000

I use the following wsh script to set the default printer:


Dim oNet
Set oNet =WScript.CreateObject("WScript.Network")

oNet.SetDefaultPrinter("hp LaserJet 1300 PCL 5e")

Set oNet=nothing


(The port name for that printer is DOT4_002)
This works fine and the controlpanel is showing that "hp LaserJet..." is the
default printer. Excel however indicates that the Application.DefaultPrinter
is "unknown". When I set the default printer manually in the controlpanel
Excel recognizes the default printer to be "hp LaserJet 1300 PCL 5e on
Ne02:". I don't understand where Excel gets "on Ne02:" from. The portname
should be DOT4_002.

On some computers Excel still prints correctly to the "hp LaserJet...",
however on others Excel reports an error.

Any ideas?

TIA
po


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Setting default printer using wsh

previously posted by KeepItCool as indicated:


http://groups.google.co.uk/groups?se...&output=gplain


This should produce exactly what you're looking for
but note it will NOT work for xl97

The PrinterFind function will return an array of
localized strings ready to assign to the ActivePrinter.

I've amended an old post from myself to allow filtering.
see test procedure for example of how to use.
On a userform you could simply use s'th like
cboPrinters.list=PrinterFind





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 Test()
Dim vaList
'Get all printers
vaList = PrinterFind
'Show m
MsgBox Join(vaList, vbLf), , "List of printers"

'Get all laserjets
vaList = PrinterFind(Match:="Laserjet")

'Switch to the first laserjet found
If UBound(vaList) = -1 Then
MsgBox "Printer not found"
ElseIf MsgBox("from " & vbTab & ": " & ActivePrinter & vbLf & _
"to " & vbTab & ": " & vaList(0), _
vbOKCancel, "Switch Printers") = vbOK Then
Application.ActivePrinter = vaList(0)
End If
End Sub

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

'------------------------------------------------------------------
'written by keepITcool

'requires xl2000 or newer.
'returns a zerobased array of complete localized printer strings
'results are filtered on Match string, if no result the ubound = -1
'------------------------------------------------------------------

'Split ActivePrinter string to get localized word for "on"
aPrn = Split(Excel.ActivePrinter)
sCon = " " & aPrn(UBound(aPrn) - 1) & " "

'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)
'Filter array on Match
If Match < vbNullString Then aPrn = Filter(aPrn, Match, -1, 1)

For n = LBound(aPrn) To UBound(aPrn)
'Add 16bit portname for each Printer
sBuf = Space(lLen)
lRet = GetProfileString(sKey, aPrn(n), vbNullString, sBuf, lLen)
aPrn(n) = aPrn(n) & sCon & _
Mid(sBuf, InStr(sBuf, ",") + 1, lRet - InStr(sBuf, ","))
Next
'Return the result
PrinterFind = aPrn

End Function

--
Regards,
Tom Ogilvy

"PO" <po wrote in message ...
Hi

Excel 2000

I use the following wsh script to set the default printer:


Dim oNet
Set oNet =WScript.CreateObject("WScript.Network")

oNet.SetDefaultPrinter("hp LaserJet 1300 PCL 5e")

Set oNet=nothing


(The port name for that printer is DOT4_002)
This works fine and the controlpanel is showing that "hp LaserJet..." is

the
default printer. Excel however indicates that the

Application.DefaultPrinter
is "unknown". When I set the default printer manually in the controlpanel
Excel recognizes the default printer to be "hp LaserJet 1300 PCL 5e on
Ne02:". I don't understand where Excel gets "on Ne02:" from. The portname
should be DOT4_002.

On some computers Excel still prints correctly to the "hp LaserJet...",
however on others Excel reports an error.

Any ideas?

TIA
po




  #3   Report Post  
Posted to microsoft.public.excel.programming
PO PO is offline
external usenet poster
 
Posts: 66
Default Setting default printer using wsh

Thanks Tom

The problem is that the code I sent was written in VBScript in a web-page so
I can't use the API call.
The trick to find out the localized setting for "on" was a big help though
since some of our companies Excel versions are in Swedish and some in
English.

However I still need to find out from where to get the correct "suffix",
i.e. hp LaserJet on Ne02. It's called Ne02 on my computer and something else
on other computers. Also the suffix differs depending on which printer I
want to set the default printer.

Any ideas?
Regards
po

"Tom Ogilvy" wrote in message
...
previously posted by KeepItCool as indicated:



http://groups.google.co.uk/groups?se...tcoolnl%40msne
ws.microsoft.com&output=gplain


This should produce exactly what you're looking for
but note it will NOT work for xl97

The PrinterFind function will return an array of
localized strings ready to assign to the ActivePrinter.

I've amended an old post from myself to allow filtering.
see test procedure for example of how to use.
On a userform you could simply use s'th like
cboPrinters.list=PrinterFind





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 Test()
Dim vaList
'Get all printers
vaList = PrinterFind
'Show m
MsgBox Join(vaList, vbLf), , "List of printers"

'Get all laserjets
vaList = PrinterFind(Match:="Laserjet")

'Switch to the first laserjet found
If UBound(vaList) = -1 Then
MsgBox "Printer not found"
ElseIf MsgBox("from " & vbTab & ": " & ActivePrinter & vbLf & _
"to " & vbTab & ": " & vaList(0), _
vbOKCancel, "Switch Printers") = vbOK Then
Application.ActivePrinter = vaList(0)
End If
End Sub

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

'------------------------------------------------------------------
'written by keepITcool

'requires xl2000 or newer.
'returns a zerobased array of complete localized printer strings
'results are filtered on Match string, if no result the ubound = -1
'------------------------------------------------------------------

'Split ActivePrinter string to get localized word for "on"
aPrn = Split(Excel.ActivePrinter)
sCon = " " & aPrn(UBound(aPrn) - 1) & " "

'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)
'Filter array on Match
If Match < vbNullString Then aPrn = Filter(aPrn, Match, -1, 1)

For n = LBound(aPrn) To UBound(aPrn)
'Add 16bit portname for each Printer
sBuf = Space(lLen)
lRet = GetProfileString(sKey, aPrn(n), vbNullString, sBuf, lLen)
aPrn(n) = aPrn(n) & sCon & _
Mid(sBuf, InStr(sBuf, ",") + 1, lRet - InStr(sBuf, ","))
Next
'Return the result
PrinterFind = aPrn

End Function

--
Regards,
Tom Ogilvy

"PO" <po wrote in message ...
Hi

Excel 2000

I use the following wsh script to set the default printer:


Dim oNet
Set oNet =WScript.CreateObject("WScript.Network")

oNet.SetDefaultPrinter("hp LaserJet 1300 PCL 5e")

Set oNet=nothing


(The port name for that printer is DOT4_002)
This works fine and the controlpanel is showing that "hp LaserJet..." is

the
default printer. Excel however indicates that the

Application.DefaultPrinter
is "unknown". When I set the default printer manually in the

controlpanel
Excel recognizes the default printer to be "hp LaserJet 1300 PCL 5e on
Ne02:". I don't understand where Excel gets "on Ne02:" from. The

portname
should be DOT4_002.

On some computers Excel still prints correctly to the "hp LaserJet...",
however on others Excel reports an error.

Any ideas?

TIA
po






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Setting default printer using wsh

the code is getting its information from sys.ini I believe (or win.ini) [in
the system or system32 directories] This is a simple text file that you can
read with your code an pull out the information you need. The .ini file
preceded the implementation of the registry in early verisons of windows,
but it is still updated with the information you need.

The API accesses this file to return the information. If you look at it in
wordpad, you will see the structure.

--
Regards,
Tom Ogilvy


"PO" <po wrote in message ...
Thanks Tom

The problem is that the code I sent was written in VBScript in a web-page

so
I can't use the API call.
The trick to find out the localized setting for "on" was a big help though
since some of our companies Excel versions are in Swedish and some in
English.

However I still need to find out from where to get the correct "suffix",
i.e. hp LaserJet on Ne02. It's called Ne02 on my computer and something

else
on other computers. Also the suffix differs depending on which printer I
want to set the default printer.

Any ideas?
Regards
po

"Tom Ogilvy" wrote in message
...
previously posted by KeepItCool as indicated:




http://groups.google.co.uk/groups?se...tcoolnl%40msne
ws.microsoft.com&output=gplain


This should produce exactly what you're looking for
but note it will NOT work for xl97

The PrinterFind function will return an array of
localized strings ready to assign to the ActivePrinter.

I've amended an old post from myself to allow filtering.
see test procedure for example of how to use.
On a userform you could simply use s'th like
cboPrinters.list=PrinterFind





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 Test()
Dim vaList
'Get all printers
vaList = PrinterFind
'Show m
MsgBox Join(vaList, vbLf), , "List of printers"

'Get all laserjets
vaList = PrinterFind(Match:="Laserjet")

'Switch to the first laserjet found
If UBound(vaList) = -1 Then
MsgBox "Printer not found"
ElseIf MsgBox("from " & vbTab & ": " & ActivePrinter & vbLf & _
"to " & vbTab & ": " & vaList(0), _
vbOKCancel, "Switch Printers") = vbOK Then
Application.ActivePrinter = vaList(0)
End If
End Sub

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

'------------------------------------------------------------------
'written by keepITcool

'requires xl2000 or newer.
'returns a zerobased array of complete localized printer strings
'results are filtered on Match string, if no result the ubound = -1
'------------------------------------------------------------------

'Split ActivePrinter string to get localized word for "on"
aPrn = Split(Excel.ActivePrinter)
sCon = " " & aPrn(UBound(aPrn) - 1) & " "

'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)
'Filter array on Match
If Match < vbNullString Then aPrn = Filter(aPrn, Match, -1, 1)

For n = LBound(aPrn) To UBound(aPrn)
'Add 16bit portname for each Printer
sBuf = Space(lLen)
lRet = GetProfileString(sKey, aPrn(n), vbNullString, sBuf, lLen)
aPrn(n) = aPrn(n) & sCon & _
Mid(sBuf, InStr(sBuf, ",") + 1, lRet - InStr(sBuf, ","))
Next
'Return the result
PrinterFind = aPrn

End Function

--
Regards,
Tom Ogilvy

"PO" <po wrote in message ...
Hi

Excel 2000

I use the following wsh script to set the default printer:


Dim oNet
Set oNet =WScript.CreateObject("WScript.Network")

oNet.SetDefaultPrinter("hp LaserJet 1300 PCL 5e")

Set oNet=nothing


(The port name for that printer is DOT4_002)
This works fine and the controlpanel is showing that "hp LaserJet..."

is
the
default printer. Excel however indicates that the

Application.DefaultPrinter
is "unknown". When I set the default printer manually in the

controlpanel
Excel recognizes the default printer to be "hp LaserJet 1300 PCL 5e on
Ne02:". I don't understand where Excel gets "on Ne02:" from. The

portname
should be DOT4_002.

On some computers Excel still prints correctly to the "hp

LaserJet...",
however on others Excel reports an error.

Any ideas?

TIA
po








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
setting the default printer for this workbook only mikecoxffbb Excel Worksheet Functions 1 October 25th 08 01:47 AM
members on my network printer not able to print to default printer smeheut Excel Discussion (Misc queries) 0 June 18th 07 06:42 PM
Default printer setting John Excel Discussion (Misc queries) 1 March 22nd 07 03:20 PM
Printer Multiple Worksheets with a particular Printer Setting PP[_2_] Excel Worksheet Functions 0 March 14th 07 02:02 PM
Setting Printer Nathan[_4_] Excel Programming 4 June 23rd 04 05:36 PM


All times are GMT +1. The time now is 02:42 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"