79556997

Date: 2025-04-05 13:07:15
Score: 0.5
Natty:
Report link

Sorry OCD just kicked in:

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

Dim shell As Object
Dim waitc As Boolean: waitc = False     ' Wait until Complete
Dim style As Integer: style = 1         ' Not sure what this is for
Dim errco As Integer                    ' Error Code
Dim fpath As String                     ' Full path to the OneDrive executable
Dim fcomm As String                     ' Full command

    fpath = "C:\Users\%username%\AppData\Local\Microsoft\OneDrive\OneDrive.exe"
    Set shell = VBA.CreateObject("WScript.Shell")
    
    Select Case action
    Case 0
        fcomm = Chr(34) & fpath & Chr(34) & " /shutdown"
    Case 1
        fcomm = Chr(34) & fpath & Chr(34) & " /background"
    End Select
    
    errco = shell.Run(fcomm, style, waitc)
    ManageOnedriveSync = errco
    
End Function

I turned it into a function so it can be checked if sucessfully completed.
Also changed the meaning of the input parameters for more logic and intuitive
0 to close, 1 to start again.

Shutdown:

x = ManageOnedriveSync(0)

Start in the background (without opening the File Explorer):

x = ManageOnedriveSync(1)
Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Arnold