Solved

set default printer

Posted on 2003-11-06
9
5,386 Views
Last Modified: 2013-12-26
I need to create an app in VB.NET that will allow me to change the Default Printer in the Windows 2000 Control Panel.  It is not sufficient to just select a printer to print to for the duration of the program.

0
Comment
Question by:TomDavie
9 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 9693379
Hi TomDavie,

You need to close some of your open questions. Failure to maintain your questions is a violation of the member agreement and may lead to your suspension or termination of your account. A guideline of 5 open questions is considered acceptable and you currently have 7.

7 open questions:
11/06/03 http://www.experts-exchange.com/Q_20789664.html "set default printer"
08/20/03 http://www.experts-exchange.com/Q_20714704.html "Need to create a linked document or a sh..."
08/13/03 http://www.experts-exchange.com/Q_20708333.html "Extracting email addresses from Microsof..."
07/23/03 http://www.experts-exchange.com/Q_20687946.html "Using VBA (Word 2000).  Want to use a wi..."
06/05/03 http://www.experts-exchange.com/Q_20637872.html "Cannot output to variables"
06/05/03 http://www.experts-exchange.com/Q_20637669.html "Cannot insert a variable into a select s..."
05/14/03 http://www.experts-exchange.com/Q_20615876.html "Error message obtained while rolling out..."


Tim Cottee MCSD, MCDBA, CPIM
Brainbench MVP for Visual Basic
http://www.brainbench.com
0
 

Author Comment

by:TomDavie
ID: 9693517
Hi. Sorry about that.  I've closed six of the questions and posted comments where appropriate.
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 9694207
0
 

Author Comment

by:TomDavie
ID: 9694664
the code needs to be compatible with VB.NET.  I have code for VB5 and VB6 but does anybody know if this will be compatible with VB.NET?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 48

Expert Comment

by:Mikal613
ID: 9694682
Than you should post in .NET Area
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 9694697
0
 
LVL 5

Expert Comment

by:fantasy1001
ID: 9699385
This will show all the available printer
http://www.vbdotnetheaven.com/Code/Jun2003/2093.asp

To set default printer, you need to access the registry

HKEY_CURRENT_USER/Printers/DevModes2 for winxp

~ fantasy ~
0
 
LVL 4

Accepted Solution

by:
Clothahump earned 300 total points
ID: 9717907
I found this code some time ago.  I really don't remember where I got it.  Copy it into a module, then call it like this:

On your form, put a listbox.  In Form Load, put
ProfileLoadWinIniList Me.lstPrinters, "PrinterPorts"

Then set the listbox Click event:
Private Sub lstPrinters_Click()
    SetDefaultPrinterWinNT (lstPrinters.List(lstPrinters.ListIndex))
End Sub



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

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

   Dim posDriver As Long
   Dim posPort As Long
   DriverName = ""
   PrinterPort = ""

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

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

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

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


Public Function ProfileLoadWinIniList(LST As ListBox, lpSectionName As String) As Long

'Load the data from win.ini.

   Dim success As Long
   Dim nSize As Long
   Dim lpKeyName As String
   Dim ret As String
 
  'call the API passing null as the parameter
  'for the lpKeyName parameter. This causes
  'the API to return a list of all keys under
  'that section. Pad the passed string large
  'enough to hold the data. Adjust to suit.
   ret = Space$(8102)
   nSize = Len(ret)
   success = GetProfileString(lpSectionName, _
                              vbNullString, _
                              "", _
                              ret, _
                              nSize)
   
  'The returned string is a null-separated
  'list terminated by a pair of null characters.
 
   If success Then
        iCurrentPrinter = 1
     'trim terminating null and trailing spaces
      ret = Left$(ret, success)
     
        'with the resulting string,
        'extract each element
         Do Until ret = ""
     
           'strip off an item and
           'add the item to the listbox
            lpKeyName = StripNulls(ret)
'            If InStr(UCase$(lpKeyName), "SI") > 0 Then
'                sPrinterArray(iCurrentPrinter) = lpKeyName
'                iCurrentPrinter = iCurrentPrinter + 1
'                If iCurrentPrinter > MAXPRINTERS Then
'                    Exit Do
'                End If
                LST.AddItem lpKeyName
'            End If
     
         Loop
 
   End If
 
  'return the number of items as an
  'indicator of success
   ProfileLoadWinIniList = iCurrentPrinter - 1

End Function


Private Function StripNulls(startstr As String) As String

 'Take a string separated by chr$(0)
 'and split off 1 item, shortening the
 'string so next item is ready for removal.
  Dim pos As Long

  pos = InStr(startstr$, Chr$(0))
 
  If pos Then
     
      StripNulls = Mid$(startstr, 1, pos - 1)
      startstr = Mid$(startstr, pos + 1, Len(startstr))
   
  End If

End Function


Public Sub SetDefaultPrinterWinNT(PrinterName As String)
' modified CRS 08/05 to pass the printer name in

   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
     
      Call 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
         SetDefaultPrinter PrinterName, DriverName, PrinterPort
      End If
       
'    End If
   
End Sub
'--end block--'

0
 

Author Comment

by:TomDavie
ID: 9746767
The accepted soultion closely matched some code I had already found on the Microsoft.com
website.  

All I had to do was adapt the code for VB.NET
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now