[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


Access: save Excel file to PDF... suddenly not working

Posted on 2009-06-29
Medium Priority
Last Modified: 2012-05-07
Hi EE,
About 2 weeks ago, I searched thru EE as to how to save Excel file as PDF file.
Got ideas from numerous points, mainly to use ps file, distiller to create PDF file with a name.

I ran Adobe's update on pc on Friday.
The PDFtoFile of distiller is not working.... Below is a code snippet.....

I'm open to other approaches, as long as I can create pdf file, as I need to ftp the file
to a web server.

Please help... thx, sandra

Public Sub f005_CopyFromRecordSet(strSql As String, _
                                  strCPath As String, _
                                  strExcelFile As String, _
                                  strBackRevisedExcelFile As String, _
                                  strBackPDFFile As String)
Dim strExcelPath          As String
Dim strPSfile             As String
Dim strLOGfile            As String
Dim lngPos                As Long
Dim db                    As DAO.Database
Dim rst                   As DAO.Recordset
Dim fld                   As DAO.Field
strExcelPath = strCPath & strExcelFile
strBackRevisedExcelFile = strCPath & Mid(strExcelFile, 6)
Call e090_LaunchExcel
' Set objExcel = CreateObject("Excel.Application")
Set objExcelActiveWkbs = objExcel.Workbooks
Set objExcelActiveWkb = objExcelActiveWkbs.Open(Filename:=strExcelPath)
Set objExcelActiveWS = objExcel.ActiveSheet
objExcel.Visible = True
Set db = CurrentDb
Set rst = db.OpenRecordset(strSql)
Debug.Print strSql
objExcelActiveWS.Range("A2").CopyFromRecordset rst
With objExcelActiveWS
     With .Cells
         With Selection.Borders(xlDiagonalDown)
              .LineStyle = xlNone
         End With
         With Selection.Borders(xlDiagonalUp)
              .LineStyle = xlNone
         End With
         With Selection.Borders(xlEdgeLeft)
              .LineStyle = xlContinuous
              .Weight = xlThin
              .ColorIndex = xlAutomatic
         End With
         With Selection.Borders(xlEdgeTop)
             .LineStyle = xlContinuous
             .Weight = xlThin
             .ColorIndex = xlAutomatic
         End With
         With Selection.Borders(xlEdgeBottom)
             .LineStyle = xlContinuous
             .Weight = xlThin
             .ColorIndex = xlAutomatic
         End With
         With Selection.Borders(xlEdgeRight)
             .LineStyle = xlContinuous
             .Weight = xlThin
             .ColorIndex = xlAutomatic
         End With
         With Selection.Borders(xlInsideVertical)
             .LineStyle = xlContinuous
             .Weight = xlThin
             .ColorIndex = xlAutomatic
         End With
         With Selection.Borders(xlInsideHorizontal)
             .LineStyle = xlContinuous
             .Weight = xlThin
             .ColorIndex = xlAutomatic
         End With
     End With
     With .Columns("E:E")
          Selection.NumberFormat = "mm/dd/yy;@"
     End With
    With .Columns("F:F")
         Selection.NumberFormat = "mm/dd/yy;@"
    End With
End With
objExcel.DisplayAlerts = False
' save Excel file
objExcelActiveWkb.SaveAs Filename:= _
     strBackRevisedExcelFile, _
     FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
     ReadOnlyRecommended:=False, CreateBackup:=False
' Create PS, LOG, PDF file names from Excel file name
lngPos = InStr(strBackRevisedExcelFile, ".")
strPSfile = Left(strBackRevisedExcelFile, lngPos) & "ps"
strLOGfile = Left(strBackRevisedExcelFile, lngPos) & "log"
strBackPDFFile = Left(strBackRevisedExcelFile, lngPos) & "pdf"
Debug.Print strPSfile
Debug.Print strLOGfile
Debug.Print strBackPDFFile
' objExcel.ActivePrinter = "Adobe PDF"
' objExcel.ActivePrinter = "Adobe PDF on Ne05:"
' objExcel.ActivePrinter = "CutePDF Writer on CPW2:"
'save default printer
Dim strDefaultPrinter As String
strDefaultPrinter = objExcel.ActivePrinter
' print Excel file to Acrobat PS file
Dim objPDF_Distiller As PdfDistiller
Set objPDF_Distiller = New PdfDistiller
objExcel.ActiveSheet.PrintOut , Copies:=1, _
PrintToFile:=True, PrToFilename:=strPSfile
' Convert Acrobat PS file to PDF file
objPDF_Distiller.FileToPDF strPSfile, strBackPDFFile, ""
' delete work files
Kill strPSfile
Kill strLOGfile
Set objPDF_Distiller = Nothing
'Restore the default settings
objExcel.ActivePrinter = strDefaultPrinter
objExcel.DisplayAlerts = True
Call e110_CloseExcel(True)
Set objExcel = Nothing
Set objExcelActiveWkb = Nothing
Set objExcelActiveWkbs = Nothing
Set objExcelActiveWS = Nothing
Set rst = Nothing
Set db = Nothing
Set fld = Nothing
End Sub

Open in new window

Question by:mytfein
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
  • 5
  • 4
LVL 39

Accepted Solution

thenelson earned 2000 total points
ID: 24736389
You can print the report to a pdf printer driver:
www.pdfill.com                 (my favorite)
http://www.cutepdf.com/   (recommended by several other in this TA)
www.pdf995.com              (free version is nagware)

Or you can use automation to create a pdf in Adobe Acrobat.  
Or you can interface with Ghostscript: www.almosawe.ae/jaffer/pdf-Report.zip
Or you can use this: http://www.lebans.com/reporttopdf.htm

Author Comment

ID: 24736531
Thx TheNelson,
a) You wrote alot of info... thank you...

b) Do you have code to "print" to these printers, yet save as pdf?

pls advise, tx, sandra


Author Comment

ID: 24736624
Hi TheNelson,

I just noticed this post by you, suggesting Fox it -
   would that print a pdf to a file?

tx, sandra
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

LVL 39

Assisted Solution

thenelson earned 2000 total points
ID: 24737039
A pdf printer driver looks like any other printer loaded into the OS except it creates a pdf file instead cf sending data to a physical printer.  For example When I click on Start - Printers And Faxes, one of the printers displayed is "PDFill PDF&Image Writer".  When I print anything, if I select that printer, a pdf file is saved. So You can select that printer for an Access report. Selecting a printer for a report using code depends upon the Access version you have. See the discussion below:

Take a look at Leban's Report to Pdf: http://www.lebans.com/reporttopdf.htm

Foxit views pdf files. It's a faster, smaller, more featured replacement for Adobe Reader. You could use it to print to a pdf printer driver but that would be creating a pdf file from a pdf file (I would think copy would be easier).

Access 2002+, you can use printer objects:

Sub SetPrinter(strFormname As String)

    DoCmd.OpenForm FormName:=strFormname, view:=acDesign, _
                   datamode:=acFormEdit, windowmode:=acHidden
    With Forms(form1).Printer   'Within the form's code you can use With Me.Printer
        .DeviceName = **Name of printer as listed in Printers and Faxes**    'To change the printer  You also need to set UseDefaultPrinter = false

        .TopMargin = 1440
        .BottomMargin = 1440
        .LeftMargin = 1440
        .RightMargin = 1440
        .ColumnSpacing = 360
        .RowSpacing = 360
        .ColorMode = acPRCMColor
        .DataOnly = False
        .DefaultSize = False
        .ItemSizeHeight = 2880
        .ItemSizeWidth = 2880
        .ItemLayout = acPRVerticalColumnLayout
        .ItemsAcross = 6
        .Copies = 1
        .Orientation = acPRORLandscape
        .Duplex = acPRDPVertical
        .PaperBin = acPRBNAuto
        .PaperSize = acPRPSLetter
        .PrintQuality = acPRPQMedium
    End With
    DoCmd.Close objecttype:=acForm, objectname:=strFormname, _

End Sub

Before 2002 with an mda file you can use PrtDevMode, PrtDevNames, and PrtMip properties - very complicated - consult the Win32 Software Development Kit for complete documentation or http://www.mvps.org/access/reports/rpt0009.htm or you can use 's an API workaround too:
Microsoft: Access Modules (VBA Coding) FAQ - Change the Default Printer

Before 2002 with an mde file, your only option is using RunCommand acCmdPageSetup with Sendkeys before each print. For example:

Sendkeys "{RIGHT}%zll~"
RunCommand acCmdPageSetup

will set the page setup to legal size paper for most printers.

Author Comment

ID: 24737195
thx  the Nelson,

ok, i'm not advanced like you...

i want to create a pdf from Excel (though I'm in Access controlling Excel)
i think the above is to create pdf from access report, which is not what i need.

(can you pls check another post that i just added to EE, I read another of your posts about printing
pdf to printer using foxit/shell, and after installing foxit for some reason i changed my my mind, and
now pdfs are opening in a reduced mode, instead of full page, and i do not know how to set
the default back....)

also, is there any way to make the acrobat distiller technique work again?
tx, s

LVL 39

Expert Comment

ID: 24737412
<i want to create a pdf from Excel>
What I think I would do is link to the spreadsheet (In Access database window, tables, select New, then link table) and create a report that I would use to create the pdf file.

<now pdfs are opening in a reduced mode, instead of full page, >
Sorry abut that. Look like captainreiss gave you a possibl solution in question # 24530334.

<also, is there any way to make the acrobat distiller technique work again?>
I'm not all that familiar with acrobat distiller.  Why don't you add the acrobat zone to this question?

Author Comment

ID: 24737586
thx the nelson,
i still wantt to try from excel to pdf, before access report to pdf....
will be in touch, tx, s

Author Comment

ID: 24738859
Hi theNelson,

i will be closing this post - and since you provide info that would be useful for future ....

i posted a similar quest, and incidentally someone else asked a quest on this subject in the past week,
if you take a look, you will see my hoped for approach...

other wise i might try installing fox it, again, it's just that alot of my settings got switched an not sure
if fox it did that... tx, s
LVL 39

Expert Comment

ID: 24739518
You're welcome.  Glad to help and thank you very much for the points with "A" grade!

Happy Computing!


Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

650 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