6 Replies Latest reply on Dec 18, 2014 3:27 PM by Alex Smith

    Invisible solidworks VBA process help

    Alex Smith

      I have having a problem and not sure how to solve it.  I am making a macro using VBA to open a file invisible and perform a save as to a new name and exit.  The code is below:


      Option Compare Database

      Dim swApp As Object


      Dim Part As Object

      Dim boolstatus As Boolean

      Dim longstatus As Long, longwarnings As Long


      Private Sub Command0_Click()


      Set swApp = CreateObject("sldworks.application")


      Set Part = swApp.OpenDoc6("D:\SWIM_CACHE\3005622.SLDASM", 2, 0, "", longstatus, longwarnings)

      swApp.ActivateDoc2 "3005622.SLDASM", False, longstatus

      Set Part = swApp.ActiveDoc

      Dim myModelView As Object

      Set myModelView = Part.ActiveView

      longstatus = Part.SaveAs3("D:\SWIM_CACHE\temp.SLDASM", 0, 2)



      End Sub


      The macro works great for the smaller subassemblies which are merely used for testing the code, but the ones I ultimately want to process is much larger in comparison to the ones I have been testing.  When pulling the assembly in a regular session, there are a series of windows that popup during regeneration i.e. saves, errors, etc. before you reach the assembly.  Attached is the error code I get.  Can anyone give me any pointers or possibly modify the code I presented above?  Thanks for the new knowledge in advance!


        • Re: Invisible solidworks VBA process help
          Daniel Andersson

          The method Saveas3 is obsolete and it is recommended to use CopyDocument. Try that method instead and let us know if it worked out.



          • Re: Invisible solidworks VBA process help
            Keith Rice

            Hi Alex,


            I have some code you can try, but first some comments / questions:


            1. The macro you shared doesn't open anything invisibly, nor is it really necessary for what you're doing. May I ask why you need the file to open invisibly?


            2. Is there a reason why you are using Option Compare Database? This should only be used with Access databases and it doesn't look like one is involved with your macro in any way.


            3. If you plan on using this macro seriously, I would recommend not hard-coding paths into your sub procedures / functions. At minimum you should use a constant, as I do below. Optimally you should have the user input the file path via InputBox() or a user form.


            4. I wouldn't recommend using ISldWorks::ExitApp in a macro. This is really intended for stand-alone use. If the user needs to close out of SolidWorks, they should do it manually.


            5. In the future, when you post an error on the forums, please share what line the error is occurring on. When you click Debug, the problematic line is highlighted yellow.


            6. I realize that your data might be proprietary or so large that transferring it isn't feasible, but otherwise the file set should always be provided. Otherwise helping debug the code is quite difficult.


            All of that being said, you can try this code below. Aside from removing unnecessary code and using a newer API call, I am using an option in ISldWorks::OpenDoc6 and IModelDocExtension::SaveAs that suppresses all dialogs that appear when opening and saving the file. Please let us know what message boxes you see, if any.


            Const OPEN_PATH As String = "D:\SWIM_CACHE\3005622.SLDASM"
            Const SAVE_PATH As String = "D:\SWIM_CACHE\temp.SLDASM"
            Dim swApp As SldWorks.SldWorks
            Dim swModel As SldWorks.ModelDoc2
            Dim lngErr As Long, lngWarn As Long
            Sub main()
                Set swApp = Application.SldWorks
                Set swModel = swApp.OpenDoc6(OPEN_PATH, swDocASSEMBLY, _
                    swOpenDocOptions_Silent, Empty, lngErr, lngWarn)
                If lngErr <> 0 Then
                    swApp.SendMsgToUser "Open failed with error code: " & lngErr
                    Exit Sub
                End If
                swModel.Extension.SaveAs SAVE_PATH, swSaveAsCurrentVersion, _
                    swSaveAsOptions_Silent, Nothing, lngErr, lngWarn
                If lngErr <> 0 Then
                    swApp.SendMsgToUser "Save failed with error code: " & lngErr
                    Exit Sub
                End If
            End Sub



            SolidWorks API Video Tutorials

              • Re: Invisible solidworks VBA process help
                Alex Smith

                Thanks Keith and Daniel for your reply.  Your assumption is correct, I am trying to open the file from an access database.


                I have implemented an automation project used to process work orders.  It uses a  text file linked in the equations manager.  The text file is generated in access and saved to a location locally.  The template assembly is fetched from Teamcenter and regenerated.  Since it is already linked, it simply generates our product.  A template drawing exists as well, so after the unit is generated, the drawing is also fully detailed.  After both have been completed, they assembly and drawing are renamed to a new part number and checked into Teamcenter.  This process used to take approximately 5 days to produce before the automation, now it takes about 15 minutes.


                At this point I would like to add some more automation, by using Access to launch an invisible session, rebuild the assembly model & drawing, and save as the new number.  Since our engineers do not know Solidworks, they can simply push the command button in the Access form and have their data generated for them without interacting with Solidworks.  Our process also requires an Excel spreadsheet of the assembly BOM and a PDF of the drawing.


                I am hopeful to have these functions built into the same command eventually as to save even more time.  This will allow the engineers to work independent of our drafters.  This will be a task I plan on tackling afterward this.


                The ultimate goal is to use the Access command to launch Swimexport to export the model & drawing; generate the Solidworks data & documents as mentioned above; and use SwimImport to check the models back into Teamcenter...........all invisible.


                I am taking it 1 step at a time.  Thanks for your code.

                  • Re: Invisible solidworks VBA process help
                    Alex Smith

                    Forgot to mention.  The path will always be the same, but the new name will be manually entered into a form field by an engineer.  True automation would eliminate this step as well, but 1 step at a time.

                    • Re: Invisible solidworks VBA process help
                      Daniel Andersson

                      Alex, I do not at all like to read your post... It brings back my thoughts and my head starts to spin again about some ideas I have for my work.

                      You see, we too use SolidWorks and Teamcenter. I have spent about 3 - 4 months thinking and making blueprints in my head of a automation application as well. I finaly decided to scrap all thoughts since we most likly face a change of CAD system within 1-2 years (to NX). There is also discussions about RuleStream...  But now my old ideas and thoughts suddenly pop up again!


                      I think you are on the right path with the OpenDoc6 method since you are talking about making rebuilds and generate drawings.


                      Just a bit curious, how does Teamcenter handle the equation file at you end? Is that one also imported to Teamcenter and have references to the parts/assemblies? We currently do not use this approach but it is always good to hear what other do.


                      The second thought is how you push data to the IRM in Teamcenter. Have you enabled mapping PDM->CAD and CAD-PDM?

                      I'm thinking that you might need a separate IPS file (or make a PLMXML file) to get the data to the IRM.


                      And the last thought that comes to my mind... I do not like to usage of swimimport and swimexport that much, since they expose username and password. Which usually is something you want to keep only to a small group...


                      Sorry for the questions but it is fun to hear from someone that is using Teamcenter and SolidWorks.

                        • Re: Invisible solidworks VBA process help
                          Alex Smith

                          The text file link is removed before it goes into Teamcenter.  Currently we press the button in Access that generates the text file, then hit rebuild in solidworks and there you go.  Works great.


                          I would just like to have access do a little more, either through a button or check box.  To launch a process that:

                          1.  Generates text file (like it already does)

                          2.  Launches an invisible Solidworks session that opens up a particular assembly

                          3.  Performs a rebuild

                          4.  And saves as a new number (entered on Access form)


                          After a little direction and code help with this, Solidworks will be automated to the extent I am planning on.  Afterwards I am going to focus on automating the Teamcenter process so any ideas / help is appreciated.  Using swimexport & swimimport was a possible direction that came to mind...


                          Here is the text file generated (Global Variable=value):