Inventor crashes after oDoc.Close (VBA)

Inventor crashes after oDoc.Close (VBA)

dominiek_vanwest
Advocate Advocate
773 Views
5 Replies
Message 1 of 6

Inventor crashes after oDoc.Close (VBA)

dominiek_vanwest
Advocate
Advocate

Hi,

 

I have the following VBA code (to Update, Save, CheckIn and Close the file):

Public Sub UpdateSaveCheckinClose()
    Dim oDoc As Document
    Set oDoc = ThisApplication.ActiveDocument
    oDoc.Update
    oDoc.Save2 (True)
    
    Dim oCtrlDef As ControlDefinition
    Set oCtrlDef = ThisApplication.CommandManager.ControlDefinitions("VaultCheckinTop")
    oCtrlDef.Execute
    
    oDoc.Close
End Sub

But Inventor crashes right after oDoc.Close. 

 

However, if I add the following after oDoc.close, I have no problem.

MsgBox ("Document closed")

 

Why does Inventor crashes without the MsgBox?

And how can I solve this problem?

 

Thanks in advance,

Dominiek

0 Likes
Accepted solutions (1)
774 Views
5 Replies
Replies (5)
Message 2 of 6

rogmitch
Advocate
Advocate
Accepted solution

It is difficult to diagnose a crash but it could be a timing issue.  You could try adding a  DoEvents statment after the Execute or/and Close commands.  Occasionally I have seen this help in my code.

 

Roger Mitchell

0 Likes
Message 3 of 6

dominiek_vanwest
Advocate
Advocate

@rogmitch wrote:

It is difficult to diagnose a crash but it could be a timing issue.  You could try adding a  DoEvents statment after the Execute or/and Close commands.  Occasionally I have seen this help in my code.

 

Roger Mitchell


And how would that look exactly? I haven't worked with the DoEvents statement yet.

0 Likes
Message 4 of 6

rogmitch
Advocate
Advocate

Just as an example from my code:

 

Set oProjectionEntity = osketch.AddByProjectingEntity(oPartCompDef.WorkAxes.Item(3)) 'Z axis
oProjectionEntity.Construction = True
DoEvents

more code...

 

0 Likes
Message 5 of 6

dominiek_vanwest
Advocate
Advocate

I put the DoEvents before oDoc.Close and it works!

I haven't got a crash anymore.

Thanks!

 

What does the DoEvents statement actually do?

0 Likes
Message 6 of 6

rogmitch
Advocate
Advocate

Essentially it forces all Window messages that are queued to be processed while the current thread is suspended.  

 

This means that any process that is necessary to ensure the correct behaviour of the current thread willl now be finished and hence the current thread can continue to completion without crashing.

 

 Roger Mitchell

0 Likes