SolidWorks Shuts-Down unexpectedly when I just want to close the ModelDoc.

I have used this same code below, at the end of all of my sub-routines to close out a part or assembly file and now when I use it, it is closing down SolidWorks.  Any idea's what is causing this?

    swModel.EditRebuild3

    swModel.ViewZoomtofit2

    swModel.SaveSilent

    swApp.CloseDoc swModel.GetPathName  'This is the line in which SolidWorks closes, I just want the modeldoc to close, not the program.

    Set swModel = Nothing

I don't get any error message during run-time, or when I step-through.  SolidWorks just closes unexpectedly.  Here is the code for the entire sub routine:

Private Sub Test1()
                         
Sleep 3000

    Dim swModel                   As SldWorks.ModelDoc2
    Dim strNewPathName      As String
    Dim runMacroError          As Long
    Const strOldFileName     As String = "305701"
    Const strFileExt             As String = "sldprt"
   
    strNewPathName = GetNewPathName(strOldFileName, strFileExt)
   
    'Open the model
    If strFileExt = "sldprt" Then
        Set swModel = swApp.OpenDoc6(strNewPathName, swDocPART, swOpenDocOptions_Silent, "", 0, 0)
    Else
        Set swModel = swApp.OpenDoc6(strNewPathName, swDocASSEMBLY, swOpenDocOptions_Silent, "", 0, 0)
    End If
   
    On Error GoTo ErrorHandler
  
    Custprop = swModel.DeleteCustomInfo2("Default", "ShopOrder")
    Custprop = swModel.DeleteCustomInfo2("Default", "Drawn")
    Custprop = swModel.DeleteCustomInfo2("Default", "Checked")
    Custprop = swModel.DeleteCustomInfo2("Default", "Date")

   
    Custprop = swModel.AddCustomInfo3("Default", "ShopOrder", swCustomInfoText, strJobnumber)
    Custprop = swModel.AddCustomInfo3("Default", "Drawn", swCustomInfoText, strDrawn)
    Custprop = swModel.AddCustomInfo3("Default", "Checked", swCustomInfoText, strChecked)
    Custprop = swModel.AddCustomInfo3("Default", "Date", swCustomInfoText, strDate)

  
swApp.RunMacro2 "C:\\MY DOC'S\\Disfunctional Macro.swp", "Airside_Extended_Tubesheets", "Main", swRunMacroDefault, runMacroError
  
    'Rebuild, Save and close the model
    swModel.EditRebuild3
    swModel.ViewZoomtofit2
    swModel.SaveSilent
    swApp.CloseDoc swModel.GetPathName 'This is the line in which SolidWorks closes, I just want the modeldoc to close, not the program.
    Set swModel = Nothing
Exit Sub

ErrorHandler:
    MsgBox "Wrong ItemID Try Again 701!"

End Sub

SolidworksApi macros
Answers 7
KR
KR 2012-06-01 Best Answer

Shawn,

I have always used ISldWorks::CloseDoc, which requires you to specify just the name of the document (e.g., part.SLDPRT), which you can extract from IModelDoc2::GetPathName.  Try that and see if it helps.

Keith

Video Tutorials for the SolidWorks API

ST
ST 2012-06-01

I thought that this line of code does that?

swApp.CloseDoc swModel.GetPathName

KR
KR 2012-06-01

Sorry I wasn't clear.  Did you try using just the model name rather than the entire path name?

ST
ST 2012-06-01

Tried that, and no difference.  It acts like it was commanded to shut-down.  No errors or anything.

KR
KR 2012-06-01

Does ISldWorks::CloseAll also cause it to crash?  Does ISldWorks::CloseDoc crash with any doc or just one in particular?  Does it matter what type of document it is?  Is it a document from an older version?

I would recommend you use IModelDoc2::Save3 and specify a silent save in the Options rather than using the obsolete SilentSave.  Try this and see if makes any difference.

Keith

Video Tutorials for the SolidWorks API

JS
JS 2012-06-01

The API documentation states that you can pass an empty string to close the active document.  Have you tried that?

Also from the API docs:

---------

If you are closing the only document open in the SolidWorks session and this SolidWorks session is a background session, then calling this method, or calling ISldWorks::QuitDoc, results in the SolidWorks session being closed.

A background SolidWorks session can be created when the SolidWorks session is started by your application and the session is not controlled by the user; i.e., ISldWorks::UserControl set to false.

To keep the SolidWorks session open, set ISldWorks::UserControl to True, which makes the session visible, or you can close your documents at the end of your program execution.

----------

Any chance you are sending SW to the background elsewhere in your code?

Jim S.

ST
ST 2012-06-04

Hey Keith,

I switched this line, swApp.CloseDoc swModel.GetPathName, for, swApp.CloseAllDocuments (False), and now it works.  The SaveSilent seems to be okay for the time being, so I am going to leave it untouched.  But if I come across any more issues with this, I will switch it to the Save3 function like you suggested.

Thanks for the help!

Shawn