12 Replies Latest reply on Sep 17, 2013 5:10 PM by Ross Doyle

    open sw-document in Excel with VBA

    Stijn Chapelle

      Hi all,


      Not sure if I'm in the right part of the Forum, but I hope so...


      I'm trying to do some automation with VBA with which I'm quite new, just started 2 days ago and quite enjoying it!

      Now I'm having a little trouble and I trust in your knowledge. To keep it simple:


      I'd like to build an Excel-button (that's no problem). After I would like the button to open a solidworks document on a mouse click. Anyone any idea how to do this?


      In the end I'll be doing something else, but if this works I'll figure out the rest.


      Thanks in advance!



        • Re: open sw-document in Excel with VBA
          Rais Patel



          Add reference to solidworks library in excel vba.

          then you can do the solidwork programming in excel vba same as u do in solidworks vba.

            • Re: open sw-document in Excel with VBA
              Stijn Chapelle

              At first I tried something with ShellExecute, but my skills aren't good enough to understand the solutions people bring forward (Keep on getting errors).


              Rais, I'll try it your way, thanks for the reply!



                • Re: open sw-document in Excel with VBA
                  Rais Patel

                  here is one sample file. Not required to add reference to sldworks library.

                    • Re: open sw-document in Excel with VBA
                      Stijn Chapelle

                      This is definitly what I want, thanks.


                      It isn't quite working though; I'm getting the following error:



                      Run-time error '429':


                      ActiveX component can't create object



                      any idea's?


                      off topic, i think it's time to take a class in VBA

                      • Re: open sw-document in Excel with VBA
                        Stijn Chapelle

                        First of all,


                        I lack A LOT of knowledge in VBA (as I have none) so I might have made wrong assumptions, but:


                        I've got a working button .


                        I tried your code at first, but it didn't work. I compared it with other code I had that managed to open sw as a program. I ended up with the following code in my button:



                        Private Sub CommandButton1_Click()


                        Set swapp = CreateObject("sldworks.application")

                        swapp.Visible = True

                        swapp.OpenDoc "C:\part1.sldprt", 1


                        End Sub



                        first, somehow publicly 'set swapp as object' upfront and then GetObject didn't work. This does, so i stuck with it


                        second, don't know why but if I don't add the swapp.Visible = True, it looks like it doesn't do anything.

                        I guess it still works without the line (as logicaly it should only set visibility) and windows seems somehow busy for a while. Later when I return that line of code and run it, it's immediatly done.

                        Anyway without it it doesn't what I want it to do, and now it does .


                        Thanks for your help!!


                        PS: I also found out that IF I want to open a drawing instead of a part, the last rule should end in ",3" instead of ",1". I presume the number at the end tells solidworks wether it's a .sldprt (1), a .sldasm (2, haven't checked this though) or a .slddrw (3, have checked this )


                        Message was edited by: Stijn Chapelle

                  • Re: open sw-document in Excel with VBA
                    Kalair Antarpreet

                    Hi Stijn,

                    I am trying to do somthing similar to you and i have no knowledge of VBA, so i think you can help me here. I got the success of launching solidworks with the help of macro in excel. Now the next step is that I want to pass some values to the global variables in solidworks from excel can you help me to get aroud it.


                    • Re: open sw-document in Excel with VBA
                      Stijn Chapelle

                      Hi all,


                      Still having some kind of trouble...


                      I manage to build a correct .txt file which my .sldprt uses for (linked) equations. When I open this file with my windows explorer and rebuild everything works fine.


                      When I open it using the button with the above code (see 'correct' answer) and I rebuild my part; the global variables and equations all turn into ??? with a yellow triangular error sign that says: "Unable to compute value. Invalid entry."


                      However, when I save the part, close SW and double-click on the part in the explorer, it all works again.


                      After I save this, and use my button again, the equations turn into errors again.


                      his tells me the error is in the way I open the document.


                      At the moment I use:

                      sldworks.application.OpenDo path, 1 (for part, 3 for drawing). If I use OpenDoc6 instead of OpenDoc it doesn't open my part anymore.


                      Anyone here who can help?

                        • Re: open sw-document in Excel with VBA
                          Ross Doyle



                          I am having a similar problem with my global variables having the yellow triangular sign that says: "Unable to compute value.  Invalid entry."


                          After running my code, I get that symbol next to every global variable, even ones that I manually add.  So to recap, its almost like the part and assembly are corrupted as it does not allow me to add new global variables.


                          Did you ever find out what is causing this issue and what the workaround is?