How do you close the file in a FileNewNotify2() event?

Jul 12, 2004 4 Replies

Hi,



I'm fairly new to the SolidWorks API and am having a heck or a time with it.



The problem I am currently having, is when a user creates a new file, a dialog box opens for user interaction. But if the user cancels, I need to close the newly created file.



I thought the FileNewNotify2 event was the way to go, but for the life of me I cannot close that file! and to be honest, I don't find the API Documentation all that helpful.



This has got to be something simple, can somebody provide me with a clue?



Thanks in advance, John


----------------------------------------------------------------------------



--------------



---- CancelTest.cls -- CancelTest.cls -- CancelTest.cls -- CancelTest.cls -- CancelTest.cls --



----------------------------------------------------------------------------



-------------- Option Explicit



Implements SWPublished.SwAddin Private WithEvents m_oSldWrks As SldWorks.SldWorks



Private Function m_oSldWrks_FileNewNotify2(ByVal newDoc As Object, _ ByVal DocType As Long, _ ByVal templateName As String) As Long '// open a dlg box



'// if dlg box was cancelled, then close new file Dim sTitle As String sTitle = newDoc.GetTitle() '// newDoc is assumed to be a SldWorks.PartDoc Debug.Print sTitle m_oSldWrks.QuitDoc sTitle '// doesn't work m_oSldWrks.CloseDoc sTitle '// doesn't work



End Function



Private Function SwAddin_ConnectToSW(ByVal ThisSW As Object, _ ByVal Cookie As Long) As Boolean '// set data member object Set m_oSldWrks = ThisSW End Function



Private Function SwAddin_DisconnectFromSW() As Boolean '// clear data member object Set m_oSldWrks = Nothing End Function


Modify this code for your userform

Dim swApp as Sldworks.Sldworks Dim NewModelDoc as Sldworks.Modeldoc2

Private Sub CommandButtonCancel_Click() swapp.CloseDoc (NewModelDoc.GetTitle) End End Sub

Private Sub YourUserFormName_Initialize() Set swApp = Application.SolidWorks Set NewModelDoc = swApp.ActiveDoc End Sub

Then when FileNewNotify2 envokes your UserForm the user form will grab the New Document's Object for you to manipulate.

Regards, Corey Scheich

CancelTest.cls --

Hi Corey,

I tried what you suggested and it worked fine in a SolidWorks Macro.

However, I am building a Visual Basic COM DLL and although I modified my old code (the FileNewNotify2 event specifically) to use the ActiveDoc as you suggested, BUT it still did not work.

Private Function m_oSldWrks_FileNewNotify2(ByVal newDoc As Object, _ ByVal DocType As Long, _ ByVal templateName As String) As Long Dim oDoc As SldWorks.ModelDoc2 Set oDoc = m_oSldWrks.ActiveDoc m_oSldWrks.CloseDoc oDoc.GetTitle() '// doesn't work m_oSldWrks.QuitDoc oDoc.GetTitle() '// doesn't work End Function

Does anybody know why Corey's code would work in a Macro, but the same thing will not work from a VB COM DLL?

Thanks, John

John I think you are going to have to Load and show a user form and cancel using the userform. I can't get it to work Unless I use a userform. You have to envoke the form for your user to be able to cancel anyway.

Private Function m_oSldWrks_FileNewNotify2(ByVal newDoc As Object, _ ByVal DocType As Long, _ ByVal templateName As String) As Long

Load Userform1 Userform1.Show True End Function

then on the user form

Private Sub CommandButton1_Click() ThisLibrary.m_oSldWrks.CloseDoc (ThisLibrary.m_oSldWrks.ActiveDoc.GetTitle) End Sub

Corey

well, a couple of questions before i give a couple of **guesses***

1) was your VB code also done from a form module? A form module is a (type of) class module, and only class modules can recieve events. 2) was your VB form one that was imported from the VBA form? If so, check the syntax: the default control naming, and even some of the events, differ from VBA, to VB....

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required