VBA 启动停止 Onedrive 同步客户端

Ric*_*iaz 1 excel vba onedrive

需要:通过 VBA 启动或停止 Onedrive 同步客户端

原因:运行循环遍历文件并进行一些更改的过程导致 Excel 在 Onedrive 同步客户端运行时崩溃

Ric*_*iaz 6

找不到更“优雅”的方式来做到这一点,但它有效

如果您使用的是 32 位版本,请调整程序文件文件夹

' Credits: /sf/ask/3475682451/
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
Run Code Online (Sandbox Code Playgroud)

关闭:

ManageOnedriveSync 1
Run Code Online (Sandbox Code Playgroud)

开始:

ManageOnedriveSync 0
Run Code Online (Sandbox Code Playgroud)