Macro SolidWorks via Excel

Hello 

I was looking for a macro to do the equivalent of the [Alt + TAB] action (change active document) and I found this code:

 

Sub main()

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim vModels As Variant
Dim count As Integer
Dim opendocs As New Collection
Dim docname As Variant
Dim currentdocTitle As String
Dim title As String

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
currentdocTitle = swModel.GetTitle
vModels = swApp.GetDocuments

For count = LBound(vModels) To UBound(vModels) 'get list of open documents

    Set swModel = vModels(count)
    If swModel.Visible <> False Then
        opendocs.Add (swModel.GetTitle)
    End If

Next

count = 0

If opendocs(opendocs.count) = currentdocTitle Then ' if its the last document in the list, switch to the first document

    swApp.ActivateDoc2 opendocs(1), False, 0
 
End

Else ' else find the next document and activate
 
    For count = 1 To opendocs.count
 
        If opendocs(count) = currentdocTitle Then
            swApp.ActivateDoc2 opendocs(count + 1), False, 0
            End
        End If
 
    Next
 
End If

End Sub

 

and it works perfectly when the launches this macro directly via SolidWorks.

My problem is that I am creating a tool that launches via Excel and therefore has to write all my macros in VBA Excel and not VBA Solidworks.

when I copy/paste this code into my Excel macro, here is the error message I get:

run-time error '438'
Object doesn't support this property or method

If I click on debug, it highlights this line for me

Set swApp = Application.SldWorks

 

I've tried several things but I don't have much of an idea anymore...

 

On previous macros I have this code:

Set swApp = CreateObject("SldWorks.application")

 

So I tried to do the same for this one but it doesn't work I get this error message:

Run-time error'13':
Tpe mismatch

pointing to this line:

swApp.ActivateDoc2 opendocs(1), False, 0

 

on VBA Excel I have the references SldWorks 2017 Type Library, SOLIDWORKS 2017 Commends type library and SOLIDWORKS 2017 Constant type library enabled.

 

Does anyone have any ideas?

Thanks in advance

Yves

Hello

Just "why" change the active document from excel?

Otherwise on SW it's [Ctrl] + [Tab]

Hi remrem,

Yes I know that it's [Ctrl]+[Tab] the keyboard command on solidworks but if I have to do this from Excel it's because I'm creating a  machine configuration tool that launches from Excel, so all solidworks macros are written in Excel

If you want to do your configuration macro on Excel. You must not do this under any circumstances!

You need to create an object for each open document. And then you have to select a document and perform your operations, then select another document and perform your operations, etc...

Otherwise if the user changes doc during the processing all your code collapses.

How do you open your documents in your macro?

1 Like

My macro in Excel consists of opening a solidworks file (an assembly composed of several parts) and then replacing all the parts that compose it, not parts whose names and paths have been selected in the excel file. For the moment my program is working very well.

For one of the parts, I have to open it before opening the assembly in order to modify 2/3 sides (I already have a side that works for that) and then save it under a new name. It's a bit of a simplification but at some point I open the plan and I have to switch back to the document part.

When replacing the parts, the macro will go to get this new part (no PB for that)

To open the room:

Set swApp = CreateObject("SldWorks.application")

Rep = ShellExecute(0, "open", CHEMIN & "\" & NOM, vbNullString, vbNullString, 5)
Set Part = swApp.ActiveDoc

 My room opens well as its plan (with the same method).

then I use the code I posted in my original question by replacing swApp with swApp2 since swApp is already used in what I just copied/pasted.

There is no chance that the user will change the doc during the processing of my code since all the steps are done automatically, after clicking on a single button.

 

I strongly advise you to read SW's API help.

To open a document, I advise you not to use the "ShellExecute" commands but to do it with the SW APIs like here: http://help.solidworks.com/2018/english/api/sldworksapi/Open_Assembly_Document_Example_VB.htm

You will be able to open several documents on the same process, modify them independently, close them...

It is not advisable to open several SW processes because it causes serious malfunctions, not to mention file management...

1 Like

Then 

I just made an essay doing exactly what is written on the page you sent.

I get the same error message:

run-time error '438'
Object doesn't support this property or method

pointing to the line:

Set swApp = Application.SldWorks

 

Unfortunately, I'm not very good at SolidWoprks macro and am therefore a bit lost with all the objects, processes, applications...

Hello

Try starting your macro as follows:

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2

If swApp Is Nothing Then
   Set swApp = CreateObject("SLDWORKS.application")
   swApp.Visible = True
Else
   Set swApp = Application.SldWorks
End If

Set swModel = swApp.ActiveDoc
    
If swModel Is Nothing Then
   MsgBox "Aucun document SW n'est ouvert"
Else
   MsgBox swModel.GetTitle
End If

 

This allows you to either create a Solidworks instance or to attach to the existing one if a Solidworks is already open and then check if a document exists.

You need at least the SldWorks 2017 reference type Library.

Kind regards

1 Like

Hello

Sorry for my response time but I had to switch to other projects.

I think I can see a little more clearly now, I can manage my open SolidWorks application... My problem now is that, in my sequence of actions that I want the macro to do, I need to open a room, its drawing, go back to the room, save it under a new name, close the room, save the drawing under the same name as the new room and then close the drawing.

I have to do all this but my problem is that I have to do the same thing again with a second part after all this, and then... It doesn't work.

To close my document with a justifying document:

swApp.CloseDoc NOMFICHIEROUVERT.SLDPRT

 

And to close my drawing document I use:

swApp.CloseDoc swModel.GetTitle

 

Because 

swApp.CloseDoc NOMFICHIEROUVERT.SLDDRW

does not work.

This method closes my drawing file but also closes the solidworks window and I think my problem comes from there because the application is not closed for all that.

and when I ask to open the second room with:

    Set swDocSpecification = swApp.GetOpenDocSpec(PATH & "\" & NOUVELLEPIECEAOUVRIR.SLDPRT)
    Set swModel = swApp.OpenDoc7(swDocSpecification)

(Same method used before to avage my first piece as well as its drawing), I have the following message:

Run-time error '-2147023170 (800706be)':

Automation error
The remote procedure call failed

pointing to:

Set swDocSpecification = swApp.GetOpenDocSpec(PATH & "\" & NOUVELLEPIECEAOUVRIR.SLDPRT)

 

Does anyone have any ideas?

Thanks in advance

Yves

Hello

If you open Solidworks with a CreateObject in the macro, when you close the last document it disconnects the object "SLDWORKS.application". To avoid this, you can open your following file before closing the last document of the first process:

Sub Macro1()

    Dim swApp As SldWorks.SldWorks
    Dim swModelUn As SldWorks.ModelDoc2
    Dim swModelDeux As SldWorks.ModelDoc2

    If swApp Is Nothing Then
        Set swApp = CreateObject("SLDWORKS.application")
        swApp.Visible = True
    Else
        Set swApp = Application.SldWorks
    End If

    Set swDocSpecification = swApp.GetOpenDocSpec("C:\Users\DRO\Desktop\piece1520.sldprt")
    Set swModelUn = swApp.OpenDoc7(swDocSpecification)
    MsgBox swModelUn.GetTitle
    
    Set swDocSpecification = swApp.GetOpenDocSpec("C:\Users\DRO\Desktop\piece1520.slddrw")
    Set swModelUn = swApp.OpenDoc7(swDocSpecification)
    MsgBox swModelUn.GetTitle
    
    swApp.CloseDoc swModelUn.GetTitle
    
    Set swModelUn = swApp.ActiveDoc
    MsgBox swModelUn.GetTitle
    
    Set swDocSpecification = swApp.GetOpenDocSpec("C:\Users\DRO\Desktop\piece1532.sldprt")
    Set swModelDeux = swApp.OpenDoc7(swDocSpecification)
    
    swApp.CloseDoc swModelUn.GetTitle

    MsgBox swModelDeux.GetTitle
    
    swApp.CloseDoc swModelDeux.GetTitle
    
End Sub

 

Another solution is to reset the swApp to Nothing and then recreate an object before processing file 2:

    Set swApp = Nothing
    
    If swApp Is Nothing Then
        Set swApp = CreateObject("SLDWORKS.application")
        swApp.Visible = True
    Else
        Set swApp = Application.SldWorks
    End If
    
    Set swDocSpecification = swApp.GetOpenDocSpec("C:\Users\DRO\Desktop\piece1533.sldprt")
    Set swModelDeux = swApp.OpenDoc7(swDocSpecification)
    
    MsgBox swModelDeux.GetTitle
    
    swApp.CloseDoc swModelDeux.GetTitle

 

Another solution would be to open Solidworks with a Shell ("C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\SLDWORKS.exe") at the beginning of the macro and then hook to that Solidworks, which is what the line Set swApp = Application.SldWorks does in the if at the beginning. Be careful, opening Solidworks by this principle is much longer and may force you to set a tempo before continuing the processing, and in addition it is still, in my opinion, much less clean.

Kind regards

2 Likes

Hello and Happy New Year to all

Again, sorry for the response time.

Since I have two pairs (PART file + DRW file) to open and save under a new name, I tried to open the second PART file and its DRW before closing the first DRW file, let me explain:

  • - I open PART1
  • - I open DRW1
  • - I'm coming back to PART1
  • - I close PART1
  • - I open PART2
  • - I open DRW2
  • - I'm coming back to PART2
  • - Close PART2

And now I've come back to DRW2 but since I also still have DRW1 open, I should be able to close DRW2... but no... no error messages but neither DRW2 nor DRW1 close.

On the other hand, if I proceed as follows:

  • - I open PART1
  • - I open DRW1
  • - I'm coming back to PART1
  • - I close PART1
  • - I open PART2
  • - I open DRW2
  • - I close DRW2
  • - Close PART2

In this case, DRW2 closes along with PART2

Any ideas?

Thank you

Yves

Hello

Be careful, in the example given above, I played with several ModelDoc2 objects:

Dim swModelUn As SldWorks.ModelDoc2

Dim swModelTwo As SldWorks.ModelDoc2

Objects that must be manipulated in order to define, activate and close them. You don't quickly get mixed up and then either you close the wrong document, or you try to close an undefined object, etc ...

Kind regards