3 Replies Latest reply on Nov 11, 2015 2:32 PM by Deepak Gupta

    2016 broke my macro that was written for 2014, help

    Tyler Holden

      My macro as shown below, worked great in 2014, it is broken in 2016.  I click the macro button and solidworks sits there frozen and i end up having to close it and restart solidworks.  Please advise. 




      'Export Drawing as DXF_PDF (Revision).swp ------------- 01/29/15



      'Description: Macro to save active drawing as PDF and DXF.



      'Pre-Condition: An open drawing with one view.



      'Post-Condition: Macro save active drawing as PDF and DXF in specified location with referenced model file name

      '                along with drawing revision as exported file name in the file name.



      'Please back up your data before use and USE AT OWN RISK



      ' This macro is provided as is.  No claims, support, refund, safety net, or

      ' warranties are expressed or implied.  By using this macro and/or its code in

      ' any way whatsoever, the user and any entities which the user represents,

      ' agree to hold the authors free of any and all liability.  Free distribution

      ' and use of this code in other free works is welcome.  If any portion of

      ' this code is used in other works, credit to the authors must be placed in

      ' that work within a user viewable location (e.g., macro header).  All other

      ' forms of distribution (i.e., not free, fee for delivery, etc.) are prohibited

      ' without the expressed written consent by the authors.  Use at your own risk!

      ' ------------------------------------------------------------------------------

      ' Written by: Deepak Gupta (http://gupta9665.com/)

      ' -------------------------------------------------------------------------------



      Option Explicit



      Sub main()



      Dim swApp           As SldWorks.SldWorks

      Dim swModel         As SldWorks.ModelDoc2

      Dim swDrawModel     As SldWorks.ModelDoc2

      Dim swDraw          As SldWorks.DrawingDoc

      Dim swView          As SldWorks.View

      Dim swExportPDFData As SldWorks.ExportPdfData

      Dim nErrors         As Long

      Dim nWarnings       As Long

      Dim Revision        As String

      Dim PartNumber      As String

      Dim nFileName       As String

      Const Path          As String = "\\WDMYCLOUDMIRROR\Public\QuadM Release Drawings" 'Change the path here



      Set swApp = Application.SldWorks

      Set swDrawModel = swApp.ActiveDoc



      ' Check to see if a drawing is loaded.

      If swDrawModel Is Nothing Then

              MsgBox "There is no active drawing document"

              Exit Sub

      End If


      If swDrawModel.GetType <> swDocDRAWING Then

              MsgBox "Open a drawing first and then TRY again!"

              Exit Sub

      End If



      Set swDraw = swDrawModel



      Revision = swDraw.CustomInfo("Revision")

      If Revision = "" Then

              Revision = ""

      End If



      Set swExportPDFData = swApp.GetExportFileData(1)

      swExportPDFData.ViewPdfAfterSaving = False





      Set swView = swDraw.GetFirstView

      Set swView = swView.GetNextView

      Set swModel = swView.ReferencedDocument



      ' Determine if there is any view

      If swModel.GetPathName = "" Then

              MsgBox "Insert a View first and then TRY again!"

              Exit Sub

      End If



      'Get Model Name

      PartNumber = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)

      PartNumber = Left(PartNumber, InStrRev(PartNumber, ".") - 1)



      nFileName = Path & "\" & PartNumber & " " & Revision



      'Save as DXF

      swDraw.SaveAs3 nFileName & ".DXF", 0, 0



      'Save as PDF`

      swDraw.SaveAs3 nFileName & ".PDF", 0, 0



      End Sub

        • Re: 2016 broke my macro that was written for 2014, help
          John Alexander

          When you get the error, what line does the debugger point to and what is the error?


          Edit: Sorry, poor reading comprehension on my part.


          I don't have any way of testing your code (2014 is highest release available to me) but I can suggest how to proceed. Your macro is hanging somewhere, you need to identify what function call is causing the issue. When the program starts to hang, you can use the "Pause/Break" key on your keyboard (I think it's shift+break or cntrl+break) to kill the macro during execution. When this happens, the debugger should open with the current line highlighted. This will inform how to proceed with your debugging.


          If solidworks is actually frozen, the above approach may not work. One alternative is to insert several Debug.Print statements in several places in the macro. You can use these print statements to see how far the macro got before it started to hang. The print statements will display whatever you want them to in the vba editor's Immediate Window. In this case, you have to run the macro with the editor open.

          • Re: 2016 broke my macro that was written for 2014, help
            JOHN GEORGE


            It's working fine here in 2016

            You need to modify this line


            Const Path          As String = "\\WDMYCLOUDMIRROR\Public\QuadM Release Drawings" 'Change the path here


            Specify the folder "-----------"  to save location

            • Re: 2016 broke my macro that was written for 2014, help
              Deepak Gupta

              Can't check it but make sure this path exist and you've write access.


              \\WDMYCLOUDMIRROR\Public\QuadM Release Drawings