Save as PDF - From onedrive

kgkev

Well-known Member
Joined
Jun 24, 2008
Messages
1,285
Office Version
  1. 365
Platform
  1. Windows
Hi, I have a code which has worked well for a long time, but I noticed that if a file is saved in onedrive it goes to the Else '//Error: file path not found


Is there a simple work around for this?

VBA Code:
Private Sub Save_as_pdf()
Dim FSO As Object
Dim s(1) As String
Dim sNewFilePath As String
    Set FSO = CreateObject("Scripting.FileSystemObject")
    s(0) = ActiveWorkbook.FullName
    If FSO.FileExists(s(0)) Then
        '//Change Excel Extension to PDF extension in FilePath
        s(1) = FSO.GetExtensionName(s(0))
        If s(1) <> "" Then
            s(1) = "." & s(1)
            sNewFilePath = Replace(s(0), s(1), ".pdf")
            '//Export to PDF with new File Path
            ActiveSheet.ExportAsFixedFormat _
                Type:=xlTypePDF, _
                filename:=sNewFilePath, _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, OpenAfterPublish:=True
        End If
    Else '//Error: file path not found
        MsgBox "Error: this workbook may be unsaved.  Please save and try again."
    End If
    Set FSO = Nothing
End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Microsoft recommends synchronizing OneDrive with your disk space.

Try the code below.
You can also use "OneDriveCommercial" or "OneDriveConsumer" as an environment variable in the Environ function, depending on which OneDrive you want to reference.
VBA Code:
Private Sub Save_as_pdf()
    Dim FSO As Object
    Dim s(1) As String
    Dim sNewFilePath As String
    Dim lPos As Long
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    s(0) = ActiveWorkbook.FullName

    If s(0) Like "https://*" Then
        lPos = InStr(1, s(0), "Documents/", vbTextCompare) + Len("Documents/") - 1

        s(0) = Environ("OneDrive") & Replace(Mid(s(0), lPos), "/", "\")
    End If

    If FSO.FileExists(s(0)) Then
        s(1) = FSO.GetBaseName(s(0))
        If s(1) <> "" Then
            sNewFilePath = FSO.GetFile((s(0))).ParentFolder.Path & "\" & s(1)

            '//Export to PDF with new File Path
            ActiveSheet.ExportAsFixedFormat _
                    Type:=xlTypePDF, _
                    Filename:=sNewFilePath, _
                    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                    IgnorePrintAreas:=False, OpenAfterPublish:=True
        End If
    Else    '//Error: file path not found
        MsgBox "Error: this workbook may be unsaved.  Please save and try again."
    End If
    Set FSO = Nothing
End Sub
Artik
 
Upvote 0
brilliant - works perfecting in & out of onedrive.

Thanks
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,977
Members
449,095
Latest member
Mr Hughes

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