Search code examples
vbacatia

catia vba Do Until .saved and DoEvents, how to regain control of CATIA?


This code allows me to quickly close and, if necessary, save documents with a keyboard shortcut.

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

When I use the Save As dialog box and save my document, the code does not always properly resume or it skips the next command(s), I'm not sure what the case is here. (Practically this means my document won't close)
Therefore I tried inserting a sleep time, which didn't change anything so I also tried adding a Do Until loop with a DoEvents method and this completely broke the macro. See here my failed attempt at fixing the problem:

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

My problem now becomes much worse; while in the Do loop VB won't allow CATIA or the user to do anything and I need to use Ctrl+Break to get out of the loop.

Is there a way to make this code work while still using catia.startcommand "save as" ?

I know it should be possible to accomplish the same thing by making my own dialog box where the user browses to and selects the correct save location, but if possible I'd much rather just use the catia.startcommand.


Solution

  • After some time testing and experimenting I've finally found a way to use the startcommand "save as" method and have my code behave consistently.

    The very first section of code posted in the question behaves erratically only occasionally, however this code will close a document even if the user selects "cancel" in the save as dialog box, completely discarding any work done on the document since the last time it was saved. This is of course unacceptable behavior.
    Therefore a .saved check must be implemented after the save as command, like so:

    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
    

    When running this code however, the original problem of the macro skipping code/completely stopping (still not sure what the case is here) will now happen 100% of the time.
    I have finally found a simple workaround that seems to behave as intended 100% of the time; a simple .activate after the save as command seems to solve all problems, like so:

    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