5 Replies Latest reply on Apr 13, 2015 12:02 PM by Ivana Kolin

    Export specific bom-template

    Frauke Stevens

      Hi,

       

      I have quite some experience in VBA programming in Excel/Access/Outlook, but I'm completely new to VBA-programming in Solidworks and it seems I'm overlooking some very basic knowledge, because I've found a quite straight forward macro on this forum to export a specific bom-template, see below, but sadly it doesn't come past the third line...

       

      Dim swApp As SldWorks.SldWorks

      Dim swModel As SldWorks.ModelDoc2

      Dim swBOMTable As SldWorks.BomTableAnnotation

      Dim swTable As SldWorks.TableAnnotation

      Dim swAnn As SldWorks.Annotation

      Const BOMTemplate As String = "I:\Templates\Tables\Standard Bom\bom-export.sldbomtbt"

      Const OutputPath As String = "G:\Stuklijsten\"

      Dim ConfigName As String

       

      Sub main()

       

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

         

          ConfigName = swModel.GetActiveConfiguration.Name

          Set swBOMTable = swModel.Extension.InsertBomTable(BOMTemplate, 0, 0, swBomType_e.swBomType_Indented, ConfigName)

          Set swTable = swBOMTable

        

          swTable.SaveAsText OutputPath & "BOMTable_" & swModel.GetTitle() & ".xls", vbTab

          Set swAnn = swTable.GetAnnotation

          swAnn.Select3 False, Nothing

       

          swModel.EditDelete

         

      End Sub

       

      Can somebody figure out why I keep getting the run time error '91 Object variable or With block variable not set, this would be very much appreciated...

        • Re: Export specific bom-template
          Frauke Stevens

          Ik zie dat mijn vraag ondertussen al behoorlijk vaak bekeken is, maar toch nog geen reactie.

          Ik weet niet zo goed wat ik daaruit moet concluderen, is mijn vraagstelling niet juist, of niet duidelijk genoeg, heb ik het op de verkeerde plek gezet?

           

          Nog even ter verduidelijking, ik heb een tekening geopend staan, maar toch blijft het swModel Nothing, waardoor ConfigName niet kan worden uitgevoerd...

            • Re: Export specific bom-template
              Ivana Kolin

              vraagstelling was wel goed, hier moet je geduld hebben.

               

              1. Is het een solidworks macro of probeer je het vanuit excel vba?

              2. Als je een model (welke dan ook) open hebt, dan kan swModel nooit Nothing zijn. Dit lijkt meer op twee keer opgestarte solidworks, kijk even bij processen in taakbeheer.

               

              3. I denk dat je dit zoekt.

               

              Dim swApp As SldWorks.SldWorks
              Dim swModel As SldWorks.ModelDoc2
              Dim swBOMTable As SldWorks.BomTableAnnotation
              Dim swTable As SldWorks.TableAnnotation
              Dim swAnn As SldWorks.Annotation
              Const BOMTemplate As String = "I:\Templates\Tables\Standard Bom\bom-export.sldbomtbt"
              Const OutputPath As String = "G:\Stuklijsten\"
              Dim ConfigName As String
              Dim swDraw                      As SldWorks.DrawingDoc
              Dim swView                      As SldWorks.View
              Dim swDrawComp                  As SldWorks.DrawingComponent
              Sub main()
              
                  Set swApp = Application.SldWorks
                  Set swModel = swApp.ActiveDoc
                  Set swDraw = swModel
              
              
                  Set swView = swDraw.GetFirstView
                   Do While Not swView Is Nothing
                     
                      Set swDrawComp = swView.RootDrawingComponent
                      If Not swDrawComp Is Nothing Then
                          ConfigName = swView.ReferencedConfiguration
                          Exit Do
                      End If
                      Set swView = swView.GetNextView
                  Loop
              
              
                  If swDrawComp Is Nothing Then Exit Sub
                      
                   swDrawComp.Select False, Nothing
                   Set swBOMTable = swView.InsertBomTable4(False, 0.2, 0.3, swBOMConfigurationAnchor_TopLeft, swBomType_Indented, Configuration, BOMTemplate, False, swNumberingType_Detailed, True)
              
              
                  Set swTable = swBOMTable
                
                  swTable.SaveAsText OutputPath & "BOMTable_" & swModel.GetTitle() & ".xls", vbTab
                  Set swAnn = swTable.GetAnnotation
                  swAnn.Select3 False, Nothing
              
                  swModel.EditDelete
                 
              End Sub
              
              
            • Re: Export specific bom-template
              Keith Rice

              You would get an error on the third line in main() only if you didn't have a document open.

               

              You can learn more about common VBA errors by using the PDF I mention at the top of my Macro Library.

               

              I would recommend you check out some of the tutorials at our web site, and also this blog post:

              7 Mistakes New SolidWorks API Programmers Make

               

              Keith

              SolidWorks API Tutorials

                • Re: Export specific bom-template
                  Frauke Stevens

                  Dear Keith,

                   

                  Thanks for your answer, sadly this hasn't help me to solve the issue.

                  Before I posted my question I already found the articles you referring to and already figured out that I am in the 4th category, not understanding the SolidWorks API Object model. I also read the PDF about the most common VBA errors, I totally understand that I'm running into the first example for runtime error 91 No variable set. But the only thing that is stated with this example is that this error will occure if no part is open, but I believe that I did open an AssemblyDoc before running the macro. And looking at the Object Model I find that an AssemblyDoc is a SldWorks.ModelDoc2 type of object, which is the type set for the variable swModel, so I don't get it.

                   

                  Likely my lack of understanding is due to the fact that I don't work with SolidWorks myself. My collegues do and we have tried to figure out together why the drawing on my screen is not the ActiveDoc, but without success. Therefor I've included a print screen to show what is open in SolidWorks.PrintScreen.png

                  I also found the remark somewhere that maybe the code is reffering to another SolidWorks instance, but looking at the task manager, I only see one SolidWorksApplication.

                  I absolutely don't wan't to fall into category 7. Giving up to quickly, I'm not a quiter and with or without your help I'm going to figure this out. It seems to me though that the SolidWorks API Experts should be able to point out what I am missing in app. 0.1% of the time I've already put into this. Please help me to get past this hurdle, I am really not someone that just posts every issue I run into on a forum, I am way to proud for that...