Problem opening an asm driven by a design table

Hellow !

I'm trying to open an asm in VBA (from an external excel file).

This asm is driven by a design table to put some cinderblock on a plank.

The plank is driven by a design table to set its dimensions.

The cindeblocks are driven by design table to set their dimensions.

When I try to open the asm, solidworks automatically :

- opens the asm, opens its design table, closes its design table.

- opens the plank, opens its design table, closes its design table, updates the plank.

- opens the cinderblocks, opens their design table, closes their design table, updates the cindeblocks.

- update the asm.

The problem is : Because of opening all these excel file while a macro is running in another one, 7 out of 10 times it crashes.

Here is the code :

<p>Private Sub CommandButton1_Click()</p><p><br>Dim longstatus As Long<br>Dim longwarnings As Long<br>Dim swApp As SldWorks.SldWorks<br>Dim swPart1 As SldWorks.ModelDoc2<br>Dim swPart2 As SldWorks.ModelDoc2<br>Dim swPart3 As SldWorks.ModelDoc2</p><p></p><p>On Error Resume Next</p><p></p><p>Set swApp = GetObject(, "SldWorks.Application")</p><p><br>If Err.Number = 0 Then<br>Else<br>    If MsgBox("Veuillez lancer Solidworks avant d'ouvrir la MEP.", 0 + 4096 + 48, "Attention") = True Then<br>    End If<br>    Exit Sub<br>End If</p><p></p><p>Set swPart1 = swApp.OpenDoc6(PathCinderblock, 1, 0, "", longstatus, longwarnings)<br>DoEvents<br>Set swPart2 = swApp.OpenDoc6(PathPlank,1, 0, "", longstatus, longwarnings)<br>DoEvents<br>Set swPart3 = swApp.OpenDoc6(PathASM, 2, 0, "", longstatus, longwarnings)<br>DoEvents<br>End Sub</p>

At the beginning, I tried directly opening the ASM, and opening solidworks with Set swApp = New SldWorks.SldWorks, it worked sometimes, but it seemed to crash even more frequently.

So I tried how it is above. But again, it crashes frequently.

Does someone have a solution to this problem ?

Thank you,

Simon

PS : Don't mind my English, I'm not very good at it.

SolidworksApi macros
Answers 5
PB
PB 2015-09-30 Best Answer

What lines cause the design table to be opened and closed? I use the following code to test for the existence of a design table, it opens and closes it immediately.

<p>    Set swDesTable = swModel.GetDesignTable</p><p>    bRet = swDesTable.Attach</p><p>    If bRet Then</p><p>       swDesTable.Detach</p><p>     end if </p>

According to https://support.microsoft.com/en-en/kb/118468 the command "doevent" causes the program to continue instead of wait. I think it will run more smoothly if those lines are removed, although it may take longer.

SF
SF 2015-10-01

Thanks Peter,

<p>Peter Brinkhuis:</p><p></p><p>What lines cause the design table to be opened and closed?</p>

No one. It's solidworks which, at the opening of the file, opens the various design table and closes it to check if there is update to do on the part.

Actually, this action don't mind me. I want Solidworks updating part at their opening. But I was wondering if it has a way to do it hidden.

Maybe what I can try is : Disable the automatic opening of the design table at the opening of the part or asm. Open the 3 elements, and when they are opened, updating them (open/close their design table) one by one.

But I don't really know what is the VBA code for this.

<p>Peter Brinkhuis:</p><p></p><p>According to <a class="jive-link-external-small" href="/external-link.jspa?url=https://support.microsoft.com/en-en/kb/118468" rel="nofollow" target="_blank" data-community-id="1">https://support.microsoft.com/en-en/kb/118468</a> the command "doevent" causes the program to continue instead of wait. I think it will run more smoothly if those lines are removed, although it may take longer.</p>

Oh. I knew I hadn't totally understund how this command was running. I just tried it before posting here. It don't matter if I write it or not, crashes are still there.:x

PB
PB 2015-10-01

I thought maybe the OpenDoc6 command was outdated, but it's the most recent version. Some more info here. There is no mention at all about design tables, but the following lines may help:

<p><span style="color: #000000; font-family: Arial, Helvetica, sans-serif; font-size: 13px;">You may want to set the current working directory before calling ISldWorks::OpenDoc6. This can be done using the </span><a href="http://help.solidworks.com/2012/English/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.ISldWorks~SetCurrentWorkingDirectory.html" style="color: #0f6cb6; font-family: Arial, Helvetica, sans-serif; font-size: 13px;" data-community-id="1">ISldWorks::SetCurrentWorkingDirectory</a><span style="color: #000000; font-family: Arial, Helvetica, sans-serif; font-size: 13px;"> method. </span></p>
SF
SF 2015-10-05

Thank you Peter,

But unfortunatey, it didn't solve the problem. The crash occurs exactly when solidworks try to open the first design table.

<p>Sub lancer_solidworks()</p><p>Dim swApp As SldWorks.SldWorks<br>Dim Part As SldWorks.ModelDoc2</p><p>Set swApp = New SldWorks.SldWorks<br>swApp.Visible = True<br>swApp.SetCurrentWorkingDirectory ("My path")</p><p>Part = swApp.OpenDoc6("My Path\My file.SLDDRW", 3, 0, 0, 0, 0)</p><p>End Sub</p>
SF
SF 2015-10-06

Ok, after a few test I know exactly what happens :

      If a macro is running (in excel) while solidworks checks the design table : excel crashes. Even if it's just a Sleep.

The problem is : My macro open solidworks, open a drawing (drawing of the asm and the parts), and interact with it.

I know I can open soldiworks and the drawing thanks to a macro, then interact with it thanks to another, but I would like both being done in just one.

EDIT : It's a bit more complicated actually... or maybe not :

     The excel file is my interface for setting the three design table. It has vaious sub.

     When I just open the excel file and open solidworks (thanks to a macro), excel doesn't crash.

     When I open the excel file, I run some more sub (without running sub which edit the design table), and I open solidworks (always with the same macro) excel crashes.

I don't know much about memory process, but it seems that the problem is link to this.

Does anyone know about it ?

Thank you,

EDIT 2 : Ok, with excel in safe mode, and my interface already filled (so, I havn't run any macro before running the one which open solidworks and intreact with the drawing), it hadn't crash.

So there is no problem in my code, which is a very good news !

But it really seems to be a memory problem (all my pointers already are set to nothing, I don't know what I can do more )