Print to default printer problem - Urgent Please

T

Tim

Hi folks,

I have a vba code to print file to default printer. It works great to all
printer installed in my window XP except "adobe pdf" printer. It pops up a
dialog box and ask for file name. Does anyone know how to suppress the
dialog box and provide the file name via vba? Please help. I have spent two
weeks on this problem.

Thanks in advance.

Tim.
 
A

Albert D. Kallal

The most simple solution is to use Stephan's pdf system here:

http://www.lebans.com/reporttopdf.htm


The advantage of using the above code sample over that of a pdf printer
installed is significant.

*** you will find that you do not have to install some Adobe software, which
not only often problematic but it often breaks after Adobe issues some
automatice update. Also, adobe cost $$$, and the above Stephen solution is
free.

*** and other significant advantage of Stephen solution is that it does not
rely on, no or does it install a printer driver. What this means is that you
can leave the current printer that the user has set up alone, and you do not
have to write additional code to switch or change the default printer
driver. Needless to say switching the default printer can also effect other
applications that you're running. So once again there's a considerable long
list of significant advantages of not having to install a printer driver,
and furthermore there is even a longer list of significant advantages in
which you don't have to switch to a particular printer driver.

*** Stephen solution makes deployment to machines that don't have Adobe
installed a real snap, as you don't once again have to installed all be on
the target machine, nor do you have to install and set up a print driver.

I could probably write on for some pages here, but suffice to say you now
have a solution that is free, eliminates the need to install a printer
driver,and furthermore the system is designed for you to supply the output
PDF name in code.
 
T

Tim

Hi Albert,

Thank you very much for your quick respone. My company does not allow me to
use third party's software unless I get approval (IT department). The adobe
is approved software in our company. Therefore, I need to use it.

Let me explain my problem again. I tried to print files to my default
printer via vba in window XP. The file could be excel file, text file and
pdf file (to reduce the size). When I used the "adobe pdf" printer, it pops
up a dialog window and ask for file name. Is a way to pass file name to the
window and suppress the window?

Thanks a lot.

Tim.
 
A

Albert D. Kallal

Let me explain my problem again. I tried to print files to my default
printer via vba in window XP. The file could be excel file, text file and
pdf file (to reduce the size).

You don't mention how you printing these documents via vba. However, there
is some sample code here:

http://www.mvps.org/access/reports/rpt0011.htm

How the file is printed with different applications likely could change how
you set this, but the above is an example for ms-access....
 
T

Tim

Hi Albert,

Thank you for the info but it does not work with my case because I don't
have pdf writer installed in my system. My version is adobe 7. I used the
following code to print to "adobe pdf". Please help.

Private Declare Function ShellExecute Lib "shell32.dll" Alias
"ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long


Public Sub test()

Call ShellExecute(0, "print", "c:\Temp\debug.txt", "", "", 1)

End Sub

Thanks in advance.

Tim.
 
A

Albert D. Kallal

Tim said:
Hi Albert,

Thank you for the info but it does not work with my case because I don't
have pdf writer installed in my system. My version is adobe 7.

the pdfwriter from Adobe, and Adobe acrobat are much the same product..are
they not?????

Oh well, try some of the Adobe support groups, they should have lots of
examples.

As mentioned, I used Stephens solution for ms-access, and for non ms-access
solutions I use

http://sourceforge.net/projects/pdfcreator/

The beauty/advanges of the above pdfcreater is that you can easily create
windows scripts that not only sets the output file name, but lets you
COMBINE many/several pdf's into one pdf, and then you can simple print that.
The samples for pdfcrator are also really good.

however, in your case, I would suggest trying the adobe support groups, as
there going to people there with a lot more knowledge then in this access
group....
 
G

gllincoln

Hi Tim,

Go into the 'printer properties' of the Adobe PDF, Printing Preferences
button, Adobe PDF Settings tab - change the Adobe PDF Output Folder to
whatever folder you prefer and have it automatically name the file *.pdf -
the default setting is Prompt for PDF filename but this is user
configurable. Might want to consider how you wish duplicate names to be
handled - you can set it up to ask to Replace existing PDF - if this isn't
checked, then it will automatically overwrite the old with the new.

Hope this helps,
Gordon
 
T

ThuUCI

We can programmatically setup print to PDF printer.

Here is the code:

Option Compare Database

Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, ByVal lpWindowName As String) As Long


Private Declare Function RegOpenKeyEx Lib "advapi32.dll" _
Alias "RegOpenKeyExA" (ByVal hKey As Long, _
ByVal lpSubKey As String, _
ByVal ulOptions As Long, _
ByVal samDesired As Long, _
phkResult As Long) As Long

Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long)
As Long

Private Declare Function RegCreateKeyEx Lib "advapi32.dll" _
Alias "RegCreateKeyExA" (ByVal hKey As Long, _
ByVal lpSubKey As String, _
ByVal Reserved As Long, _
ByVal lpClass As String, _
ByVal dwOptions As Long, _
ByVal samDesired As Long, _
ByVal lpSecurityAttributes As
Long, _
phkResult As Long, _
lpdwDisposition As Long) As Long

Private Declare Function RegSetValueEx Lib "advapi32.dll" _
Alias "RegSetValueExA" (ByVal hKey As Long, _
ByVal lpValueName As String, _
ByVal Reserved As Long, _
ByVal dwType As Long, _
lpData As Any, _
ByVal cbData As Long) As Long

Private Declare Function apiFindExecutable Lib "shell32.dll" _
Alias "FindExecutableA" (ByVal lpFile As String, _
ByVal lpDirectory As String, _
ByVal lpResult As String) As Long


Public Const HKEY_CURRENT_USER As Long = &H80000001
Public Const ERROR_SUCCESS As Long = 0
Public Const KEY_ALL_ACCESS As Long = &H3F
Public Const REG_OPTION_NON_VOLATILE As Long = 0
Public Const REG_SZ As Long = 1
Public Const REG_BINARY As Long = 3
Public Const REG_DWORD As Long = 4


Public Const KEY_READ As Long = &H20019
Public Const KEY_WRITE As Long = &H20006





Public Sub PrintReportToPDF2(ByVal strReport As String, ByVal OutputPath As
String)
'On Error GoTo PrintReportToPDF_error
If (Application.Printers.Count <= 0) Then
MsgBox ("There is no printer drive installed on this computer")
Exit Sub
End If

Dim fso As Scripting.FileSystemObject
Set fso = New FileSystemObject

'-------- Check OutputPath
Dim objArrString() As String
Dim strOutputFile As String
Dim strOutputPath As String

If (InStr(OutputPath, ".") < 0) Then
If Not (Right(OutputPath, 1) = "\") Then
OutputPath = OutputPath & "\"
End If
OutputPath = OutputPath & strReport
End If

objArrString = Split(OutputPath, "\")

If (UBound(objArrString) - LBound(objArrString) > 0) Then
strOutputFile = objArrString(UBound(objArrString))
strOutputPath = Left(OutputPath, Len(OutputPath) - Len(strOutputFile))
Else
MsgBox ("Please specify the output path!")
GoTo PrintReportToPDF_error
End If

If Not (fso.FolderExists(strOutputPath)) Then
fso.CreateFolder (strOutputPath)
End If

If Not (GetFileExtension(strOutputFile) = "pdf") Then
If MsgBox("Output file should be pdf file! Do you wish to change file :
'" & strOutputFile & "' to pdf file?", vbYesNo, "Error") = vbNo Then
GoTo PrintReportToPDF_error
Else
OutputPath = Left(OutputPath, Len(OutputPath) - 3) & "pdf"
End If
End If

If (fso.FileExists(OutputPath)) Then

If MsgBox("File : '" & OutputPath + "' already exist! Do you wish to
overwrite?", vbYesNo, "Error") = vbNo Then
GoTo PrintReportToPDF_error
End If

End If
Const PDF_PRINTER As String = "Adobe PDF" '"GS PDFWriter" '"Adobe PDF"
'Const TEMP_PATH As String = "C:\output.pdf"

Dim prtDefaultPrinter As Printer

'DoCmd.OpenReport strReportName, acViewNormal
'

Dim i As Integer
For i = 0 To Application.Printers.Count - 1
If (Application.Printers.Item(i).DeviceName = PDF_PRINTER) Then
Application.Printer = Application.Printers(PDF_PRINTER)
Exit For
End If
Next

'Create the Registry Key where Acrobat looks for a file name
If Not (CreateNewRegistryKey(HKEY_CURRENT_USER, "Software\Adobe\Acrobat
Distiller\PrinterJobControl")) Then
MsgBox "Unexpected error #" & Err.Number & " - " & Err.Description
GoTo PrintReportToPDF_error
End If


'Put the output filename where Acrobat could find it
If Not (SetRegistryValue(HKEY_CURRENT_USER, "Software\Adobe\Acrobat
Distiller\PrinterJobControl", _
Find_Exe_Name(CurrentDb.Name, CurrentDb.Name), OutputPath))
Then

MsgBox "Unexpected error #" & Err.Number & " - " & Err.Description
GoTo PrintReportToPDF_error
End If

'print report

DoCmd.OpenReport strReport, acViewNormal

'-----Close pdf file

Application.Printer = prtDefaultPrinter


'Set fso = New Scripting.FileSystemObject
'fso.CopyFile TEMP_PATH, OutputPath, True
'fso.DeleteFile TEMP_PATH
Set fso = Nothing
Exit Sub
PrintReportToPDF_error:
If Not (prtDefaultPrinter Is Nothing) Then
Application.Printer = prtDefaultPrinter
End If

If Not (fso Is Nothing) Then
Set fso = Nothing
End If
Exit Sub

End Sub

Public Function Find_Exe_Name(prmFile As String, prmDir As String) As String

Dim Return_Code As Long
Dim Return_Value As String

Return_Value = Space(260)
Return_Code = apiFindExecutable(prmFile, prmDir, Return_Value)

If Return_Code > 32 Then
Find_Exe_Name = Return_Value
Else
Find_Exe_Name = "Error: File Not Found"
End If

End Function

Public Function CreateNewRegistryKey(ByVal prmPredefKey As Long, ByVal
prmNewKey As String) As Boolean
On Error GoTo CreateNewRegistryKey_error

Dim hNewKey As Long 'handle to the new key
Dim lRetVal As Long 'result of the RegCreateKeyEx function

lRetVal = RegOpenKeyEx(prmPredefKey, prmNewKey, 0, KEY_ALL_ACCESS, hNewKey)

If lRetVal <> 5 Then
lRetVal = RegCreateKeyEx(prmPredefKey, prmNewKey, 0&, _
vbNullString, REG_OPTION_NON_VOLATILE, _
KEY_ALL_ACCESS, 0&, hNewKey, lRetVal)
End If

RegCloseKey (hNewKey)

If lRetVal = ERROR_SUCCESS Then
CreateNewRegistryKey = True
Exit Function
Else
CreateNewRegistryKey = False
Exit Function
End If
CreateNewRegistryKey_error:
CreateNewRegistryKey = False
Exit Function
End Function

Function SetRegistryValue(ByVal hKey As Long, _
ByVal KeyName As String, _
ByVal ValueName As String, _
Value As Variant) As Boolean

' Write or Create a Registry value
' returns True if successful
' Use KeyName = "" for the default value
' Value can be an integer value (REG_DWORD), a string (REG_SZ)
' or an array of binary (REG_BINARY). Raises an error otherwise.
On Error GoTo SetRegistryValue_error
Dim handle As Long
Dim lngValue As Long
Dim strValue As String
Dim binValue() As Byte
Dim byteValue As Byte
Dim length As Long
Dim retVal As Long

' Open the key, exit if not found
If RegOpenKeyEx(hKey, KeyName, 0, KEY_WRITE, handle) Then
SetRegistryValue = False
Exit Function
End If

' three cases, according to the data type in Value
Select Case VarType(Value)
Case vbInteger, vbLong
lngValue = Value
retVal = RegSetValueEx(handle, ValueName, 0, REG_DWORD, lngValue, 4)
Case vbString
strValue = Value
retVal = RegSetValueEx(handle, ValueName, 0, REG_SZ, ByVal strValue,
Len(strValue))
Case vbArray
binValue = Value
length = UBound(binValue) - LBound(binValue) + 1
retVal = RegSetValueEx(handle, ValueName, 0, REG_BINARY,
binValue(LBound(binValue)), length)
Case vbByte
byteValue = Value
length = 1
retVal = RegSetValueEx(handle, ValueName, 0, REG_BINARY, byteValue,
length)
Case Else
RegCloseKey handle
Err.Raise 1001, , "Unsupported value type"
End Select

RegCloseKey handle ' Close the key and signal success

If (retVal = ERROR_SUCCESS) Then ' signal success if the value was written
correctly
SetRegistryValue = True
Exit Function
Else
SetRegistryValue = False
Exit Function
End If
SetRegistryValue_error:
SetRegistryValue = False
Exit Function
End Function

Private Function GetFileExtension(ByVal strFileName As String) As String
Dim objArrFileExt() As String
objArrFileExt = Split(strFileName, ".")
If (UBound(objArrFileExt) - LBound(objArrFileExt) > 0) Then
'' Get the file extension
GetFileExtension = objArrFileExt(UBound(objArrFileExt))
Exit Function
End If
GetFileExtension = ""

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top