从word-vba中查找excel是否处于编辑模式

问题描述 投票:2回答:1

我试图从word中找出excel是否处于编辑模式,我看着这个thread并尝试修改它,但是如果你在编辑模式下有优点,然后运行它,然后退出编辑模式重新运行它仍然无效说它是编辑模式:

enter image description here

   '*********************************************************
'********* define if we need to close excel after sub is done
'***********************************************************
Public Function setExcelObject(ByRef oXLApp As Object) As Boolean
    On Error Resume Next

    Set oXLApp = GetObject(, "Excel.Application")
    If oXLApp Is Nothing Then
        Set oXLApp = CreateObject("Excel.Application")
    End If

    setExcelObject = IsInEditMode(oXLApp)

End Function


Public Function IsInEditMode(ByRef exapp As Object) As Boolean
        If exapp.Interactive = False Then
            IsInEditMode = False
        Else
            On Error GoTo terminate
                exapp.Interactive = False
                exapp.Interactive = True

                IsInEditMode = False

        End If
        Exit Function

terminate:
         IsInEditMode = True
         Exit Function

    End Function

注意:它也需要花费很长时间(15秒)来确定它处于编辑模式...

excel vba excel-vba word-vba
1个回答
1
投票

这是一个有效的代码:

'**********************************************************************
'********* See if we can open excel, true is Yes we can work with excel
'**********************************************************************
Public Function setExcelObject(ByRef oXLApp As Object) As Boolean
    On Error Resume Next

    Set oXLApp = GetObject(, "Excel.Application")
    If oXLApp Is Nothing Then
        Set oXLApp = CreateObject("Excel.Application")
    End If

    setExcelObject = Not IsInEditMode(oXLApp)

    If setExcelObject = False Then Set oXLApp = Nothing

End Function

' *****************************************************************
' **************** Check if excel is in edit mode ****************
'*****************************************************************
Public Function IsInEditMode(ByRef exapp As Object) As Boolean
            On Error GoTo terminate
                exapp.Interactive = False
                exapp.Interactive = True

                IsInEditMode = False

        Exit Function
terminate:
         IsInEditMode = True
         Exit Function

    End Function

' *************************************************************
' *************** Check if excel is open, true, means we should not close excel after we are done.....
'*****************************************************************
Function ExcelOpen() As Boolean
    ExcelOpen = FindWindow("XLMAIN", vbNullString)
End Function

上面的代码然后从几个程序调用,如下所示:

' Get excel object
If Not FileHandling.setExcelObject(oXLApp) Then
    failMessage = "You are editing a cell in excel, stop doing that!"
    GoTo terminate
End If

' check if we need to close after
closeExcelMy = FileHandling.ExcelOpen

'See if we can open workbook
If Not FileHandling.GetWorkbook(wbName, oXLApp, xlApp) Then
    failMessage = "Failed to open workbook"
    GoTo terminate
End If

oXLApp.Visible = True
© www.soinside.com 2019 - 2024. All rights reserved.