6 Replies Latest reply on Jan 7, 2016 2:30 PM by Jackie Yip

    Reload Multiple Templates in Multi-sheet drawing

    Garret Hansen

      I am working on a larger project in small pieces and this is the first part.

       

      I have a macro that Deepak Gupta had been very helpful on.  I have been searching and can't quite find the answer and my stab at it is not working.

       

      I have drawings that have multiple sheets and they don't always have the same template.  So what I want to do is instead of fixing the template to reload as is done here:

       

      ' Change Sheet format name here which you to put on the drawing sheet.

      Const sTemplateName As String = "BV-ASSEMBLY-RDI-TBLK-in.slddrt"

       

      I would like to get and set the template for each sheet with this code:

       

      Set sTemplateName = swSheet.GetTemplateName

       

      Then the rest of the code would be the same.  However I get a compile error on the GetTemplateName.

       

      Below is the code that Deepak created and I have made some edits to.

      '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 the older sheet format.

       

       

      'Post-Condition: Macro will set the sheet format to NONE and then relaod the same format from the specified location.

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

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

      '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 longstatus      As Long

      Dim longwarnings    As Long

      Dim nErrors         As Long

      Dim nWarnings       As Long

      Dim i               As Long

       

       

      Dim sFileName       As String

      Dim Path            As String

      Dim sTemplateName   As Variant

       

       

       

       

       

       

      ' Change sheet format location here

      Const sTemplatePath As String = "C:\RDIVault\ENGINEERING\SW TEMPLATES\"

       

       

      ' Change Sheet format name here which you to put on the drawing sheet.

      'Const sTemplateName As String = "BV-ASSEMBLY-RDI-TBLK-in.slddrt"

       

       

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

       

       

      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 and update the format

          For i = 0 To UBound(vSheetName)

       

       

              swDraw.ActivateSheet vSheetName(i)

              Set swSheet = swDraw.GetCurrentSheet

             

              Set sTemplateName = swSheet.GetTemplateName

             

              vSheetProps = swSheet.GetProperties

         

          'Set the sheet format to NONE

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

         

          'Reload the sheet format from the speicifed location

          swModel.SetupSheet5 swSheet.GetName, swDwgPapersUserDefined, swDwgTemplateCustom, vSheetProps(2), vSheetProps(3), False, sTemplatePath & sTemplateName, 0.4318, 0.2794, "Default", True

          swDraw.ViewZoomtofit2

         

      Next i

       

       

          swDraw.ActivateSheet vSheetName(0)

          swDraw.ForceRebuild3 False

          swDraw.Save3 1, nErrors, nWarnings

             

      Set swDraw = Nothing

       

       

      End Sub

      Any help would be greatly appreciated.

        • Re: Reload Mulitple Templates in Multi-sheet drawing
          Bill Florac

          Not being an expert in VBA but doesn't GetSheetName return a string, not an object so you would not use the Set command?

           

          Anyway, I have no problem calling GetTemplateName from my external application (not script). It might also be a safer code to make sure that GetCurrentSheet does not return nothing.

            • Re: Reload Mulitple Templates in Multi-sheet drawing
              Garret Hansen

              Thanks Bill that made me look at little closer and so I have changed the following code:

               

              Dim vTemplateName   As Variant                 (It was  Dim sTemplateName   As Variant)

               

               

              vTemplateName = swSheet.GetTemplateName        (It was  Set sTemplateName = swSheet.GetTemplateName)

               

               

              swModel.SetupSheet5 swSheet.GetName, swDwgPapersUserDefined, swDwgTemplateCustom, vSheetProps(2), vSheetProps(3), False, sTemplatePath & vTemplateName, 0.4318, 0.2794, "Default", True

                  swDraw.ViewZoomtofit2

               

              This code will now pop up the message window that asks you to choose the template that you want to use.  I have to manually select the appropriate template but it does it for each sheet so I can have different templates for each sheet.

               

              I think I am getting closer but I need it to figure out what template that specific sheet already has and then refresh it on a sheet by sheet basis.

                • Re: Reload Mulitple Templates in Multi-sheet drawing
                  Garret Hansen

                  I am a bit of a hack when it comes to code so I am grateful there are smarter people than me out there that are willing to share their knowledge.

                   

                  I have been experimenting with this code and have created two new Macros.

                   

                  The first one will set the sheet format for the current active sheet only.  It is just like right clicking, hitting properties, then reloading or selecting a new sheet format from your list.  Probably not too much faster than doing it the manual way.

                   

                  The second one will set the sheet format for each sheet in the drawing individually.  It is like that above but it automatically cycles through all the sheets in your drawing and you select a format for each sheet in case you want different formats on different sheets.  This is definitely faster than the manual method.

                   

                  I am still trying to figure out how to get it to find out what sheet format is currently set and then reload that same sheet format automatically on a sheet by sheet basis.

                   

                  I am not sure if "vTemplateName = swSheet.GetTemplateName" is actually pulling the current format properly or not.

              • Re: Reload Multiple Templates in Multi-sheet drawing
                Jackie Yip

                Hello,

                 

                This Macro is exactly what I'm looking for. However, it seems like to only that is missing is the ability to run in a task scheduler because all the files need to be opened beforehand. I saw that there is custom code where you can get the macro running in a task scheduler and it will automatically open up all the files needed. Here is is: Run Macro in Task Scheduler

                 

                If someone had any experience that could add this piece of information, that would be great. Thanks!

                 

                Jackie