2 Replies Latest reply on Feb 13, 2018 5:31 PM by Matt Zellmer

    Macro won't work unless opened/closed first.

    Matt Zellmer

      I have a macro I created that saves a drawing as a PDF with the Revision added on the end.  It works fine but will only work if I first open it in the VBA editor then close it (not even saving or touching anything), then it runs just fine.  Any help is appreciated.  Below is the macro:

       

       

      Option Explicit

      Dim swApp           As SldWorks.SldWorks

      Dim swModel         As SldWorks.ModelDoc2

      Dim PDFFileName     As String

      Dim PDFPath         As String

      Dim Revision        As String

       

      Sub Main()

      Set swApp = Application.SldWorks

      Set swModel = swApp.ActiveDoc

       

      'Get Revision

      Revision = swModel.CustomInfo("Revision")

       

      'Set Path

      PDFPath = PDFPath & "C:\Users\mzellmer\Desktop\MZ\Temp"

       

      'Set File Name

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

      PDFFileName = Left(PDFFileName, InStrRev(PDFFileName, ".") - 1) & " Rev_" & Revision & ".PDF"

       

      'Save File

      swModel.SaveAs (PDFPath & "\" & PDFFileName)

       

      End Sub

        • Re: Macro won't work unless opened/closed first.
          Artem Taturevych

          Try to make the following changes:

           

          Option Explicit

          Dim swApp          As SldWorks.SldWorks

          Dim swModel        As SldWorks.ModelDoc2

          Dim PDFFileName    As String

          Dim PDFPath        As String

          Dim Revision        As String

           

          Sub Main()

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

           

          'Get Revision

          Revision = swModel.CustomInfo("Revision")

           

          'Set Path

          PDFPath = PDFPath & "C:\Users\mzellmer\Desktop\MZ\Temp"

           

          'Set File Name

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

          PDFFileName = Left(PDFFileName, InStrRev(PDFFileName, ".") - 1) & " Rev_" & Revision & ".PDF"

           

          'Save File

          swModel.SaveAs (PDFPath & "\" & PDFFileName)

           

          End Sub