簡體   English   中英

Excel應用程序未從MS-Project VBA關閉

[英]Excel Application not closing from MS-Project VBA

以下子項未關閉Excel應用程序。 它保留在任務管理器中。 有點奇怪,因為我使用相同的方法打開和關閉其他模塊中的工作簿,並且可以正常工作。 此代碼在MS-Project中。

Sub updateModules()

    'TESTE INICIAL PARA SABER SE AS INFORMAÇÕES BÁSICAS ESTÃO INSERIDAS
    If sanity_test = 0 Then
        Exit Sub
    End If
    '--------------------------------//--------------------------------

    Dim xlapp As Object
    Dim xlbook As Object
    Dim sHostName As String

    ' Get Host Name / Get Computer Name
    sHostName = Environ$("username")

    Set xlapp = CreateObject("Excel.Application")
    'xlapp.Visible = True
    Set xlbook = xlapp.Workbooks.Open(modulesVBA_loc)

    'ENCONTRAR CÓDIGO NA TABELA DO FICHEIRO MASTER
    Dim rng_modules As Range
    Dim rng_usernames As Range
    Dim username As Range
    Dim atualizado As Range
    Dim lastcol As Long


    With xlbook.Worksheets(1)
        'Última coluna
        lastcol = .Cells(2, .Columns.Count).End(xlToLeft).Column
        lastcol_letter = Functions_mod.GetColumnLetter(lastcol)
    End With

    'Range com os usernames
    Set rng_usernames = xlbook.Worksheets(1).Range("E2:" & lastcol_letter & "2")
    'Encontra o username correto
    Set username = rng_usernames.Find(sHostName)

    Set rng_modules = xlbook.Worksheets(1).Range("A3")  'Primeiro módulo
    Do While rng_modules.Value <> Empty
        linha = rng_modules.Row
        Set atualizado = username.Offset(linha - 2)
        If atualizado.Value = "Not Updated" Then
            With ThisProject.VBProject
                .VBComponents.Remove .VBComponents("CoreTeam_mod")
                .VBComponents.Import supportDoc_loc & "Project\Próxima Actualização - Apenas PP pode modificar\VBA\Modules\CoreTeam_mod.bas"
            End With
            atualizado.Value = "Updated"
        End If
        Set rng_modules = rng_modules.Offset(1)
    Loop

    xlbook.Saved = True
    xlbook.Close

End Sub

編輯 :似乎錯誤是來自獲取列字母的函數。 我已經用字母“ G”替換了lastcol_letter,並且代碼運行正常並正確關閉了Excel Application。 我應該如何編寫函數呢?

Function GetColumnLetter(colNum As Long) As String
    Dim vArr
    vArr = Split(Cells(1, colNum).Address(True, False), "$")
    GetColumnLetter = vArr(0)
End Function

要打開excel應用程序,您可以使用如下代碼:


Dim xlapp as Excel.application
Set xlapp = GetObject("", "Excel.Application")
' your other code goes here
xlapp.quit
End sub

最后寫Application.Quit來關閉實例。

您的函數GetColumnLetter (在MS Project中)使用Excel Cells對象而不引用父對象(例如工作表對象)。 當該代碼在Excel中本機運行時,Excel隱式使用活動工作表。 但是,MS Project不會使用不合格的Excel引用執行此操作。

獲取所需Range對象的更好方法是:

Dim rng_usernames As Range
Dim lastcell As Range

    With xlbook.Worksheets(1)
        'Última coluna
        Set lastcell = .Cells(2, .Columns.Count).End(xlToLeft)
        'Range com os usernames
        Set rng_usernames = .Range("E2", lastcell)
    End With

End Sub

如果宏完成后Excel仍在運行,請顯式關閉宏並將Excel對象設置為宏末尾的Nothing。

' close out
xlbook.Close SaveChanges:=True
xlapp.Quit 
Set xlbook = Nothing
Set xlapp = Nothing

注意:“ 保存工作簿”屬性指示是否已保存文件。 將其設置為True將意味着關閉文件時不提示您保存更改,並且不會保存更改。 您的代碼對文件進行了更改,這些更改看起來確實是您想要保存的。 考慮使用Workbook Close方法的SaveChanges參數來顯式保存更改。

暫無
暫無

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

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