Solved

How can I fix VBA code so that a spreadsheet exports to PDF

Posted on 2015-01-15
3
201 Views
Last Modified: 2015-01-15
Hello Experts,

Im trying to help a user in my office with a Excel 2010 spreadsheet. The spreadsheet was originally created on a MAC. There is visual basic code that allows the spreadsheet to be saved as a PDF file. I am not proficient with visual basic. When I try to run it I get Run Time 1004: Method 'SaveAs' of object' _Workbook' failed. Please see the code below:

 Sub ExportPDF()
'
' SavePDF Macro
'

'
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    ActiveWorkbook.Save
     
    'Define names
    Sheets("MonthlyReport").Select
    CurrentMonth = Sheets("DropDown").Range("L2").Value
    QuarterNumber = Sheets("DropDown").Range("L1").Value
    AttorneyName = Sheets("MonthlyReport").Range("B3").Value
    CurrentYear = Sheets("MonthlyReport").Range("B1").Value
   
    'Clear and set print areas
    Sheets("MonthlyReport").Select
    ActiveSheet.PageSetup.PrintArea = ""
    Application.Goto Reference:=QuarterNumber
    ActiveSheet.PageSetup.PrintArea = Selection.Address
   
    Sheets("Questions").Select
    ActiveSheet.PageSetup.PrintArea = ""
    Application.Goto Reference:=CurrentMonth
    ActiveSheet.PageSetup.PrintArea = Selection.Address
   
    Sheets("PlenaryInventory").Select
    ActiveSheet.PageSetup.PrintArea = ""
    Application.Goto Reference:="PI_" & CurrentMonth
    ActiveSheet.PageSetup.PrintArea = Selection.Address
   
    Sheets("ESOACaseInventory").Select
    ActiveSheet.PageSetup.PrintArea = ""
    Application.Goto Reference:="ES_" & CurrentMonth
    ActiveSheet.PageSetup.PrintArea = Selection.Address

    'Export to PDF
   
    ActiveWorkbook.SaveAs Filename:=AttorneyName & "_" & CurrentYear & "_" & CurrentMonth & ".pdf", FileFormat:=xlPDF

    'ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF _
    ', Filename:=AttorneyName & "_" & CurrentYear & "_" & CurrentMonth & ".pdf" _
    ', Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
     

    Sheets("MonthlyReport").Select
       


I am also attaching the spreadsheet.  Any help you can provide will be greatly appreciated.

Thanks so much.
Jose
Monthly-Report-Template-V2.xlsm
0
Comment
Question by:jselaya
[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
3 Comments
 
LVL 18

Expert Comment

by:Simon
ID: 40551571
I don't think that the "save as PDF" is part of the standard build of Office on Windows.

I have it on my File menu, but I think that's only because I have Acrobat (full version, not reader) installed on my machine.

If you don't have Acrobat (on Windows) you'll need to do it like this:

 ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Users\YourFileNameHere.pdf", _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=True
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 40551573
I suggest you comment out this bit:
ActiveWorkbook.SaveAs Filename:=AttorneyName & "_" & CurrentYear & "_" & CurrentMonth & ".pdf", FileFormat:=xlPDF

Open in new window

and uncomment this bit:
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF _
    ', Filename:=AttorneyName & "_" & CurrentYear & "_" & CurrentMonth & ".pdf" _
    ', Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True

Open in new window

0
 

Author Closing Comment

by:jselaya
ID: 40551611
Thanks so much. It now works!
0

Featured Post

Industry Leaders: 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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

756 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