Solved

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

Posted on 2014-01-07
7
4,633 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 49

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 27

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 27

Accepted Solution

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
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 …
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…

919 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

18 Experts available now in Live!

Get 1:1 Help Now