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


Categories: API∕Macros

Comments
Last comment By: Matt Zellmer   Tue, 13 Feb 2018 22:31:44 GMT
Re: Macro won't work unless opened/closed first.

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

By: Artem Taturevych  Tue, 13 Feb 2018 22:22:41 GMT
Re: Macro won't work unless opened/closed first.

That did it thanks, I cobbled it together from other macros I found online.

By: Matt Zellmer  Tue, 13 Feb 2018 22:31:44 GMT
You are not authorized to view this page No results found! Suggestions: Check spelling, try a different search, or browse topics below.