Excel Macro - Save PDF Error

quick_question

New Member
Joined
May 31, 2011
Messages
32
I'm trying to export my active sheet as a pdf while defining the name of the file and path as it will change each time this workbook is opened, but I continue to run into an Compile error / syntax error and can't seem to figure out where I'm going wrong -

Any help is appreciated!!

Sub SavePDF()


Dim FilePath As String
Dim DocName As String


FilePath = Sheets("PO").Range("U11").Value & ""
DocName = Sheets("PO").Range("U12").Value & ".pdf"

Worksheets("PO").Activate

ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
FileName:= FilePath & "" DocName & .pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Sub SavePDF()

It would be helpful to know what's in those cells, but for now remove all the red parts and add all the blue


Dim FilePath As String
Dim DocName As String


FilePath = Sheets("PO").Range("U11").Value & ""
DocName = Sheets("PO").Range("U12").Value & ".pdf"

Worksheets("PO").Activate

ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
FileName:= FilePath & "" DocName & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Sub
 
Upvote 0
In between the blank "" was a \, but didn't copy over for whatever reason.

U11 = C:\Users\jem\Documents\Jobs\Job1
U12 = ABC-123_garage rennovation_20161101
 
Upvote 0
in that case you either have to do this

U11 = C:\Users\jem\Documents\Jobs\Job1\

or use your old line with \ for the first
FilePath line.

Code:
[COLOR=#333333][COLOR=#333333]FilePath = Sheets("PO").Range("U11").Value [/COLOR][/COLOR][B][COLOR=#0000ff]& "\"[/COLOR]
[/B]

 
Last edited:
Upvote 0
Looks like #1 had a path with two ""'s if it had posted right?

With a few error checks...
Code:
Sub SavePDF()
  Dim FilePath As String, DocName As String
  Dim fn As String, ws As String
  
  ws = "PO"
  If Not WorkSheetExists(ws) Then
    MsgBox "Worksheet: " & ws & _
      " does not exist in ActiveWorkbook", _
      vbCritical, "Macro Ending"
    Exit Sub
  End If
  
  With Worksheets(ws)
    FilePath = .Range("U11").Value & "\"
    If Len(Dir(FilePath, vbDirectory)) = 0 Then
      MsgBox FilePath & " does not exist.", _
        vbCritical, "Macro Ending"
      Exit Sub
    End If
    
    DocName = .Range("U12").Value & ".pdf"
    fn = FilePath & DocName
    
    .ExportAsFixedFormat _
      Type:=xlTypePDF, _
      Filename:=fn, _
      Quality:=xlQualityStandard, _
      IncludeDocProperties:=True, _
      IgnorePrintAreas:=False, _
      OpenAfterPublish:=False
  End With
End Sub

 'WorkSheetExists in a workbook:
Function WorkSheetExists(sWorkSheet As String, Optional sWorkbook As String = "") As Boolean
    Dim ws As Worksheet, wb As Workbook
    On Error GoTo notExists
    If sWorkbook = "" Then
      Set wb = ActiveWorkbook
      Else
      Set wb = Workbooks(sWorkbook) 'sWorkbook must be open already.  e.g. ken.xlsm, not x:\ken.xlsm.
    End If
    Set ws = wb.Worksheets(sWorkSheet)
    WorkSheetExists = True
    Exit Function
notExists:
    WorkSheetExists = False
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top