7 Replies Latest reply on Apr 11, 2017 3:15 PM by Peter Kennedy

    Macro prevents Solidwork from Opening Files and Running

    Peter Kennedy

      I'm looking for help on how to trouble shoot this issue, I wrote a macro to to save a drawing in a various file types, the macro works as intended and there is no issues or errors when the macro runs however after running the macro I can't open new or existing files. I can make changes to file currently open and save them with no issues and then as soon as I go to close solidworks it crashes.


      I've been talking with my VAR however they haven't been able to determine an issue yet.


      Here is the solidworks object being used:


      'Setting up all the variables

      Dim swApp As SldWorks.SldWorks

      Dim swModel As SldWorks.ModelDoc2

      Dim swDraw As SldWorks.DrawingDoc

      Dim swRefModelExt As ModelDocExtension

      Dim swCustPropMgr As SldWorks.CustomPropertyManager

      Dim swView As SldWorks.View

      Dim swDrawComp As SldWorks.DrawingComponent

      Dim swComp As SldWorks.Component2

      Dim swRefModel As SldWorks.ModelDoc2

      Dim swLayerMgr As SldWorks.LayerMgr

      Dim swLayer As SldWorks.Layer

      Dim swPageSetup As SldWorks.PageSetup

      Dim printSpec As SldWorks.PrintSpecification

      Dim swModelDocExt As SldWorks.ModelDocExtension

      Dim swExportPDFData As SldWorks.ExportPdfData


      The VAR thought it might be related to this line:


          'Set the printing specs

          Set printSpec = swModelDocExt.GetPrintSpecification


      I have commented this out and the issue still happens.


      I would perfer not to share the code for this, and I'm just looking for any advice on how to try and trouble shoot the issue.


      I'm running 2015 SP 5.0.

        • Re: Macro prevents Solidwork from Opening Files and Running
          Josh Brady

          I doubt there's enough info above for anyone to chime in with anything that's actually helpful.  Does your code contain business-sensitive info?  If you don't want to post publicly, there are a number of people (myself included) who are able to help out-of-band, although likely not pro-bono if there's no advancement/benefit to the SW community.

          • Re: Macro prevents Solidwork from Opening Files and Running
            Peter Brinkhuis

            Yeah it's going to be hard to debug variable declarations. The problem probably is in the code that you're not showing us. I lent someone my crystal ball so that isn't going to help either

            • Re: Macro prevents Solidwork from Opening Files and Running
              Ivana Kolin

              I have my crystal ball

              add this line at the end of macro

              swApp.DocumentVisible True, swDocPART


              or remove this one:

              swApp.DocumentVisible False, swDocPART

              • Re: Macro prevents Solidwork from Opening Files and Running
                Nick Gvk

                here is how i save files in different types

                and this never let me down

                could be shorter in code but then its not so easy to understand


                so use this instead


                ' ******************************************************************************

                ' Macro was writting by zundappchef on 29-8-2016 . It Saves As : .pdf  .dxf  .edrw

                ' For more information visit zundappchef.com and fill out a form

                ' ******************************************************************************

                Dim swApp As Object

                Dim Part As Object



                Sub main()



                Set swApp = Application.SldWorks

                Set Part = swApp.ActiveDoc



                Dim FilePath As String

                Dim PathSize As Long

                Dim PathNoExtention As String

                Dim NewFilePath As String



                Dim FilePath2 As String

                Dim PathSize2 As Long

                Dim PathNoExtention2 As String

                Dim NewFilePath2 As String



                Dim FilePath3 As String

                Dim PathSize3 As Long

                Dim PathNoExtention3 As String

                Dim NewFilePath3 As String



                FilePath = Part.GetPathName

                PathSize = Strings.Len(FilePath)

                PathNoExtention = Strings.Left(FilePath, PathSize - 6)

                NewFilePath = PathNoExtention & "pdf"



                FilePath2 = Part.GetPathName

                PathSize2 = Strings.Len(FilePath2)

                PathNoExtention2 = Strings.Left(FilePath2, PathSize2 - 6)

                NewFilePath2 = PathNoExtention2 & "dxf"



                FilePath3 = Part.GetPathName

                PathSize3 = Strings.Len(FilePath3)

                PathNoExtention3 = Strings.Left(FilePath3, PathSize3 - 6)

                NewFilePath3 = PathNoExtention3 & "edrw"



                Part.SaveAs2 NewFilePath, 0, True, False

                Part.SaveAs2 NewFilePath2, 0, True, False

                Part.SaveAs2 NewFilePath3, 0, True, False

                MsgBox "Saved As : .pdf  .dxf  .edrw !"


                End Sub

                ' ******************************************************************************




                just remove or add more to this as u desire


                this saves as edrw

                NewFilePath3 = PathNoExtention3 & "edrw"

                if u need an dwg

                change it to this

                NewFilePath3 = PathNoExtention3 & "dwg"



                if u need it to save it in even more different file types just copy the code and increment the numbers

                good luck