Prev: newbie needs examples of creating a recordset and inserting datausing it in Access 2007?
Next: solving a "ActiveX component can't create object or return reference to this object Run Time Error 429"
From: Albert on 31 May 2010 19:22 Hello, We are using Lebans ReportToPDF module and are having conflicts with a printer driver. It seems that if we switch the default printer to any other printer it works fine. My question is: Is there a way to change the Windows Default printer setting in VBA, then set it back after? I tried setting the Application.Printer, etc., but the conflict still exists because this only changes the printer for Access and not for Windows. Thanks for any help! Albert S.
From: Phil on 1 Jun 2010 14:35 On 01/06/2010 00:22:36, "Albert" wrote: > Hello, > > We are using Lebans ReportToPDF module and are having conflicts with a > printer driver. It seems that if we switch the default printer to any > other printer it works fine. > > My question is: Is there a way to change the Windows Default printer > setting in VBA, then set it back after? > > I tried setting the Application.Printer, etc., but the conflict still > exists because this only changes the printer for Access and not for > Windows. > > Thanks for any help! > Albert S. > > This may help. Uses a freebee PDF995 printer, but as far as I remember that can be programmed Option Compare Database 'Use database order for string comparisons Option Explicit ' Code from: ' Microsoft Access 95 How-To ' (c) 1998 Ken Getz and Paul Litwin ' All rights reserved. ' You may only use this code as part of an application ' that requires its use. You must including this ' notice intact. You may not distribute the code ' as your own work, nor can you distribute the ' code on its own. Function ahtGetDefaultPrinter(dr As aht_tagDeviceRec) As Boolean ' Retrieve the default printer information. Though ' the function dutifully returns True if the ' values were available, and False otherwise, Windows ' really isn't happy without a default printer, and ' this situation rarely comes up. ' In: ' dr: a aht_tagDeviceRec structure to fill in ' Out: ' Return Value: True if info available, False otherwise. ' dr: filled in with default printer information, ' if it was available (check the function's return ' value). ' ' Comments: ' Requires the ahtGetToken() function from basGetToken ' Requires the ahtGetINIString() function from basINIFile ' Requires type definitions from basPrintTypes Dim strBuffer As String strBuffer = ahtGetINIString("Windows", "Device") If Len(strBuffer) > 0 Then With dr .drDeviceName = ahtGetToken(strBuffer, ",", 1) .drDriverName = ahtGetToken(strBuffer, ",", 2) .drPort = ahtGetToken(strBuffer, ",", 3) End With ahtGetDefaultPrinter = True Else ahtGetDefaultPrinter = False End If End Function Function ahtSetDefaultPrinter(dr As aht_tagDeviceRec) As Boolean ' Set the default printer device in Win.INI ' In: ' dr: a aht_tagDeviceRec structure to use as ' the source of information. ' Out: ' Return Value: True if set correctly, False otherwise. ' If successful, writes a string in the form: ' device=HP LaserJet 4,HPPCL5E,LPT1: ' to your Win.INI file. ' ' Comments: ' ' Requires the aht_apiWriteProfileString() declaration from basINIFile ' Requires type definitions from basPrintTypes Dim strBuffer As String ' Build up the appropriate string. strBuffer = dr.drDeviceName & "," strBuffer = strBuffer & dr.drDriverName & "," strBuffer = strBuffer & dr.drPort ' Now write that string out to WIN.INI. ahtSetDefaultPrinter = (aht_apiWriteProfileString("Windows", _ "Device", strBuffer) <> 0) End Function Function TestDefaultPrinter() ' Test the ahtDefaultPrinter() function. ' Fill in a DeviceRec structure with ' the pieces of the default printer info, ' and then print them out. Dim dr As aht_tagDeviceRec If ahtGetDefaultPrinter(dr) Then Debug.Print "Device: "; dr.drDeviceName Debug.Print "Driver: "; dr.drDriverName Debug.Print "Port : "; dr.drPort End If End Function And This Option Compare Database Option Explicit 'Read INI settings Declare Function GetPrivateProfileString Lib "kernel32" Alias _ "GetPrivateProfileStringA" (ByVal lpApplicationName As String, _ ByVal lpKeyName As Any, ByVal lpDefault As String, _ ByVal lpReturnedString As String, ByVal nSize As Long, _ ByVal lpFileName As String) As Long 'Write settings Declare Function WritePrivateProfileString Lib "kernel32" Alias _ "WritePrivateProfileStringA" (ByVal lpApplicationName As String, _ ByVal lpKeyName As Any, ByVal lpString As Any, _ ByVal lpFileName As String) As Long Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) ' These functions used with procedures from ' Microsoft Access 95 How-To '(c) 1998 Ken Getz and Paul Litwin ' All rights reserved. ' Other modules from this source are: ' basDefaultPrinter ' basGetPrinters ' basIniFile ' basPrintTypes ' basToken ' You'll also need defaultprt.zip Private drexisting As aht_tagDeviceRec 'Const AcrobatName = "Acrobat PDFWriter" 'Const AcrobatDriver = "PDFWRITR" 'Const AcrobatPort = "LPT1:" Const AcrobatName = "PDF995" Const AcrobatDriver = "PDF995" Const AcrobatPort = "NE01:" Sub ResetDefaultPrinter() Call ahtSetDefaultPrinter(drexisting) End Sub Function ChangeToAcrobat() If ahtGetDefaultPrinter(drexisting) Then Dim dr As aht_tagDeviceRec With dr .drDeviceName = AcrobatName .drDriverName = AcrobatDriver .drPort = AcrobatPort End With Call ahtSetDefaultPrinter(dr) End If End Function Sub ChangePdfFileName(NewFileName As String) Call Call aht_apiWriteProfileString("Acrobat PDFWriter", "PDFFileName", NewFileName) End Sub Function PDFWrite(ReportName As String, PDFPath As String, ToKill As Boolean, _ Optional RptCaption As String, Optional StrCriteria As String) As String '?pdfwrite("RptWaitingList", "C:\Documents and Settings\Phil\My Documents\Access\MDB\WFYC\PDFS", true) ' Runs an Access report to PDF995 to create a pdf file from the report. ' Input parameters are the name of the report within the current database, ' the path for the output file, and an optional criteria for the report ' Be sure to check that the "Generating PDF CS" setting in pdfsync.ini is set to 0 ' when pdf995 is idle. This codes uses that as a completion flag as it seems to be ' the most reliable indication that PDF995 is done writing the pdf file. ' Note: The application.printer object is not valid in Access 2000 ' and earlier. In that case, set the printer in the report to pdf995 ' and comment out the references herein to the application.printer Dim SyncFile As String, MaxWaittime As Long Dim IniFileName As String ', tmpPrinter As Printer Dim OutputFile As String, CaptionFile As String Dim X As Long Dim TmpOutputFile As String, TmpAutoLaunch As String ' set the location of the PDF995.ini and the pdfsync files IniFileName = "c:\pdf995\res\pdf995.ini" SyncFile File = "c:\documents and settings\all users\application data\pdf995\res\pdfsync.ini" ' build the output file name from the path parameter and the report name If Mid(PDFPath, Len(PDFPath), 1) <> "\" Then PDFPath = PDFPath & "\" OutputFile = PDFPath & ReportName & ".pdf" If RptCaption = "" Then RptCaption = ReportName End If CaptionFile File = PDFPath & RptCaption & ".pdf" ' This appears to be the name that is saved ' ' PDF995 operates asynchronously. We need to determine when it is done so we can ' continue. This is done by creating a file and having PDF995 delete it using the ' ProcessPDF parameter in its ini file which runs a command when it is complete. ' save current settings from the PDF995.ini file TmpOutputFile = ReadINIfile("PARAMETERS", "Output File", IniFileName) TmpAutoLaunch = ReadINIfile("PARAMETERS", "Autolaunch", IniFileName) ' remove previous pdf if it exists On Error Resume Next If ToKill = True Then 'if you need a new file If Dir(CaptionFile) <> "" Then ' and can find it Kill CaptionFile ' delete it End If End If On Error GoTo CleanUp ' setup new values in PDF995.ini X X = WritePrivateProfileString("PARAMETERS", "Output File", OutputFile, IniFileName) X = WritePrivateProfileString("PARAMETERS", "AutoLaunch", "0", IniFileName) ' change the default printer to PDF995 ' if running on Access 2000 or earlier, comment out the next two lines 'Set tmpPrinter = Application.Printer 'Application.Printer = Application.Printers("PDF995") Call ChangeToAcrobat ' Set default printer to PDF995 'print the report DoCmd.OpenReport ReportName, acViewNormal, , StrCriteria Call ResetDefaultPrinter ' Reset default printer ' cleanup delay to allow PDF995 to finish up. When flagfile is nolonger present, PDF995 is done. Sleep (1000) MaxWaittime = 30000 'If pdf995 isn't done in 5 min, quit anyway Do Do While ReadINIfile("PARAMETERS", "Generating PDF CS", SyncFile) = "1" And MaxWaittime > 0 Sleep (1000) MaxWaittime = MaxWaittime - 1000 Loop ' restore the original default printer and the PDF995.ini settings CleanUp: Sleep (1000) X X = WritePrivateProfileString("PARAMETERS", "Output File", TmpOutputFile, IniFileName) X = WritePrivateProfileString("PARAMETERS", "AutoLaunch", TmpAutoLaunch, IniFileName) X = WritePrivateProfileString("PARAMETERS", "Launch", "", IniFileName) 'On Error Resume Next ' if running on Access 2000 or earlier, comment out the next line 'Application.Printer = tmpPrinter PDFWrite = CaptionFile ' Name of saved file End Function Function ReadINIfile(sSection As String, sEntry As String, sFilename As String) As String Dim X As Long Dim sDefault As String Dim sRetBuf As String, iLenBuf As Integer Dim sValue As String 'Six arguments 'Explanation of arguments: 'sSection: ini file section (always between brackets) 'sEntry : word on left side of "=" sign 'sDefault$: value returned if function is unsuccessful 'sRetBuf$ : the value you're looking for will be copied to this buffer string 'iLenBuf% : Length in characters of the buffer string 'sFileName: Path to the ini file sDefault$ = "" sRetBuf$ = String$(256, 0) '256 null characters iLenBuf% = Len(sRetBuf$) X = GetPrivateProfileString(sSection, sEntry, _ sDefault$, sRetBuf$, iLenBuf%, sFilename) ReadINIfile = Left$(sRetBuf$, X) End Function
From: Albert S. on 1 Jun 2010 14:49
Hello, Thanks for the information. We are pretty happy with the Lebans function and it may turn out that the one you recommended will also cause an error. What I really want to do is just switch the default printer and then switch it back. Also, it may not be the pdf output, but the sending by mail to Outlook. I am going to try to stop the code execution before it outputs the email message and see if that helps. I will post back if any success. Albert S. "Phil" <phil(a)stantonfamily.co.uk> wrote in message news:I7ednaIl0_1pz5jRnZ2dnUVZ7oadnZ2d(a)brightview.co.uk... > On 01/06/2010 00:22:36, "Albert" wrote: >> Hello, >> >> We are using Lebans ReportToPDF module and are having conflicts with a >> printer driver. It seems that if we switch the default printer to any >> other printer it works fine. >> >> My question is: Is there a way to change the Windows Default printer >> setting in VBA, then set it back after? >> >> I tried setting the Application.Printer, etc., but the conflict still >> exists because this only changes the printer for Access and not for >> Windows. >> >> Thanks for any help! >> Albert S. >> >> > > This may help. Uses a freebee PDF995 printer, but as far as I remember > that > can be programmed > > Option Compare Database 'Use database order for string comparisons > Option Explicit > > ' Code from: > ' Microsoft Access 95 How-To > ' (c) 1998 Ken Getz and Paul Litwin > ' All rights reserved. > > ' You may only use this code as part of an application > ' that requires its use. You must including this > ' notice intact. You may not distribute the code > ' as your own work, nor can you distribute the > ' code on its own. > > Function ahtGetDefaultPrinter(dr As aht_tagDeviceRec) As Boolean > > ' Retrieve the default printer information. Though > ' the function dutifully returns True if the > ' values were available, and False otherwise, Windows > ' really isn't happy without a default printer, and > ' this situation rarely comes up. > > ' In: > ' dr: a aht_tagDeviceRec structure to fill in > ' Out: > ' Return Value: True if info available, False otherwise. > ' dr: filled in with default printer information, > ' if it was available (check the function's return > ' value). > ' > ' Comments: > ' Requires the ahtGetToken() function from basGetToken > ' Requires the ahtGetINIString() function from basINIFile > ' Requires type definitions from basPrintTypes > > Dim strBuffer As String > > strBuffer = ahtGetINIString("Windows", "Device") > If Len(strBuffer) > 0 Then > With dr > .drDeviceName = ahtGetToken(strBuffer, ",", 1) > .drDriverName = ahtGetToken(strBuffer, ",", 2) > .drPort = ahtGetToken(strBuffer, ",", 3) > End With > ahtGetDefaultPrinter = True > Else > ahtGetDefaultPrinter = False > End If > End Function > > Function ahtSetDefaultPrinter(dr As aht_tagDeviceRec) As Boolean > > ' Set the default printer device in Win.INI > > ' In: > ' dr: a aht_tagDeviceRec structure to use as > ' the source of information. > ' Out: > ' Return Value: True if set correctly, False otherwise. > ' If successful, writes a string in the form: > ' device=HP LaserJet 4,HPPCL5E,LPT1: > ' to your Win.INI file. > ' > ' Comments: > ' ' Requires the aht_apiWriteProfileString() declaration from basINIFile > ' Requires type definitions from basPrintTypes > > Dim strBuffer As String > > ' Build up the appropriate string. > strBuffer = dr.drDeviceName & "," > strBuffer = strBuffer & dr.drDriverName & "," > strBuffer = strBuffer & dr.drPort > > ' Now write that string out to WIN.INI. > ahtSetDefaultPrinter = (aht_apiWriteProfileString("Windows", _ > "Device", strBuffer) <> 0) > End Function > > Function TestDefaultPrinter() > > ' Test the ahtDefaultPrinter() function. > ' Fill in a DeviceRec structure with > ' the pieces of the default printer info, > ' and then print them out. > > Dim dr As aht_tagDeviceRec > > If ahtGetDefaultPrinter(dr) Then > Debug.Print "Device: "; dr.drDeviceName > Debug.Print "Driver: "; dr.drDriverName > Debug.Print "Port : "; dr.drPort > End If > End Function > > And This > > Option Compare Database > Option Explicit > > 'Read INI settings > Declare Function GetPrivateProfileString Lib "kernel32" Alias _ > "GetPrivateProfileStringA" (ByVal lpApplicationName As String, _ > ByVal lpKeyName As Any, ByVal lpDefault As String, _ > ByVal lpReturnedString As String, ByVal nSize As Long, _ > ByVal lpFileName As String) As Long > > 'Write settings > Declare Function WritePrivateProfileString Lib "kernel32" Alias _ > "WritePrivateProfileStringA" (ByVal lpApplicationName As String, _ > ByVal lpKeyName As Any, ByVal lpString As Any, _ > ByVal lpFileName As String) As Long > > Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) > > ' These functions used with procedures from > ' Microsoft Access 95 How-To > '(c) 1998 Ken Getz and Paul Litwin > ' All rights reserved. > > ' Other modules from this source are: > ' basDefaultPrinter > ' basGetPrinters > ' basIniFile > ' basPrintTypes > ' basToken > > ' You'll also need defaultprt.zip > > Private drexisting As aht_tagDeviceRec > 'Const AcrobatName = "Acrobat PDFWriter" > 'Const AcrobatDriver = "PDFWRITR" > 'Const AcrobatPort = "LPT1:" > > Const AcrobatName = "PDF995" > Const AcrobatDriver = "PDF995" > Const AcrobatPort = "NE01:" > > Sub ResetDefaultPrinter() > > Call ahtSetDefaultPrinter(drexisting) > > End Sub > > Function ChangeToAcrobat() > If ahtGetDefaultPrinter(drexisting) Then > Dim dr As aht_tagDeviceRec > With dr > .drDeviceName = AcrobatName > .drDriverName = AcrobatDriver > .drPort = AcrobatPort > End With > Call ahtSetDefaultPrinter(dr) > End If > > End Function > > Sub ChangePdfFileName(NewFileName As String) > > Call Call aht_apiWriteProfileString("Acrobat PDFWriter", "PDFFileName", > NewFileName) > > End Sub > > Function PDFWrite(ReportName As String, PDFPath As String, ToKill As > Boolean, > _ Optional RptCaption As String, Optional StrCriteria As String) As String > '?pdfwrite("RptWaitingList", "C:\Documents and Settings\Phil\My > Documents\Access\MDB\WFYC\PDFS", true) > > ' Runs an Access report to PDF995 to create a pdf file from the report. > ' Input parameters are the name of the report within the current database, > ' the path for the output file, and an optional criteria for the report > > ' Be sure to check that the "Generating PDF CS" setting in pdfsync.ini is > set > to 0 ' when pdf995 is idle. This codes uses that as a completion flag as > it > seems to be ' the most reliable indication that PDF995 is done writing the > pdf file. > > > ' Note: The application.printer object is not valid in Access 2000 > ' and earlier. In that case, set the printer in the report to pdf995 > ' and comment out the references herein to the application.printer > > Dim SyncFile As String, MaxWaittime As Long > Dim IniFileName As String ', tmpPrinter As Printer > Dim OutputFile As String, CaptionFile As String > Dim X As Long > Dim TmpOutputFile As String, TmpAutoLaunch As String > > ' set the location of the PDF995.ini and the pdfsync files > IniFileName = "c:\pdf995\res\pdf995.ini" > SyncFile File = "c:\documents and settings\all users\application > data\pdf995\res\pdfsync.ini" > > ' build the output file name from the path parameter and the report name > If Mid(PDFPath, Len(PDFPath), 1) <> "\" Then PDFPath = PDFPath & "\" > OutputFile = PDFPath & ReportName & ".pdf" > > If RptCaption = "" Then > RptCaption = ReportName > End If > CaptionFile File = PDFPath & RptCaption & ".pdf" ' This appears to be the > name that is saved > > ' ' PDF995 operates asynchronously. We need to determine when it is done > so > we can ' continue. This is done by creating a file and having PDF995 > delete > it using the ' ProcessPDF parameter in its ini file which runs a command > when > it is complete. > > ' save current settings from the PDF995.ini file > TmpOutputFile = ReadINIfile("PARAMETERS", "Output File", IniFileName) > TmpAutoLaunch = ReadINIfile("PARAMETERS", "Autolaunch", IniFileName) > > ' remove previous pdf if it exists > On Error Resume Next > > If ToKill = True Then 'if you need a new file > If Dir(CaptionFile) <> "" Then ' and can find it > Kill CaptionFile ' delete it > End If > End If > > On Error GoTo CleanUp > > ' setup new values in PDF995.ini > X X = WritePrivateProfileString("PARAMETERS", "Output File", OutputFile, > IniFileName) X = WritePrivateProfileString("PARAMETERS", "AutoLaunch", > "0", > IniFileName) > > ' change the default printer to PDF995 > ' if running on Access 2000 or earlier, comment out the next two lines > 'Set tmpPrinter = Application.Printer > 'Application.Printer = Application.Printers("PDF995") > > Call ChangeToAcrobat ' Set default printer to > PDF995 > > 'print the report > DoCmd.OpenReport ReportName, acViewNormal, , StrCriteria > > Call ResetDefaultPrinter ' Reset default printer > > ' cleanup delay to allow PDF995 to finish up. When flagfile is nolonger > present, PDF995 is done. Sleep (1000) > MaxWaittime = 30000 'If pdf995 isn't done in 5 min, quit anyway > Do Do While ReadINIfile("PARAMETERS", "Generating PDF CS", SyncFile) = "1" > And MaxWaittime > 0 Sleep (1000) > MaxWaittime = MaxWaittime - 1000 > Loop > > ' restore the original default printer and the PDF995.ini settings > CleanUp: > Sleep (1000) > X X = WritePrivateProfileString("PARAMETERS", "Output File", > TmpOutputFile, > IniFileName) X = WritePrivateProfileString("PARAMETERS", "AutoLaunch", > TmpAutoLaunch, IniFileName) X = WritePrivateProfileString("PARAMETERS", > "Launch", "", IniFileName) 'On Error Resume Next > > ' if running on Access 2000 or earlier, comment out the next line > 'Application.Printer = tmpPrinter > PDFWrite = CaptionFile ' Name of saved file > > End Function > > Function ReadINIfile(sSection As String, sEntry As String, sFilename As > String) As String > > Dim X As Long > Dim sDefault As String > Dim sRetBuf As String, iLenBuf As Integer > Dim sValue As String > > 'Six arguments > 'Explanation of arguments: > 'sSection: ini file section (always between brackets) > 'sEntry : word on left side of "=" sign > 'sDefault$: value returned if function is unsuccessful > 'sRetBuf$ : the value you're looking for will be copied to this buffer > string > 'iLenBuf% : Length in characters of the buffer string > 'sFileName: Path to the ini file > > sDefault$ = "" > sRetBuf$ = String$(256, 0) '256 null characters > iLenBuf% = Len(sRetBuf$) > X = GetPrivateProfileString(sSection, sEntry, _ > sDefault$, sRetBuf$, iLenBuf%, sFilename) > ReadINIfile = Left$(sRetBuf$, X) > > End Function |