save to pdf not working

orsm6

Active Member
Joined
Oct 3, 2012
Messages
496
Office Version
  1. 365
Platform
  1. Windows
Hi all - i was sure this worked before - but now it doesn't. when i run this macro i get this error: run-time error 1004 Document not saved. The document may be open, or an error may have been encountered when saving.

the document is opened in desktop App but the document is opened from a sharepoint.

Code:
Sub Save_Sheets_As_PDF()

    Dim replaceSelected As Boolean
    Dim ws As Worksheet
    Dim excludeSheets As String
   
    excludeSheets = "|RAW|HOW-TO|"        'The sheet names to exclude delimited by "|"
   
   
    With ThisWorkbook

        replaceSelected = True
        For Each ws In .Worksheets
            If InStr(excludeSheets, "|" & ws.Name & "|") = 0 Then
                ws.Select replaceSelected
                replaceSelected = False
            End If
        Next
           
        .ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="Menu Card.pdf", _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
           
        .Worksheets(1).Select True
   
    End With
   
End Sub

is it because it is on a sharepoint that it isn't working?

in debug mode this line is yellow:
Code:
        .ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="Menu Card.pdf", _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Are you trying to save the PDF to SharePoint? I believe that in the absence of a full export path, such as a user's desktop, the PDF will attempt to be saved to the same location that the excel file is in. Therefore, if the excel file is in SharePoint, the PDF will attempt to save to the same SharePoint location. This might be problematic. I also believe that runtime to be the result of a path permissions issue (i.e.; not able to save to default location).

If you want the PDF to save somewhere specifically, then you'd want to enter that in the Filename:= portion.

For example:

VBA Code:
Activesheet.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
        "https://COMPANY.sharepoint.com/sites////Menu Card.pdf"

If you don't want the pdf to save to SharePoint, but rather a specific location on a user's computer, you could use the following:

Code:
Sub exportit()

Dim user As String: user = Environ("username") 'Sets the system user ID
Dim fPath As String: fPath = "C:\Users\" & user & "\Desktop\" 'Can change Desktop to any other folder
Dim fName As String: fName = "Menu Card"

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    fPath & fName & ".pdf", Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True

End Sub
 
Upvote 0
Solution
Are you trying to save the PDF to SharePoint? I believe that in the absence of a full export path, such as a user's desktop, the PDF will attempt to be saved to the same location that the excel file is in. Therefore, if the excel file is in SharePoint, the PDF will attempt to save to the same SharePoint location. This might be problematic. I also believe that runtime to be the result of a path permissions issue (i.e.; not able to save to default location).

If you want the PDF to save somewhere specifically, then you'd want to enter that in the Filename:= portion.

For example:

VBA Code:
Activesheet.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
        "https://COMPANY.sharepoint.com/sites////Menu Card.pdf"

If you don't want the pdf to save to SharePoint, but rather a specific location on a user's computer, you could use the following:

Code:
Sub exportit()

Dim user As String: user = Environ("username") 'Sets the system user ID
Dim fPath As String: fPath = "C:\Users\" & user & "\Desktop\" 'Can change Desktop to any other folder
Dim fName As String: fName = "Menu Card"

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    fPath & fName & ".pdf", Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True

End Sub
sorry for the late reply - back from holidays - i will put this in and see how I go, thank you for your feedback. will let you know how it goes.
 
Upvote 0
Are you trying to save the PDF to SharePoint? I believe that in the absence of a full export path, such as a user's desktop, the PDF will attempt to be saved to the same location that the excel file is in. Therefore, if the excel file is in SharePoint, the PDF will attempt to save to the same SharePoint location. This might be problematic. I also believe that runtime to be the result of a path permissions issue (i.e.; not able to save to default location).

If you want the PDF to save somewhere specifically, then you'd want to enter that in the Filename:= portion.

For example:

VBA Code:
Activesheet.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
        "https://COMPANY.sharepoint.com/sites////Menu Card.pdf"

If you don't want the pdf to save to SharePoint, but rather a specific location on a user's computer, you could use the following:

Code:
Sub exportit()

Dim user As String: user = Environ("username") 'Sets the system user ID
Dim fPath As String: fPath = "C:\Users\" & user & "\Desktop\" 'Can change Desktop to any other folder
Dim fName As String: fName = "Menu Card"

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    fPath & fName & ".pdf", Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True

End Sub
UPDATE: thank you very much. i used the second code to save to desktop (i see what you mean that it was trying to save back to where file existed on sharepoint). The code you provide works just fine.

thanks again
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,941
Members
449,094
Latest member
teemeren

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