Microsoft Visual C++ Runtime Error

Hi,
I am using Solidworks 2007 Sp-1.0 software in my Windows 2003server. Frequently i am facing an runtime exception while closingthe open documents. We have developed an consle application inVisual Studio 2005 which will automate the SolidWorks applicationthrough the Solidworks API.

After creating a TIF image for the opened drawing, we are trying toclose the drawing by using the CloseDoc method available in theISldWorks class. Following is the code what we have developed. Ihave included try catch block within the statements which causesthis exception. After performing the appropriate steps, whileclosing the opened solid works document, this exception is thrown.After an user interaction (Click on OK button) the exception iscaught by the try catch block.

I am not sure what is causing this exception. PFA code snippet foryour reference.

void SWUtil:ocUnload(void)
{
// make sure the solidworks session was passed in
try
{
VerifySW();
}
catch(CString csError)
{
CString csMsg;
csMsg.Format("DocUnload: %s",csError);
throw csMsg;
}

try
{
// Now unload each of the loaded documents.
m_Model = pm_swApp->GetActiveDoc();


while ( m_Model )
{
try
{

CString csName = m_Model.GetTitle();
if ( csName.GetLength() > 0 )
{
lfLog.DebugMsgStart(csName);
pm_swApp->CloseDoc( csName );
m_csModelFileName.Empty();
}
}
catch(...)
{
CString csError;
csError.Format("DocUnload: Could not close the (%s)",m_csModelFileName);
throw csError;
}



try
{
if ( m_Model)
{
m_Model.ReleaseDispatch();
m_Model = 0;
}
}
catch(...)
{
CString csError;
csError.Format("DocUnload: Could not release the model dispatch formodel (%s)", m_csModelFileName);
throw csError;
}

// Get next active document..if any.
m_Model = pm_swApp->GetActiveDoc();
}

}
catch(...)
{
CString csError;
csError.Format("DocUnload: Error while closing the m_Model");
throw csError;
}


//
// Force back to invisible in case it was made visible by a BOMcheck.
//
pm_swApp->SetUserControl(FALSE);
pm_swApp->SetVisible(FALSE);
}


We are using the IModelDoc2->Close() option to close thedocument. Following is the exception thrown.

Runtime Error!

Program

This application has requested the runtime to terminate it in anunusual way. Please contact the applications support team for moreinformation.


Can any one please help me on this. Your help is greatlyappreciated !
SolidworksApi macros