Excel Macro for Save as PDF (to a share point folder)

drefiek2

Board Regular
Joined
Apr 23, 2023
Messages
59
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi,
I am very new to macros and would like to know how I can do the following. I have a single sheet in an excel workbook and I want a button which will:
1.save the sheet (an array of cells) as a PDF (landscape) in a particular work sharepoint folder
2. The pdf file name should be whatever is in cell C3
3. If the save is successful I want a pop up message box to say that. If it is unsuccessful I would like an error message box to say that.


Thank you in advance.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Use the Excel Macro Recorder to record yourself performing Step 1 as you have described above. Then post the code generated by the macro recorder here and we will see if it is possible to automate it to save to your SharePoint folder.
 
Upvote 0
Hi, I recorded the macro - here it is:
Note I have removed company information from the web address.

VBA Code:
Sub SaveAsPDF()
'
' SaveAsPDF Macro
'
' Keyboard Shortcut: Ctrl+Shift+S
'
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
        "https://COMPANY-my.sharepoint.com/personal/COMPANY/Test.pdf" _
        , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=False
End Sub
 
Upvote 0
So, one way to do it.
VBA Code:
Sub SaveAsPDF()
'
' SaveAsPDF Macro
'
' Keyboard Shortcut: Ctrl+Shift+S
'
    Dim SharePointPath As String
    Dim PdfFileName As String
    Dim Msg As String

    On Error GoTo SaveError

    SharePointPath = "https://COMPANY-my.sharepoint.com/personal/COMPANY/" '<<<<<<<<<<<< edit as required.

    PdfFileName = ActiveSheet.Range("C3").Value

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=SharePointPath & PdfFileName, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

    Msg = "File save was successful"
    MsgBox Msg, vbInformation, "Success"
    Exit Sub

SaveError:
    Msg = "File save was not successful:" & vbCr & vbCr & Err.Number & " - " & Err.Description
    MsgBox Msg, vbCritical, "Save Error"
End Sub
 
Upvote 0
Solution
Hi, thank you that works perfectly so far! Tried and tested.

Cell C3 is a date cell (U.K format e.g. Monday 24 April 2023). I have used
Excel Formula:
=TODAY()
as the function here which auto-populates on open.

In the SharePoint folder I have chosen to save the PDFs in, there will be folders for years and then folders for months inside those folders. Is there a way for the macro to use the date in C3 to determine which year and folder to save the PDFs in or is that way out of Excel's reach?
 
Upvote 0
Hi,
After more testing I noticed that certain date formats in cell C3 will not save as PDF to the specified folder. It doesn't seem to accept / or . within the date e.g. 24/04/2023 or 24.04.2023. I have formatted the date so that it appears as e.g. Monday 24 April 2023 on the sheet, but in the formula bar it still shows 24.04.2023 or 24/04/2023.
 
Upvote 0
Hi,
After more testing I noticed that certain date formats in cell C3 will not save as PDF to the specified folder. It doesn't seem to accept / or . within the date e.g. 24/04/2023 or 24.04.2023. I have formatted the date so that it appears as e.g. Monday 24 April 2023 on the sheet, but in the formula bar it still shows 24.04.2023 or 24/04/2023.
There are certain characters that are illegal to use in a windows file name. Typically:

< (less than)​
> (greater than)​
: (colon - sometimes works, but is actually NTFS Alternate Data Streams)​
" (double quote)​
/ (forward slash)​
\ (backslash)​
| (vertical bar or pipe)​
? (question mark)​
* (asterisk)​
and all Non-printable characters​

 
Upvote 0
There are certain characters that are illegal to use in a windows file name. Typically:

< (less than)​
> (greater than)​
: (colon - sometimes works, but is actually NTFS Alternate Data Streams)​
" (double quote)​
/ (forward slash)​
\ (backslash)​
| (vertical bar or pipe)​
? (question mark)​
* (asterisk)​
and all Non-printable characters​

Hi, thank you for clarifying. After some searching I've tweaked my code and it now saves as a PDF using the date cell and removing the illegal characters. It works a treat but I have one more thing I would like it to do and I'm not sure how to do it. When saving I do not want it to overwrite if it has already been saved as a PDF (for example the user has forgotten to change the date in C3). How do I add a check to make sure it will not overwrite a saved PDF, including a pop up warning box beforehand? This is my code:

VBA Code:
Sub SaveAsPDF()
'
' SaveAsPDF Macro
'
' Keyboard Shortcut: Ctrl+Shift+S
'
    Dim SharePointPath As String
    Dim PdfFileName As String
    Dim Msg As String

    On Error GoTo SaveError

    SharePointPath = "FILE LOCATION" '<<<<<<<<<<<< edit as required.

    PdfFileName = Replace(Range("D6").Value, "/", "") & ActiveSheet.Range("D7").Value

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=SharePointPath & PdfFileName, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

    Msg = "Handover successfully delivered to SharePoint."
    MsgBox Msg, vbInformation, "Save Successful"
    Exit Sub

SaveError:
    Msg = "Handover was not delivered to SharePoint:" & vbCr & vbCr & Err.Number & " - " & Err.Description
    MsgBox Msg, vbCritical, "Save Failure"
End Sub
 
Upvote 0
Possibly this, but I have never personally tested whether the FileSystemObject's FileExists method works with an http style SharePoint address.
VBA Code:
Sub SaveAsPDF()
    '
    ' SaveAsPDF Macro
    '
    ' Keyboard Shortcut: Ctrl+Shift+S
    '
    Dim SharePointPath As String
    Dim PdfFileName As String
    Dim Msg As String
    
    On Error GoTo SaveError
    
    SharePointPath = "FILE LOCATION" '<<<<<<<<<<<< edit as required.
    
    PdfFileName = Replace(Range("D6").Value, "/", "") & ActiveSheet.Range("D7").Value
    
    With CreateObject("Scripting.FileSystemObject")
        If .FileExists(SharePointPath & PdfFileName) Then
            MsgBox "File already exists"
        Else
            ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=FPath
            Debug.Print "File Saved"
    
            ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=SharePointPath & PdfFileName, _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
            
            Msg = "Handover successfully delivered to SharePoint."
            MsgBox Msg, vbInformation, "Save Successful"
        End If
    End With
    Exit Sub
    
SaveError:
    Msg = "Handover was not delivered to SharePoint:" & vbCr & vbCr & Err.Number & " - " & Err.Description
    MsgBox Msg, vbCritical, "Save Failure"
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,169
Members
453,021
Latest member
Justyna P

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