Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    Visual Basic




Add Default Printer


I need to know how to add printers (including the default) to a Windows 2000 workstation using Visual Basic code. I want to automate the process instead of manually adding. Does anyone have any code that I could use? Thanks!




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Vb/crystal:how To Replace Crviewer Default Printer With Windows Common Dialog Printer
hi all,
I'm trying to create a crystal report through vb.
does anyone know how to replace the default printer ( that comes with crviewer control) with a windows common dialog printer. I mean, with the options to select differnt printers etc.

Set Default Paper Size For Default Printer ...
So, hope that someone could help me..hix..
If i cant do this i will loose my job, my boss say so. I have to print a webpage with the size of paper is A5 baut the default always is Letter, hix, I could change it by hand but the users may not. So i want to set the default paper size of default printer to A5.
Thanks a lot.

How Can I Change The Default Printer At Runtime To Use It With Printer.print Command?
Hi everyone,
I have a little application which uses printer.print command to do some print jobs, of course there are some other things that I print using Data Reports.
When my application starts, I think it instantiates printer object with the default printer (lx300). Once the application is started, there are some options that use printer.print, but there are options that use printer.print too but I need to print them in another printer (ip1000). I have a common dialog that lets me choose the printer, and when I choose ip1000 every printer.print call goes to lx300. I assume there's no way to "reinstantiate" printer object to print in ip1000.
Also, when I close my application ip1000 becomes my default printer. Does the common dialog can select a printer without changing the Windows default printer like Word, Excel. do?

Thanks a lot for your help

VB6 - Print A Text File To A Remote Printer And My Default Printer
Please how can I get a text file located in my hard drive (C:) to print to my default printer and also print to a Remote Printer?

The remote printer is named "U288".

Any workable code snipet will be appreciated.

Thanks y'all.

Giftx.

How Can I Change The Default Printer At Runtime To Use It With Printer.print Command?
Hi everyone,
I have a little application which uses printer.print command to do some print jobs, of course there are some other things that I print using Data Reports.
When my application starts, I think it instantiates printer object with the default printer (lx300). Once the application is started, there are some options that use printer.print, but there are options that use printer.print too but I need to print them in another printer (ip1000). I have a common dialog that lets me choose the printer, and when I choose ip1000 every printer.print call goes to lx300. I assume there's no way to "reinstantiate" printer object to print in ip1000.
Also, when I close my application ip1000 becomes my default printer. Does the common dialog can select a printer without changing the Windows default printer like Word, Excel. do?

Thanks a lot for your help

Changed Default Printer But Job Goes To Old Default...
I have a print command with the following code:

Code:
Private Sub cmdPrint_Click()
Dim tmpPrinter As Printer
Dim DefaultPrinter As Printer

Set DefaultPrinter = Printer ' Save the default printer

For Each tmpPrinter In Printers
If tmpPrinter.DeviceName = cmbPrinters.Text Then
Set Printer = tmpPrinter
Exit For
End If
Next tmpPrinter

CrystalReport1.ReportFileName = FileIn.Path & "" & FileIn.FileName
CrystalReport1.Destination = crptToPrinter
CrystalReport1.PrintReport

MsgBox "Your report was printed"

Set Printer = DefaultPrinter ' Change back to default printer
End Sub
where the combo box is populated by this code:

Code:
i = 0
For Each InstalledPrinters In Printers
cmbPrinters.AddItem InstalledPrinters.DeviceName
i = i + 1
Next InstalledPrinters
cmbPrinters.ListIndex = 0
I select a printer (not the default printer, but a different one) from the combo list, then click on Print and the job is still going to the original default printer. Stepping through the code, I can see how Printer changes (to the printer I selected) during the execution, but the job keeps going to the old default printer. Also the last line of code in the cmdPrint_Click sub gives me a "Run-time error '13': Type mismatch" error.
What am I missing? Can anyone help please?

Thanks,
poli

How To Printer.Print To Non Default Printer

I have a small app that I need to modify.  Basically it sends a report straight to the (default) printer by Printer.Print

Users want to print to their non-default printer, too.  I could change the default printer by Set Printer = another Printer and then get back to the original one, but is there a better way to do it?
Have fun.

---- Andy

Default Printer
I have a network computer with a printer attached to lpt1 that I use for printing. The problem is, since the computer uses a dynamic ip I have to reconnect to the printer every time the computer restarts, it uses one of three ip addresses ( since there are three computers on the network). I have tried connecting to the printer by the computer's name, but this doesn't work. Can a small app be written to check and see if there is a printer attached to a network computer and if it's there set that printer as the default? I think I need a winsock control to "ping" the computer and if there's a response to set the printer to the default. Can this work, and if so, how can it be done/where can i learn how?

Thanks,
redrum

Set A Default Printer
Is it possible to programmatically change a default printer? How?

this is for VB6

Default Printer
I have two printers that have been setup in my computer. One is local and the other is in the network. My program needs to print in those two printers. How can I change the default printer of my computer? Im using vb6 in Windows 98 OS.

Help! How To Set Default Printer Using VB?
I have 5 printers connected to my machine, and I set Printer(2) as default in system's Control Panel, now I want to print the data report to different printer, so I try to change printer using following VB code:

private sub command1_click()
Set rsPrinter = New Recordset
strSql = "select * from PPRINTERSITE where SITEID = '81';"
rsPrinter.Open strSql, g_dbConn, adOpenKeyset, adLockOptimistic
If Not rsPrinter.EOF Then
For Each prt In Printers
If prt.DeviceName = rsPrinter("PRINTERNAME") Then
MsgBox "Printer is " & prt.DeviceName
Set Printer = prt
Exit For
End If
Next
End If
dataReport1.PrintReport
end sub

though the message from MsgBox told me the Printer already changed to other printer ( such as Printer(0) ), but the dataReport1 still be printed in Printer(2), Can someone help me?

Thanks

Yinghong

Default Printer
Hi all, is there a way to use vb code to set the default printer to print a crystal report w/i vb? Currently there is no way to change the default printer from the crystal report (crviewer)....please help..thanks!

Get Default Printer
Hi,
I want to find an API functions that tells me the name of default printer installed on my computer. 'Printer' object from VB is not enought for what I need.
Thanks
Lucian

Default Printer
i have a small program that basically copies information from a flex grid into excel. however, i've found that the page setup properties are not active if there aren't any printers installed.

my question is what's the best way to check for installed printers? i tried an if statement: if printer.devicename = "" then don't do the page setup properties, but i still get the error 'Problem getting printer information from the system. Make sure the printer is set up correctly.'

any thoughts?

thanks,
jb

Which Is The Default Printer
I'm using the code below to change printer queues from one to the other. I need to know if the printer is the "Default Printer" before I change it. Here is the code I'm using: Thanks for the Help.

'******************************************************
'* File: ChangePrinter
'* Created: May 2003
'* Version: 1.1c
'*
'* Main Function: Changes Printer Paths
'* Usage: ChangePrinter.VBS \ServerNameOldPrinter \ServerNameNewPrinter
'*'****************************************************

'Get the command line arguments
if (Wscript.arguments.count > 2) or (Wscript.arguments.count < 2) then
wscript.echo "You put the wrong number of arguments you need to put 2. Put the old printer path first, followed by the new printer path. ex. changeprinter \hqptrz010500 \hqptrz010501"
WScript.Quit
end if

OldPrinterPath = Wscript.arguments.item(0)
NewPrinterPath = Wscript.arguments.item(1)

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oPrinters = WshNetwork.EnumPrinterConnections

For i = 0 to oPrinters.Count - 1 Step 2
If lcase(OldPrinterPath) = lcase(oPrinters.Item(i+1)) then
' Add the New Printer
WshNetwork.AddWindowsPrinterConnection NewPrinterPath

' Make the New Printer the Default Printer.
WshNetwork.SetDefaultPrinter NewPrinterPath

' Remove the Old Printer
WshNetwork.RemovePrinterConnection OldPrinterPath,true,true
End if
Next

Set WshNetwork = Nothing
Set oPrinters = Nothing
WScript.Quit

Set Default Printer OCX
Can't seem to put it into the code library but gonna put it here instead.

An ActiveX Combo To Find All Installed Printers and Specify The Default Printer.

Enjoy

Default Printer
Hi, I wish to get the device name of the default printer but VB's API does not do it. I did a search on the forum and found one thread, that thread has a link to another site about retrieving the default printer's device name but the site is no longer avaiable. Is there anyone that can help?

Default Printer
can anybody help me about how to use the windows default printer to print something? if there is a tutorial i think that will be best for me.

Default Printer
Well, seeing as this forum now includes "reporting" is suppose that this thread belongs here. If not, please feel free to move it.

How do I set the current printer to the default printer, having changed it to a not default printer?

P.S. I've searched a few returned entries in Google and none of them have the answer that I'm after.

Can't Set Default Printer
This is crazy. There are 2,000 topics on the default printer, but I still can't solve my problem. My program prints fine but I can't change the default printer.

I can't execute a simple command. Is there some component or reference I need in my program.

This gives me an "Object Required" error
TempString = "Canon Bubble-Jet BJC-265SP"
Set Printer = TempString

This is the code I want to use.


VB Code:
Dim strCurrentPrinter As String 'get current printer name and store itstrCurrentPrinter = Printer.DeviceName 'To set your printer as default printerSet Printer = "Your Printer Name" 'Do your codings to print Set Printer = strCurrentPrinter 'to set back the earlier printer as default  

Default Printer
How to get the default printer in my machine and also check the status and print a pdf file by vb?

Set Default Printer
I have searched for some help on this, but it is very vague and/or complex.

I need to retrieve the name of the default printer, change it to Win2PDF, do some printing and then change the printer back to the old default.

The system default printer is found at hkey_current_userssoftwaremicrosoftwindows ntcurrentversionwindows.

How can I read that name, change to Win2PDF and then change back again?

PS.- I am currently using a program called sdp.exe (Set Default Printer) and it is excellent, but if the user changes their printer, my program won't work properly.

VB Default Printer
It is my understanding that the VB printer is the default system printer and the object Printer will print to the system default printer. I use the following code to change the orientation to landscape

CommonDialog1.Flags = &H40
CommonDialog1.PrinterDefault = True
CommonDialog1.ShowPrinter

On completion of the CommonDialog1.ShowPrinter the orientation in the Printer object is still protrait.

What am I missing here??????

Default Printer
what is the simplest way to set a default printer with code?

VB Default Printer
I need help restoring the default printer to an original value. I have an application that uses PrinterDefault = True so when I change the printer, it marks it as default but I need to change the default back to it's original value.
Thanks...

Default Printer
Does anyone know how to check if a user has a default printer?

Default Printer
How would I retrieve the default printer on the system without using the Printer object. I'm trying to do this in Access??

Is there an API etc???

Thanks

Default Printer
How Would I retrieve the default printer in windows XP programmatically. In the previous version of Windows it was stored in win.ini.

Thanks,

Default Printer
Hi Guy's,

how do i change my default printer temporary to print a document and change back when I exit my visual basic application
thank you

Set Default Printer
Hi Everyone,

I have a piece of code that sets a specific printer to the default printer position based upon what printer the user selected from a combo box. The code works fine except for one problem. Once the new printer has been set as the default printer the code sends a broadcast message to all other programs to reload the INI file. When this happens sometimes the computer hangs or pauses until the user selects the Windows START button or some other command to get the broadcast to stop. What is causing this problem? Here is the line of code that program gets hung up on. I would appreciate any help. Thanks.


Code:

' Cause all applications to reload the INI file:
l = SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, "windows")

Default Printer??
how can i work out what printer is the default??

Getting The Default Printer
Alright, this should be easy... How do I retrieve the current default printer?

How Can You Set The Default Printer ?
Suppose that I have 2 printers installed on my PC (Printer1 and Printer2).

Printer1 is set as the default printer on the system.

Now, from VB I call the CMDialog.ShowPrinter to print a document.
If I select PRINTER2, it correctly print the document but if I have a look at the printers, I can see that it has changed the default printer to PRINTER2.

What I would like to do then, is to set the default to the PRINTER1 after printing a document. For this, I initialized a variable (prtDef) at the beginning of my program which contains the default printer name. Then, after printing a document, I try the following code :

If Printer.DeviceName <> prtDef Then
For Each prt In Printers
If prt.DeviceName = prtDef Or prt.Port = prtDef Then
Set Printer = prt ' sélectionne l'imprimante
End If
Next prt
End If

When this code is performed, the default printer is still set to PRINTER2 instead of PRINTER1 !...

Normally, PRINTER1 shoould be set to the default printer by the 'Set Printer = prt'.

Thanks for your help...
Thierry

You Want To Know How: Set Default Printer
Hi everybody,

You want to know how to set a printer to the default system printer.


Look here you have the code : http://support.microsoft.com/support.../Q266/7/67.ASP


OR

check my DLL with this message


How to use the DLL

Open a new exe project
make reference to the DLL
place a ListBox and 2 CommandButton

add this code



PHP Code:




Dim x As BMDefaultPrinter

Private Sub Command1_Click()
  
  If List1.ListIndex <> -1 Then
    x.ChangeDefaultPrinter List1.List(List1.ListIndex)
  End If
  
End Sub

Private Sub Command2_Click()

  x.ResetDefaultPrinter

End Sub

Private Sub Form_Load()
  Dim p As Printer
  
  Set x = New BMDefaultPrinter

  
  For Each p In Printers
    List1.AddItem p.DeviceName
  Next
End Sub

Private Sub Form_Unload(Cancel As Integer)
  Set x = Nothing

End Sub









MaBen

Printer Default
Is important for me.

I can know the name the printer default about the API function?
Thanks

How Can You Set A Different Printer Than Default One ?
What I would like to do is to change the default printer by another one before calling the ShowPrinter.
Here is my code, but when I call the printer dialog box, the printer specified is the default printer but not the one I selected.

Printer_Name is a global which contains the printer I want to select (\SERVERPRINTERNAME)

Dim prt As Printer
For Each prt In Printers
If LCase(prt.DeviceName) = LCase(Printer_Name) Then
Set Printer = prt
GoTo PrintDialog
End If
Next prt
MsgBox "Printer not define: " & Printer_Name, vbCritical
Exit Sub
PrintDialog:
CMDialog.ShowPrinter


Thanks for your help !...
Thierry

tdemoy@ares.fr

Default Printer
Hi,
How would I go about changing the Windows default printer through code? I know how to change the devicename for the printer object, but that isn't what I need. I need it to apply for all other Windows apps.

Thanks B.

Default Printer
Is there a way to change the default printer with code ?? If so what is it. I know the user can change the default printer with the common dialog box but I'm having a problem when I try to do it with code. Any help is appreciated

[This message has been edited by raicheman (edited 01-03-2000).]

Which One Is Default Printer?
Hi!

Does anyone know how I can check which printer is set as default? Is there an API that does it?

I also need to find a way of closing a printer. Again, is there an API that does that?

Thank you! Thank you!

Olga

Which One Is Default Printer?
Hi!

Does anyone know how I can check which printer is set as default? Is there an API that does it?

I also need to find a way of closing a printer. Again, is there an API that does that?

Thank you! Thank you!

Olga

How To Set Up A Default Printer
I programmed a VB code that print a rapport. I need a VB code that changes default printer. Thank you.

How To Set Default Printer In Win Ini In Vb6
Hi all

There was a guy back in june looking to set the default printer in win.ini. I got the code from microsoft so it works good. I had a similiar problem. I am using a Zebra TLP2844. When I wanted to view the data report which was A4 I was getting errors because the default printer was set to the zebra. So I needed to change the default to an A4 printer and then change it back.

NOTE: This code changes the default printer for the entire system. So all applications that do not specify a printer, even applications that are currently running, use this same default. For this reason, you may want to have your code remember the previous default and then set it back when your print job is finished.

The following code sample provides a means to determine which printers are available, and to designate one as the system default printer.
Step-by-Step Example

1. Start a new Standard EXE project in Visual Basic. Form1 is created by default.
2. Add a new module to the project and insert the following code:

Public Const HWND_BROADCAST = &HFFFF
Public Const WM_WININICHANGE = &H1A

' constants for DEVMODE structure
Public Const CCHDEVICENAME = 32
Public Const CCHFORMNAME = 32

' constants for DesiredAccess member of PRINTER_DEFAULTS
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const PRINTER_ACCESS_ADMINISTER = &H4
Public Const PRINTER_ACCESS_USE = &H8
Public Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _
PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)

' constant that goes into PRINTER_INFO_5 Attributes member
' to set it as default
Public Const PRINTER_ATTRIBUTE_DEFAULT = 4

' Constant for OSVERSIONINFO.dwPlatformId
Public Const VER_PLATFORM_WIN32_WINDOWS = 1

Public Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type

Public Type DEVMODE
dmDeviceName As String * CCHDEVICENAME
dmSpecVersion As Integer
dmDriverVersion As Integer
dmSize As Integer
dmDriverExtra As Integer
dmFields As Long
dmOrientation As Integer
dmPaperSize As Integer
dmPaperLength As Integer
dmPaperWidth As Integer
dmScale As Integer
dmCopies As Integer
dmDefaultSource As Integer
dmPrintQuality As Integer
dmColor As Integer
dmDuplex As Integer
dmYResolution As Integer
dmTTOption As Integer
dmCollate As Integer
dmFormName As String * CCHFORMNAME
dmLogPixels As Integer
dmBitsPerPel As Long
dmPelsWidth As Long
dmPelsHeight As Long
dmDisplayFlags As Long
dmDisplayFrequency As Long
dmICMMethod As Long ' // Windows 95 only
dmICMIntent As Long ' // Windows 95 only
dmMediaType As Long ' // Windows 95 only
dmDitherType As Long ' // Windows 95 only
dmReserved1 As Long ' // Windows 95 only
dmReserved2 As Long ' // Windows 95 only
End Type

Public Type PRINTER_INFO_5
pPrinterName As String
pPortName As String
Attributes As Long
DeviceNotSelectedTimeout As Long
TransmissionRetryTimeout As Long
End Type

Public Type PRINTER_DEFAULTS
pDatatype As Long
pDevMode As Long
DesiredAccess As Long
End Type

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

Declare Function WriteProfileString Lib "kernel32" _
Alias "WriteProfileStringA" _
(ByVal lpszSection As String, _
ByVal lpszKeyName As String, _
ByVal lpszString As String) As Long

Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lparam As String) As Long

Declare Function GetVersionExA Lib "kernel32" _
(lpVersionInformation As OSVERSIONINFO) As Integer

Public Declare Function OpenPrinter Lib "winspool.drv" _
Alias "OpenPrinterA" _
(ByVal pPrinterName As String, _
phPrinter As Long, _
pDefault As PRINTER_DEFAULTS) As Long

Public Declare Function SetPrinter Lib "winspool.drv" _
Alias "SetPrinterA" _
(ByVal hPrinter As Long, _
ByVal Level As Long, _
pPrinter As Any, _
ByVal Command As Long) As Long

Public Declare Function GetPrinter Lib "winspool.drv" _
Alias "GetPrinterA" _
(ByVal hPrinter As Long, _
ByVal Level As Long, _
pPrinter As Any, _
ByVal cbBuf As Long, _
pcbNeeded As Long) As Long

Public Declare Function lstrcpy Lib "kernel32" _
Alias "lstrcpyA" _
(ByVal lpString1 As String, _
ByVal lpString2 As Any) As Long

Public Declare Function ClosePrinter Lib "winspool.drv" _
(ByVal hPrinter As Long) As Long

Public Sub SelectPrinter(NewPrinter As String)
Dim Prt As Printer

For Each Prt In Printers
If Prt.DeviceName = NewPrinter Then
Set Printer = Prt
Exit For
End If
Next
End Sub


3. Place a ListBox and a CommandButton on Form1.
4. Add the following code to the General Declarations section of Form1:

Option Explicit

Private Function PtrCtoVbString(Add As Long) As String
Dim sTemp As String * 512, x As Long

x = lstrcpy(sTemp, Add)
If (InStr(1, sTemp, Chr(0)) = 0) Then
PtrCtoVbString = ""
Else
PtrCtoVbString = Left(sTemp, InStr(1, sTemp, Chr(0)) - 1)
End If
End Function

Private Sub SetDefaultPrinter(ByVal PrinterName As String, _
ByVal DriverName As String, ByVal PrinterPort As String)
Dim DeviceLine As String
Dim r As Long
Dim l As Long
DeviceLine = PrinterName & "," & DriverName & "," & PrinterPort
' Store the new printer information in the [WINDOWS] section of
' the WIN.INI file for the DEVICE= item
r = WriteProfileString("windows", "Device", DeviceLine)
' Cause all applications to reload the INI file:
l = SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, "windows")
End Sub

Private Sub Win95SetDefaultPrinter()
Dim Handle As Long 'handle to printer
Dim PrinterName As String
Dim pd As PRINTER_DEFAULTS
Dim x As Long
Dim need As Long ' bytes needed
Dim pi5 As PRINTER_INFO_5 ' your PRINTER_INFO structure
Dim LastError As Long

' determine which printer was selected
PrinterName = List1.List(List1.ListIndex)
' none - exit
If PrinterName = "" Then
Exit Sub
End If

' set the PRINTER_DEFAULTS members
pd.pDatatype = 0&
pd.DesiredAccess = PRINTER_ALL_ACCESS Or pd.DesiredAccess

' Get a handle to the printer
x = OpenPrinter(PrinterName, Handle, pd)
' failed the open
If x = False Then
'error handler code goes here
Exit Sub
End If

' Make an initial call to GetPrinter, requesting Level 5
' (PRINTER_INFO_5) information, to determine how many bytes
' you need
x = GetPrinter(Handle, 5, ByVal 0&, 0, need)
' don't want to check Err.LastDllError here - it's supposed
' to fail
' with a 122 - ERROR_INSUFFICIENT_BUFFER
' redim t as large as you need
ReDim t((need 4)) As Long

' and call GetPrinter for keepers this time
x = GetPrinter(Handle, 5, t(0), need, need)
' failed the GetPrinter
If x = False Then
'error handler code goes here
Exit Sub
End If

' set the members of the pi5 structure for use with SetPrinter.
' PtrCtoVbString copies the memory pointed at by the two string
' pointers contained in the t() array into a Visual Basic string.
' The other three elements are just DWORDS (long integers) and
' don't require any conversion
pi5.pPrinterName = PtrCtoVbString(t(0))
pi5.pPortName = PtrCtoVbString(t(1))
pi5.Attributes = t(2)
pi5.DeviceNotSelectedTimeout = t(3)
pi5.TransmissionRetryTimeout = t(4)

' this is the critical flag that makes it the default printer
pi5.Attributes = PRINTER_ATTRIBUTE_DEFAULT

' call SetPrinter to set it
X = SetPrinter(Handle, 5, pi5, 0)

If X = False Then ' SetPrinter failed
MsgBox "SetPrinter Failed. Error code: " & Err.LastDllError
Exit Sub
Else
If Printer.DeviceName <> List1.Text Then
' Make sure Printer object is set to the new printer
SelectPrinter (List1.Text)
End If
End If

' and close the handle
ClosePrinter (Handle)
End Sub

Private Sub GetDriverAndPort(ByVal Buffer As String, DriverName As _
String, PrinterPort As String)

Dim iDriver As Integer
Dim iPort As Integer
DriverName = ""
PrinterPort = ""

' The driver name is first in the string terminated by a comma
iDriver = InStr(Buffer, ",")
If iDriver > 0 Then

' Strip out the driver name
DriverName = Left(Buffer, iDriver - 1)

' The port name is the second entry after the driver name
' separated by commas.
iPort = InStr(iDriver + 1, Buffer, ",")

If iPort > 0 Then
' Strip out the port name
PrinterPort = Mid(Buffer, iDriver + 1, _
iPort - iDriver - 1)
End If
End If
End Sub

Private Sub ParseList(lstCtl As Control, ByVal Buffer As String)
Dim i As Integer
Dim s As String

Do
i = InStr(Buffer, Chr(0))
If i > 0 Then
s = Left(Buffer, i - 1)
If Len(Trim(s)) Then lstCtl.AddItem s
Buffer = Mid(Buffer, i + 1)
Else
If Len(Trim(Buffer)) Then lstCtl.AddItem Buffer
Buffer = ""
End If
Loop While i > 0
End Sub

Private Sub WinNTSetDefaultPrinter()
Dim Buffer As String
Dim DeviceName As String
Dim DriverName As String
Dim PrinterPort As String
Dim PrinterName As String
Dim r As Long
If List1.ListIndex > -1 Then
' Get the printer information for the currently selected
' printer in the list. The information is taken from the
' WIN.INI file.
Buffer = Space(1024)
PrinterName = List1.Text
r = GetProfileString("PrinterPorts", PrinterName, "", _
Buffer, Len(Buffer))

' Parse the driver name and port name out of the buffer
GetDriverAndPort Buffer, DriverName, PrinterPort

If DriverName <> "" And PrinterPort <> "" Then
SetDefaultPrinter List1.Text, DriverName, PrinterPort
If Printer.DeviceName <> List1.Text Then
' Make sure Printer object is set to the new printer
SelectPrinter (List1.Text)
End If
End If
End Sub

Private Sub Command1_Click()
Dim osinfo As OSVERSIONINFO
Dim retvalue As Integer

osinfo.dwOSVersionInfoSize = 148
osinfo.szCSDVersion = Space$(128)
retvalue = GetVersionExA(osinfo)

If osinfo.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then
Call Win95SetDefaultPrinter
Else
' This assumes that future versions of Windows use the NT method
Call WinNTSetDefaultPrinter
End If
End Sub

Private Sub Form_Load()
Dim r As Long
Dim Buffer As String

' Get the list of available printers from WIN.INI
Buffer = Space(8192)
r = GetProfileString("PrinterPorts", vbNullString, "", _
Buffer, Len(Buffer))

' Display the list of printer in the ListBox List1
ParseList List1, Buffer
End Sub


5. Run the project and note that the preceding code populates a ListBox with all available printers.
6. Select one printer in the list and make it the default by clicking the CommandButton.
7. Confirm that the default printer has changed: On the Start button, select Settings, select Printers, and then check which printer is marked as the default. Alternatively, you can print to the default printer from any application on your system.

Default Printer.
Was hopeing someone could point out a good primer on printing. Am trying to
change the default printer, have the device names in a list box, but can't
for the life of me figure out how to select one from the list box and set
Printer = . Any help would be appreciated.

Thank you,
Josh Madarasz

Set Default Printer
I've seen a number of questions recently on how to set the system-wide default printer from within VB and I've always seen this done using the SetPrinter API, which is a particularly obscure and even difficult thing to use. So I thought I'd look for an easier way and came across this equally obscure but easy to use library which I've never heard of before but seems to be Windows "standard issue".
Has anyone heard of the Windows Script Host Object Model? Try adding a reference to it from your program! Better still read the details Here

It's clearly all vbscript related but all the ones I've tried seem to work fine from vb6.

It's got some interesting stuff in it!

The Special Folders object is pretty useful

It even has methods to start scripts (and programs), wait for them to finish, check their stdoutput stream etc.

Anyway, in the model is a network object, which happens to have a SetDefaultPrinter method, so I tried it out. It seems to work for me, it definately changes the system wide default printer. What I can't tell is if it works on a none-networked machine (as my pc here is networked). Nor can I tell if it has any undesireable side-effects on a network, such as affecting the default printer for all the networked machines (which may even be desireable!). Anyway, 4 lines of code to change the default printer can't be bad!........

Dim x As WshNetwork
Set x = New WshNetwork
x.SetDefaultPrinter "hp deskjet 3420 series"
Set x = Nothing

If anyone wants to try this on a none-networked system I'd be interested in hearing the results!

I'd also be interested in any other comments.

Ian

How Set Default Printer
By VB how can be set default printer?

Get Default Printer
http://www.codeguru.com/forum/showt...threadid=299609

Default Printer?
Hi,

How can I use another printer than the default printer when I use the printer object (in combination with a commondialog with PrinterDefault set to True) ?

Thanks in advance

Michiel

Set Default Printer
How can I choose the default printer? The code "Set printer = ptr" does not work!
I want to print a excel sheet in a PDF - document.

RU
Panther

Setting A Default Printer
I am developing an application that prints on a preformatted carbonize continuous cheque forms. I need help on how do i get/set my dot matrix printer as my default printer operated by my application even if my default printer is other printer. Any code sample will be appreciated.

Copyright © 2005-08 www.BigResource.com, All rights reserved