首页 文章

catia vba Do until .saved和DoEvents,如何重新获得CATIA的控制权?

提问于
浏览
0

此代码允许我快速关闭,并在必要时使用键盘快捷方式保存文档 .

Sub CATMain()

Dim doc As Document
Set doc = CATIA.ActiveDocument

Dim MsgBoxRes As String

If doc.Saved Then
    doc.Close
Else
    MsgBoxRes = MsgBox("Document has changes, save & close?", vbOKCancel)
    If MsgBoxRes = "1" Then
        If Left(doc.FullName, 1) <> "Y" Then
            CATIA.StartCommand "save as"
            doc.Close
        Else
            doc.Save
            doc.Close
        End If
    Else
        Exit Sub
    End If
End If

End Sub

当我使用“另存为”对话框并保存我的文档时,代码 does not always 正确恢复或跳过下一个命令,我'm not sure what the case is here. (Practically this means my document won't关闭)
因此我尝试插入一个睡眠时间,这没有改变任何东西所以我也尝试使用DoEvents方法添加Do Until循环,这完全打破了宏 . 在这里看到我修复问题的失败尝试:

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub CATMain()

Dim doc As Document
Set doc = CATIA.ActiveDocument

Dim MsgBoxRes As String

If doc.Saved Then
    doc.Close
Else
    MsgBoxRes = MsgBox("Document has changes, save & close?", vbOKCancel)
    If MsgBoxRes = "1" Then
        If Left(doc.FullName, 1) <> "Y" Then
            CATIA.StartCommand "save as"
            DoEvents
            Do Until doc.Saved
                DoEvents
                If doc.Saved Then doc.Close
                DoEvents
                Sleep 100
            Loop
        Else
            doc.Save
            doc.Close
        End If
    Else
        Exit Sub
    End If
End If

End Sub

我的问题现在变得更糟;而在Do循环中VB将不允许CATIA或用户做任何事情,我需要使用Ctrl Break来退出循环 .

有没有办法让这段代码仍在使用catia.start命令“另存为”?

我知道应该可以通过创建自己的对话框来完成同样的事情,用户浏览并选择正确的保存位置,但如果可能的话我更愿意使用catia.start命令 .

1 回答

  • 0

    经过一段时间的测试和实验,我终于找到了一种方法来使用startcommand“另存为”方法,让我的代码表现一致 .

    问题中发布的代码的第一部分仅偶尔表现不正常,但即使用户在“另存为”对话框中选择"cancel",此代码也将关闭文档,完全丢弃自上次保存以来对文档所做的任何工作 . 这当然是不可接受的行为 .
    因此,必须在save as命令之后执行.saved检查,如下所示:

    Sub CATMain()
    
    Dim doc As Document
    Dim MsgBoxRes As String
    
    Set doc = CATIA.ActiveDocument
    
    If doc.Saved Then
        doc.Close
    Else
        MsgBoxRes = MsgBox("Document has changes, save & close?", vbOKCancel)
        If MsgBoxRes = "1" Then
            If Left(doc.FullName, 1) <> "Y" Then
                CATIA.StartCommand "save as"
                If doc.Saved Then doc.Close
            Else
                doc.Save
                doc.Close
            End If
        End If
    End If
    
    End Sub
    

    然而,当运行此代码时,宏跳过代码的原始问题/完全停止(仍不确定这里是什么情况)现在将100%发生 .
    我终于找到了一个简单的解决方法,似乎在100%的时间内表现得像预期的那样; save as命令之后的一个简单的.activate似乎解决了所有问题,如下所示:

    Sub CATMain()
    
    Dim doc As Document
    Dim MsgBoxRes As String
    
    Set doc = CATIA.ActiveDocument
    
    If doc.Saved Then
        doc.Close
    Else
        MsgBoxRes = MsgBox("Document has changes, save & close?", vbOKCancel)
        If MsgBoxRes = "1" Then
            If Left(doc.FullName, 1) <> "Y" Then
                CATIA.StartCommand "save as"
                doc.Activate
                If doc.Saved Then doc.Close
            Else
                doc.Save
                doc.Close
            End If
        End If
    End If
    
    End Sub
    

相关问题