CATIA VBA 二次开发(四)定制【关闭窗口】工具条 - 详解
2025-09-15 17:44 tlnshuju 阅读(4) 评论(0) 收藏 举报1.目标
制作【关闭窗口】工具条;
工具条下三个命令,分别为:
【关闭所有窗口】:关闭所有窗口;
【关闭其他窗口】:关闭除当前窗口外的所有窗口;
【关闭当前窗口】:关闭当前窗口;
2.实现
创建宏项目、新建工具条等此处不再赘述,参见上一条文章;
2.1关闭当前窗口
完整代码:
Sub CATMain()
' 错误处理:发生错误执行下一条语句
On Error Resume Next
' 获得当前文档
Dim oDoc As Document
Set oDoc = CATIA.ActiveDocument
' 如果当前没有打开的文档,弹出消息对话框
If oDoc Is Nothing Then
MsgBox "当前没有打开的文档!", vbInformation
End If
'关闭当前文档
oDoc.Close
End Sub通过ActiveDocument获得当前文档,再使用Close方法关闭文档;
添加错误处理,当获取当前文档失败时弹出对话框提示。
2.2关闭其他窗口
完整代码:
Sub CATMain()
On Error Resume Next
Dim oDoc As Document
Set oDoc = CATIA.ActiveDocument
If oDoc Is Nothing Then
MsgBox "当前没有打开的文档!", vbInformation
End If
' 获取当前文档Name
oDocName = oDoc.Name
' 遍历打开的所有文档
Dim doc As Document
For Each doc In CATIA.Documents
'如果遍历的文档Name不是当前文档Name就关闭文档
If doc.Name <> oDocName Then
doc.Close
End If
Next
End Sub2.3关闭所有窗口
完整代码:
Sub CATMain()
On Error Resume Next
Dim oDoc As Document
Set oDoc = CATIA.ActiveDocument
If oDoc Is Nothing Then
MsgBox "当前没有打开的文档!", vbInformation
End If
For Each doc In CATIA.Documents
doc.Close
Next
End Sub3.特别注意
文档编辑后未保存使用上述命令关闭窗口时,不会弹出是否要保存的对话框,请谨慎使用。

 
                    
                     
                    
                 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号