gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 20:32:28 24 4
gpt4 key购买 nike

我试图从 word 中找出 excel 是否处于编辑模式,我查看了这个 thread并尝试修改它,但如果您在编辑模式下有 excel,则它不起作用,然后运行它,然后退出编辑模式重新运行它仍然说它是编辑模式:

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 秒)才能确定它处于编辑模式......

最佳答案

这是一个工作代码:

'**********************************************************************
'********* 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

关于vba - 从 word-vba 中找出 excel 是否处于编辑模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47902259/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com