?
Solved

Setting default printer in Access XP

Posted on 2003-03-31
6
Medium Priority
?
1,260 Views
Last Modified: 2012-08-14
Hi,

I have an Access 2000 Application which switches the default printer for gerating PDF-Dokuments. That works great. But if I run the database on Access XP, the function for setting the default printer does not work anymore: The code is:

Option Compare Database
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


Public Sub WinNTSetDefaultPrinter(Druckername As String)
   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
   If Len(Nz(Druckername)) > 0 Then
   
       'Druckerinformationen des aktuellen Standarddruckers
       'aus der WIN.INI bestimmen
       Buffer = Space(1024)
   '    PrinterName = List1.Text
       PrinterName = Druckername
       R = GetProfileString("PrinterPorts", PrinterName, "", _
       Buffer, Len(Buffer))
       
       'Treibernamen und Anschlu_ aus Puffer parsen
       GetDriverAndPort Buffer, DriverName, PrinterPort
       
       If DriverName <> "" And PrinterPort <> "" Then
           SetDefaultPrinter Druckername, DriverName, PrinterPort
       End If
   End If
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 = ""
       
   iDriver = InStr(Buffer, ",")
   If iDriver > 0 Then
   
       DriverName = Left(Buffer, iDriver - 1)
       
       iPort = InStr(iDriver + 1, Buffer, ",")
       
       If iPort > 0 Then
           PrinterPort = Mid(Buffer, iDriver + 1, _
           iPort - iDriver - 1)
       End If
   End If
End Sub
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
   R = WriteProfileString("windows", "Device", DeviceLine)
   L = SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, "windows")
End Sub






The code who runs the functions:

.
.
.
               If CreateRegEntry(REG_SZ, "PDFFileName", "C:\" & Rpt.Caption & ".pdf") = False Then
                   MsgBox "Error  !"
                   GoTo EMailReportExit
               End If

               TmpStandardDrucker = GetProfile("win.ini", "Windows", "Device")
               Call WinNTSetDefaultPrinter("Acrobat PDFWriter")  
               
               While Not GetProfile("win.ini", "Windows", "Device") Like "Acrobat PDFWriter*"
                   DoEvents
               Wend
               
         '*** Print REPORT

               
               Call WinNTSetDefaultPrinter(Left$(TmpStandardDrucker, InStr(1, TmpStandardDrucker, ",") - 1))
.
.
.


Does anyone know, what the problem with Access XP is?  

(Note: The App has to run unter Access2000 AND unter AccessXP, so I cannot use the printers object)

Thanx

Andy
0
Comment
Question by:cas1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 58
ID: 8240104
<<(Note: The App has to run unter Access2000 AND unter AccessXP, so I cannot use the printers object)>>

 Well that answered one of my questions<g>

So what exactly happens?  Error message or just doesn't work?

Jim.
0
 
LVL 4

Expert Comment

by:dotthei
ID: 8240388
0
 
LVL 2

Accepted Solution

by:
ACSPanama earned 800 total points
ID: 8241169
I use this Code to set the defualt printer for Windows & works Great in XP.

' You can paste this code into a module..and away you go
'
'************************
' How to use
' To get the default printer
'        debug.print   GetDefaultPrinter
' To set the default printer
'        debug.print SetDefaultPrinter("HP Laser JET")
'  above returns true if success.
' To get a list of printers suitable for a listbox, or combo
'        debug.print GetPrinters
'
' that is all there folks!
'
' Thus, when printing a report, you can:
'
'       1) save the default printer into a string
'              strCurrentPtr = GetDefaultPrinter
'       2) switch to your report printer
'              SetDefaultPrinter strReportsPtr
'       3) print report
'       4) switch back to the default printer
'              SetDefaultPrinter strCurrentPtr
'

Private Const HWND_BROADCAST As Long = &HFFFF&
Private Const WM_WININICHANGE As Long = &H1A

' The following code allows one to read, and write to the WIN.INI files
' In win 2000 the printer settings are actually in the registry. However, windows
' handles this correctly
'
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

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

Private Declare Function SendMessage Lib "User32" _
   Alias "SendMessageA" _
  (ByVal Hwnd As Long, _
   ByVal wMsg As Long, _
   ByVal wParam As Long, _
   lParam As Any) As Long
   
         

Private Function fstrDField(mytext As String, delim As String, groupnum As Integer) As String

   ' this is a standard delimiter routine that every developer I know has.
   ' This routine has a million uses. This routine is great for splitting up
   ' data fields, or sending multiple parms to a openargs of a form
   '
   '  Parms are
   '        mytext   - a delimited string
   '        delim    - our delimiter (usually a , or / or a space)
   '        groupnum - which of the delimited values to return
   '
   
Dim startpos As Integer, endpos As Integer
Dim groupptr As Integer, chptr As Integer

chptr = 1
startpos = 0
 For groupptr = 1 To groupnum - 1
    chptr = InStr(chptr, mytext, delim)
    If chptr = 0 Then
       fstrDField = ""
       Exit Function
    Else
       chptr = chptr + 1
    End If
 Next groupptr
startpos = chptr
endpos = InStr(startpos + 1, mytext, delim)
If endpos = 0 Then
   endpos = Len(mytext) + 1
End If

fstrDField = Mid$(mytext, startpos, endpos - startpos)

End Function

Function SetDefaultPrinter(strPrinterName As String) As Boolean

   Dim strDeviceLine As String
   Dim strBuffer     As String
   Dim lngbuf        As Long
   
  ' get the full device string
  '
   strBuffer = Space(1024)
   lngbuf = GetProfileString("PrinterPorts", strPrinterName, "", strBuffer, Len(strBuffer))
 
  'Write out this new printer information in
  ' WIN.INI file for DEVICE item
  If lngbuf > 0 Then
     
     strDeviceLine = strPrinterName & "," & _
                     fstrDField(strBuffer, Chr(0), 1) & "," & _
                     fstrDField(strBuffer, Chr(0), 2)
                     
     Call WriteProfileString("windows", "Device", strDeviceLine)
     SetDefaultPrinter = True
     
     ' Below is optional, and should be done. It updates the existing windows
     ' so the "default" printer icon changes. If you don't do the below..then
     ' you will often see more than one printer as the default! The reason *not*
     ' to do the SendMessage is that many open applications will now sense the change
     ' in printer. I vote to leave it in..but your case you might not want this.
     '
     
     Call SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, ByVal "windows")
   
  Else
     SetDefaultPrinter = False
  End If
       
End Function

Function GetDefaultPrinter() As String

   Dim strDefault    As String
   Dim lngbuf        As Long

   strDefault = String(255, Chr(0))
   lngbuf = GetProfileString("Windows", "Device", "", strDefault, Len(strDefault))
   If lngbuf > 0 Then
      GetDefaultPrinter = fstrDField(strDefault, ",", 1)
   Else
      GetDefaultPrinter = ""
   End If

End Function


Function GetPrinters() As String
   
   ' this routine returns a list of printers, separated by
   ' a ";", and thus the results are suitable for stuffing into a combo box
   
   Dim strBuffer  As String
   Dim strOnePtr  As String
   Dim intPos     As Integer
   Dim lngChars   As Long
   
   strBuffer = Space(2048)
   lngChars = GetProfileString("PrinterPorts", vbNullString, "", strBuffer, Len(strBuffer))
   
   If lngChars > 0 Then
      intPos = InStr(strBuffer, Chr(0))
     Do While intPos > 1
        strOnePtr = left(strBuffer, intPos - 1)
        strBuffer = Mid(strBuffer, intPos + 1)
        If GetPrinters <> "" Then GetPrinters = GetPrinters & ";"
        GetPrinters = GetPrinters & strOnePtr
        intPos = InStr(strBuffer, Chr(0))
     Loop
   Else
      GetPrinters = ""
   End If
   
 End Function

Public Function testPrintersGet()

   Debug.Print GetDefaultPrinter
   Debug.Print GetPrinters
   
   
End Function

0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 2

Expert Comment

by:ACSPanama
ID: 8241207
If youe want you can also make a ComboBox on a form & change the default printer from the combobox as well.
1) Make the ComboBox on your form.
2) Change the "Row Source Type" property for the Combobox to "Value List"
3) On the OnLoad event for your form Put this code:
   Me.ComboBox.RowSource = GetPrinters
   Me.ComboBox = GetDefaultPrinter

4) On the AfterUpdate Event for your ComboBox put this Code:
   DoCmd.Hourglass True
   SetDefaultPrinter (Me.ComboBox)
   DoCmd.Hourglass False
0
 
LVL 4

Author Comment

by:cas1
ID: 8243638
@JDettman: If I look in the printers setup, the default printer IS set correctly, but the access form nevertheless prints to the previous printer. Seems like it is too quick, such like it prints before the printer is switched, but therefore I inserted the lines

While Not GetProfile("win.ini", "Windows", "Device") Like "Acrobat PDFWriter*"
   DoEvents
Wend
             
but that doesn't help.
0
 
LVL 4

Author Comment

by:cas1
ID: 8243783
Great! I do not know exactly what was wrong with MY code (looks very similar), but anyway: This code works under both versions.

Thanx and bye

Andy
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Suggested Courses

770 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