Search code examples
excelvbaexport-to-pdf

How to export a pdf with ExportAsFixedFormat(), without resaving, if the Excel file is on cloud storage


I have an Excel sheet with macros. I export a pdf of a sheet using the following code:

Public Sub NowPrintThePage(ByVal LieferantPdfName As String)
    PdfName = Sheets(LieferantPdfName).Range("F16").Value & " " & Mid(ThisWorkbook.Name, 10)
    Sheets(LieferantPdfName).ExportAsFixedFormat _
                                Type:=xlTypePDF, _
                                Filename:=PdfName, _
                                Quality:=xlQualityStandard, _
                                IncludeDocProperties:=False, _
                                IgnorePrintAreas:=False, _
                                OpenAfterPublish:=True
End Sub

I do not save the pdf file. I open it, from a folder within Windows, such that the user can decide what to do with it.

If I open the Excel file from cloud storage (OneDrive, MS Teams, etc.), the macro throws an exception at the line with ExportAsFixedFormat(...).

My guess is, that Excel tries to save the pdf before displaying to the folder of the Excel file - which is not possible because it is loaded from the cloud.
Since I do not want to save the pdf file, what can I do to generate the pdf and show it to the user, even if on the cloud?


Solution

  • Get the user's TEMP folder with this VBA function: ENVIRON("TMP")