1

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

1 Answer 1

6

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
1
  • Getting "Method 'Run' of object 'IWshtShell3' failed" error on this line errorcode = shell.Run(path, style, waitTillComplete)
    – Prem
    Commented Sep 29, 2023 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.