2

Access 2010の VBA オートメーションを使用して、 Excel 2010を使用してグラフを作成しています。私のコードは正常に動作しますが、孤立した Excel.exe プロセスが実行されたままになります。別のモジュールでの Excel VBA 関数の呼び出しに問題があることを突き止めました。この関数の呼び出しを削除すると、コードの終了時に Excel プロセスが閉じられます。この関数は、範囲を として宣言し、メソッドを として明示的に参照するFindAllなど、Access で実行できるように変更を加えたChip Pearson のものです。孤立した Excel プロセスが実行されたままにならないように、この関数をどのように変更する必要がありますか?Excel.RangeUnionExcel.Application.Union

次のコードは、メイン プロシージャで Excel アプリケーションをバインドします。

Dim oApp As New excel.Application
Dim oBook As excel.Workbook
Dim oSheet As excel.Worksheet

Set oBook = oApp.Workbooks.Add
Set oSheet = oBook.Worksheets(1)

次に、最後に:

oBook.Close False
oApp.Quit

Set oSheet = Nothing
Set oBook = Nothing
Set oApp = Nothing

そして、次の行を使用して FindAll を呼び出しています。

      b = FindAll(oSheet.Range("a2", oSheet.Range("a2").End(xlDown)), strWellID).Rows.Count

FindAll機能は以下です。

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' modFindAll
' By Chip Pearson, chip@cpearson.com. www.cpearson.com
' 24-October-2007
' This module is described at www.cpearson.com/Excel/FindAll.aspx
' Requires Excel 2000 or later.
'
' This module contains two functions, FindAll and FindAllOnWorksheets that are use
' to find values on a worksheet or multiple worksheets.
'
' FindAll searches a range and returns a range containing the cells in which the
'   searched for text was found. If the string was not found, it returns Nothing.

' FindAllOnWorksheets searches the same range on one or more workshets. It return
'   an array of ranges, each of which is the range on that worksheet in which the
'   value was found. If the value was not found on a worksheet, that worksheet's
'   element in the returned array will be Nothing.
'
' In both functions, the parameters that control the search have the same meaning
' and effect as they do in the Range.Find method.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Function FindAll(SearchRange As Excel.Range, _
                FindWhat As Variant, _
                Optional LookIn As XlFindLookIn = xlValues, _
                Optional LookAt As XlLookAt = xlWhole, _
                Optional SearchOrder As XlSearchOrder = xlByRows, _
                Optional MatchCase As Boolean = False) As Range
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' FindAll
    ' This searches the range specified by SearchRange and returns a Range object
    ' that contains all the cells in which FindWhat was found. The search parameters to
    ' this function have the same meaning and effect as they do with the
    ' Range.Find method. If the value was not found, the function return Nothing.
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    Dim FoundCell As Excel.Range
    Dim FirstFound As Excel.Range
    Dim LastCell As Excel.Range
    Dim ResultRange As Excel.Range

    With SearchRange
        Set LastCell = .Cells(.Cells.Count)
    End With

    'On Error Resume Next
    On Error GoTo 0

    Set FoundCell = SearchRange.Find(What:=FindWhat, _
            after:=LastCell, _
            LookIn:=LookIn, _
            LookAt:=LookAt, _
            SearchOrder:=SearchOrder, _
            MatchCase:=MatchCase)

    If Not FoundCell Is Nothing Then

        Set FirstFound = FoundCell
        Set ResultRange = FoundCell
        Set FoundCell = SearchRange.FindNext(after:=FoundCell)

        Do Until False ' Loop forever. We'll "Exit Do" when necessary.
            If (FoundCell Is Nothing) Then
                Exit Do
            End If

            If (FoundCell.Address = FirstFound.Address) Then
                Exit Do
            End If

            Set ResultRange = Excel.Application.Union(ResultRange, FoundCell)
            Set FoundCell = SearchRange.FindNext(after:=FoundCell)
        Loop
    End If

    Set FindAll = ResultRange

    'added below

    Set ResultRange = Nothing
    Set FoundCell = Nothing
    Set FirstFound = Nothing
    Set LastCell = Nothing
    Set SearchRange = Nothing
End Function
4

1 に答える 1

0

Jimmy Pena と Siddharth Rout は、ファントム Excel プロセスを削除する方法について優れたコメントを提供しています。

すべての場合に必ずしも推奨するわけではありませんが、それが永続的な問題であり、トラブルシューティング中に迅速な「修正」を探している場合は、この SO に記載されているように、ブルートフォースで閉じることができます。質問:それ自体を除く Excel のすべてのインスタンスを閉じる VBA スクリプト

Sub ForceExcelExit()

Dim BruteForce As String

BruteForce = "TASKKILL /F /IM excel.exe"
Shell BruteForce, vbHide

End Sub

テストにより、特にエレガントではないにしても、プロセスの迅速な終了が明らかになりました。

反復ループを使用して、このようなプロセスをもう少しエレガントな方法で終了することもできますが、アプリケーション/アプリケーション インターフェイス メソッドではなく、システム プロセス メソッドを使用します。差し込み印刷によって開始された Excel インスタンスを閉じる方法

エラー チェック、コードのカプセル化または一般的なコールバック、メソッドの使用、変数のインスタンス化、またはその他のプログラムの問題が原因でインスタンスをアクティブのままにしておくための包括的な解決策として、これらのメソッドのいずれかを推奨するわけではありませんが、修正が必要です。

于 2012-07-30T15:51:06.507 に答える