Ricardo Diaz
Ricardo Diaz

Reputation: 5696

VBA start stop Onedrive Sync client

Need: Start or stop the Onedrive sync client via VBA

Reason: Running a procedure that looped through files and made some changes caused Excel to crash while Onedrive sync client was running

Upvotes: 1

Views: 4046

Answers (1)

Ricardo Diaz
Ricardo Diaz

Reputation: 5696

Couldn't find a more "elegant" way to do it, but it works

Adjust the program files folder if you're using 32bit version

' Credits: https://stackoverflow.com/questions/49652606/wscript-shell-to-run-a-script-with-spaces-in-path-and-arguments-from-vba
Private Sub ManageOnedriveSync(ByVal action As Integer)

    Dim shell As Object
    Set shell = VBA.CreateObject("WScript.Shell")
    Dim waitTillComplete As Boolean: waitTillComplete = False
    Dim style As Integer: style = 1
    Dim errorcode As Integer
    Dim path As String
    
    Dim commandAction As String
    Select Case action
    Case 1
        commandAction = "/shutdown"
    End Select

    path = Chr(34) & "%programfiles%\Microsoft OneDrive\Onedrive.exe" & Chr(34) & " " & commandAction

    errorcode = shell.Run(path, style, waitTillComplete)

End Sub

Shutdown:

ManageOnedriveSync 1

Start:

ManageOnedriveSync 0

Upvotes: 6

Related Questions