VBA - Save As PDF

Spaztic

New Member
Joined
Jul 27, 2023
Messages
48
Office Version
  1. 365
Platform
  1. Windows
I'm working on a macro that would save my Excel workbook as a PDF.
I'm saving as pdf with some options (ignore print area, save entire workbook)...

This file will be used by many different users so the line "C:\Users\myname\OneDrive - Company\Desktop\folder\trial.pdf" probably won't work as it has my name entered and also the file name (currently 'trial') is not always the same. I'd like the saved pdf file name to be the same as the Excel file and stored in the same location as the Excel file.

Any help would be appreciated!

VBA Code:
Sub SavePDF()

    ActiveWorkbook.Save
    ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Users\myname\OneDrive - Company\Desktop\folder\trial.pdf", _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=True, OpenAfterPublish:=False

    End Sub
 
Thank you for your help Micron! When using without sharepoint...the code works as intended.
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
AFAIK, sharepoint paths use / character, file paths use \ so you would have to swap where I used \ in the code. However, I'm fairly certain Dir will not work with URL's and I don't think FSO will either. You would have to find out what the UNC path is to the sp server and use that. I think you could use Windows File Explorer to get the unc path or ask your IT person to help with that. If you use the unc path, do not swap the slash character, otherwise you'll have a forward slash in the path and then it will likely fail.

EDIT - you might be able to discover the unc path to the server if you use file explorer, navigate to a file, right click on it and choose properties. That may show you the path to the file... just use the part that gets you to the folder where the Excel wb is located.
 
Last edited:
Upvote 0
This worked for sharepoint...

VBA Code:
Dim sPath As String, sName As String

sPath = ThisWorkbook.Path
sPath = ThisWorkbook.GetLocalPath(ThisWorkbook.Path)

sName = ThisWorkbook.Name
sName = Left(sName, InStrRev(sName, ".") - 1)

' Check to see if file exists
    Dim strFileExists As String
    Dim strFileName As String
    
    Dim tsfile As String
    
    strFileName = sName & ".pdf"   ' old   sNameBuildFilePath
    tsfile = sPath & "\" & strFileName

    strFileExists = Dir(tsfile)
    
    Dim AnswerYes As String
        
    If strFileExists <> "" Then
        MsgBox "Error: " & strFileName & " already exists"
       
        AnswerYes = MsgBox("Do you Wish to overwrite?", vbQuestion + vbYesNo, "File Exists")
         
        If AnswerYes = vbNo Then
            GoTo End Sub
        End If
        
    End If
  
ThisWorkbook.Save
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, fileName:=sPath & "\" & sName & ".pdf", _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=True, OpenAfterPublish:=False
 
Upvote 0
Instead of this (code in Post #1)
Code:
C:\Users\myname
try
Code:
Environ$("userprofile") & "\"
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,189
Members
452,616
Latest member
intern444

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