簡體   English   中英

關閉所有excel工作簿(可能打開了多個excel實例)

[英]Close all excel workbooks(several instances of excel might be open)

當我發布新的personal.xlsb或用戶需要升級到最新版本時,我試圖關閉所有excel工作簿。 下面是我用於關閉所有excel工作簿的代碼,但是它僅在運行時關閉PERSONAL.XLSB:

Public Sub CloseAll()

Dim wb As Workbook



For Each wb In Application.Workbooks()

    wb.Close True ' Or False if you don't want changes saved

Next wb

Application.Quit

End Sub

這是發布代碼,即將我的personal.xlsb復制到服務器:(可能對其他人有用,因此在此處發布)

Sub publish()
    Call Settings.init 'Contains excelMakroVersjon="101" 

    Dim hFile As Long
    Dim FileContents1 As String
    Dim versionNumber As String
    Dim strFile1 As String


    strFile1 = "Z:\Dokumentstyring\LatestVersion\CopyMacro.bat"

    Kill strFile1
    Kill "Z:\Dokumentstyring\LatestVersion\PERSONAL*"

    versionNumber = Left(excelMakroVersjon, 1) & "." & Right(excelMakroVersjon, Len(excelMakroVersjon) - 1)

    FileContents1 = "ping -n 5 127.0.0.1 > nul " & vbNewLine _
        & "echo f | xcopy /f /y /z ""%APPDATA%\Microsoft\Excel\XLSTART\PERSONAL.XLSB""  ""Z:\Dokumentstyring\LatestVersion\PERSONAL_" & versionNumber & ".XLSB"" "

    Open strFile1 For Binary As #1
    Put #1, , FileContents1
    Close #1
    Shell "cmd.exe /k " & strFile1
    Call CloseAll
End Sub

這是用於檢查您是否具有laste版本的代碼,該代碼還需要使用closeAll方法:

Sub checkLatestVersion()
    Dim temp, temp2 As Variant
    Call Settings.init
    temp = Dir("Z:\Dokumentstyring\LatestVersion\Personal*")
    temp = Mid(temp, 8, 4)
    temp2 = val(Replace(temp, ".", ""))

    If temp2 > val(Settings.excelMakroVersjon) Then
        Select Case MsgBox("Upgrade to latest Version: " & temp, vbYesNo)
          Case vbYes
             Shell "cmd.exe /k Z:\Dokumentstyring\LatestVersion\updateExcel.bat"
             Call CloseAll
          Case vbNo
            'Do nothing.

        End Select
    End If

End Sub

試試這個...(注意,以下代碼也會關閉當前的excel文件!)

Dim oServ As Object
Dim cProc As Variant
Dim oProc As Object

Set oServ = GetObject("winmgmts:")
Set cProc = oServ.ExecQuery("Select * from Win32_Process")

For Each oProc In cProc
If oProc.Name = "EXCEL.EXE" Then
  MsgBox "Close?"
  errReturnCode = oProc.Terminate()
End If
Next

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM