1 Reply Latest reply on Apr 22, 2016 1:59 PM by Steven Barry

    I'm trying to fix this macro.

    Eddie Pellegrin

      This macro works perfectly to look at the sheet format loaded and then to reload it. The issue I'm having, is we have deleted one of the sizes (B) that we used to use. We used to use B, C, & D, now we only use C & D. Can someone please show me what code I can add to this macro to tell it if the original sheet format is gone, load up the C size? Right now if it's C & D it loads up great, but if its a B, it just sets the format to none. (Thanks to Deepak for writing this one)




      'Reload_All_Sheet_Formats.swp ------------------------ 03/18/2015

      'Modified Version of --- Reload Drawing Sheet Format.swp ------------- 06/05/14



      'Description: Macro to Reload Drawing Files Sheet Format for all sheets.



      'Pre-Condition: An active drawing document having any sheet format.



      'Post-Condition: Macro will set the sheet format to NONE and then reload the previous sheet format from the specified location on a sheet by sheet basis.

      'Might delete everything contained within the old/exisitng format.



      '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/)

      ' Edited by Garret Hansen 03/18/2015

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

      'Option Explicit



      Sub main()



      Dim swApp               As SldWorks.SldWorks

      Dim swModel             As SldWorks.ModelDoc2

      Dim swDraw              As SldWorks.DrawingDoc

      Dim swSheet             As SldWorks.Sheet

      Dim vSheetProps         As Variant

      Dim vSheetName          As Variant

      Dim vTemplateName       As Variant



      Dim longstatus          As Long

      Dim longwarnings        As Long

      Dim nErrors             As Long

      Dim nWarnings           As Long

      Dim i                   As Long






      On Error Resume Next



      Set swApp = Application.SldWorks

      Set swModel = swApp.ActiveDoc



      ' Check to see if a drawing is loaded.

      If swModel Is Nothing Then

              MsgBox "There is no active drawing document"

              Exit Sub

      End If


      If swModel.GetType <> swDocDRAWING Then

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

              Exit Sub

      End If



      Set swDraw = swModel


          vSheetName = swDraw.GetSheetNames



          ' Traverse the drawing sheets

          For i = 0 To UBound(vSheetName)



              swDraw.ActivateSheet vSheetName(i)

              Set swSheet = swDraw.GetCurrentSheet


              'Get the current drawing sheet format from this sheet

              vTemplateName = swSheet.GetTemplateName


              vSheetProps = swSheet.GetProperties


          'Set the sheet format to NONE

          swModel.SetupSheet5 swSheet.GetName, swDwgPapersUserDefined, swDwgTemplateNone, vSheetProps(2), vSheetProps(3), False, "", vSheetProps(5), vSheetProps(6), "Default", True


          'Reload original sheet format for this sheet

          swModel.SetupSheet5 swSheet.GetName, swDwgPapersUserDefined, swDwgTemplateCustom, vSheetProps(2), vSheetProps(3), False, vTemplateName, vSheetProps(5), vSheetProps(6), "Default", True



      Next i



          swDraw.ActivateSheet vSheetName(0)

          swDraw.ForceRebuild3 False

          swDraw.Save3 1, nErrors, nWarnings


      Set swDraw = Nothing



      End Sub

        • Re: I'm trying to fix this macro.
          Steven Barry



          You will probably want to add some kind of "if" statement before the "reload original sheet format..." part.  You need to check either if vTemplateName actually returned a value from the GetTemplateName method, or you need to check if the returned value of vTemplateName exists within your available templates.  If vTemplateName doesn't return a valid template, you should set the template to some default template that you designate (or add some additional logic to determine which template to set to, e.g. check the sheet size and use the associated template).


          You might consider adding some kind of debug statement to print the value of vTemplateName after the line where it is assigned.  This way, you can test it with a working case to see the exact syntax it comes up with (whether or not it is a full path, or just the name).


          Good luck!