cas1
asked on
Setting default printer in Access XP
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(Dru ckername 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("PrinterP orts", 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("window s", "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("Ac robat PDFWriter")
While Not GetProfile("win.ini", "Windows", "Device") Like "Acrobat PDFWriter*"
DoEvents
Wend
'*** Print REPORT
Call WinNTSetDefaultPrinter(Lef t$(TmpStan dardDrucke r, 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
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(Dru
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("PrinterP
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("window
L = SendMessage(HWND_BROADCAST
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("Ac
While Not GetProfile("win.ini", "Windows", "Device") Like "Acrobat PDFWriter*"
DoEvents
Wend
'*** Print REPORT
Call WinNTSetDefaultPrinter(Lef
.
.
.
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
@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.
While Not GetProfile("win.ini", "Windows", "Device") Like "Acrobat PDFWriter*"
DoEvents
Wend
but that doesn't help.
ASKER
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
Thanx and bye
Andy
Well that answered one of my questions<g>
So what exactly happens? Error message or just doesn't work?
Jim.