Solved

Save excel worksheet to pdf and use cell to name the file

Posted on 2014-01-07
7
4,525 Views
Last Modified: 2014-01-07
I am attempting to write a macro to save a worksheet to PDF and name it based on a cell value, however I am getting an "expected end of statement error with my code.

Sub savePDF()
     '
     ' savePDF Macro
     '
     
     '
    Dim fname As String
    With ActiveSheet
    fname = .Range("f5").Value
    .ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
    "C:\Users\IJ64770\Desktop\"& fName", Quality:= _
    xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=True
End With
End Sub

Any suggestinos as to where I am going wrong?
0
Comment
Question by:DAFranzmann
  • 3
  • 3
7 Comments
 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 39761693
Hi,

pls try

 Dim fname As String
    With ActiveSheet
    fname = .Range("f5").Value
    .ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
    "C:\Users\IJ64770\Desktop\"& fName, Quality:= _
    xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=True
End With

Open in new window

Regards
0
 

Author Comment

by:DAFranzmann
ID: 39761698
Hi Rgonzo1971

The new code returns a Run-time error

Automation error
The filename,Directory name or volume label syntax is incorrect.

Regards
0
 
LVL 26

Expert Comment

by:MacroShadow
ID: 39761699
Extra double quote after fName
Sub savePDF()
'
' savePDF Macro
'

'
    Dim fname As String
    With ActiveSheet
        fname = .Range("f5").Value
        .ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                             "C:\Users\IJ64770\Desktop\" & fname, Quality:= _
                             xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
                             OpenAfterPublish:=True
    End With
End Sub

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:DAFranzmann
ID: 39761704
I have added the extra quotation marks and it returns a:

compile error
Syntax Error

Sub savePDF()
     '
     ' savePDF Macro
     '
     
     '
Dim fname As String
    With ActiveSheet
    fname = .Range("f5").Value
    .ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
    "C:\Users\IJ64770\Desktop\" & fname", Quality:= _
    xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=True
End With

End Sub
0
 
LVL 26

Accepted Solution

by:
MacroShadow earned 500 total points
ID: 39761719
Remove the quotation mark after fname.
0
 
LVL 26

Expert Comment

by:MacroShadow
ID: 39761721
Can you verify that C:\Users\IJ64770\Desktop\" & fname is valid?
0
 

Author Closing Comment

by:DAFranzmann
ID: 39761724
Thanks MacroShadow

Sub savePDF()
     '
     ' savePDF Macro
     '
     
     '
Dim fname As String
    With ActiveSheet
    fname = .Range("f5").Value
    .ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
    "C:\Users\IJ64770\Desktop\" & fname, Quality:= _
    xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=True
End With

End Sub
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now