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

时间:2017-12-20 09:04:54

标签: excel vba excel-vba word-vba

我试图从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秒)来确定它处于编辑模式......

1 个答案:

答案 0 :(得分: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