Kevin Wilson

Macro to generate PDF in Folder

Discussion created by Kevin Wilson on Mar 2, 2020
Latest reply on Mar 2, 2020 by Kevin Wilson

Good morning!

I'm soooooo close to having this macro right I can taste it...but it's not quite there.  Could anyone out there offer me a hand please?  Note: in our work process, parts are commonly configured but each configuration will be detailed in its own drawing file.

 

My macro needs the following variables:

The congifuration-specific property "PartNumber" from the Part file.

The custom property "Revision" from the drawing file.

 

I built the macro to create a new folder in our O Drive/Released/Drawings (Manufactured) location, and title that new folder via "PartNumber".  Then create the new PDF inside that new folder, titled via "PartNumber-Revision".

 

Everything works perfectly...as long as the part is also opened and the referenced configuration is active.  But I've done some further testing and found this problem: 

If only a drawing is opened, and the part being referenced by that drawing has multiple configurations, the macro will run using only the active part configuration from the last time the part was saved.  I need it to ALWAYS look at the drawing view, and use that drawing view's referenced configuration fro the PartNumber variable...regardless of what might be active in the unopened part.  I think the issue may be in the swView part of the code, but I'm not sure.  If anyone could take a look and offer something I'd really appreciate it!

 

Here's my code (and I certainly pieced this together from code provided in the forums...thanks to everyone who I gathered info from).

 

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swCustProp As CustomPropertyManager
Dim ValOut As String
Dim ResolvedValOut As String
Dim FilePath As String
Dim FileName As String
Dim swView As SldWorks.View
Dim ConfigName As String
Dim Rev As String
Dim PDFPath As String
Dim oFolder As String
Dim PDFName As String


Sub main()

 

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swDraw = swModel

 

'Check to insure open document is a drawing
If (swDraw Is Nothing) Or (swDraw.GetType <> swDocDRAWING) Then

swApp.SendMsgToUser ("For use with Solidworks drawing files only. Please open a drawing and try again.")

 

'If open document is not a drawing, then exit the macro
Exit Sub

End If

 

Debug.Print "File = " & swModel.GetPathName

 

'Instruct swView variable to reference the drawing view
Set swView = swDraw.GetFirstView
Set swView = swView.GetNextView


'Instruct swModel variable to use the model being referenced by swView for properties
Set swModel = swView.ReferencedDocument

 

'Get the name of the part configuration that is being referenced by the drawing view
ConfigName = swView.ReferencedConfiguration

 

'Set ResolvedValOut variable equal to the part configuration specific property called PartNumber
Set swCustProp = swModel.Extension.CustomPropertyManager(ConfigName)
swCustProp.Get2 "PartNumber", ValOut, ResolvedValOut

 

'Set FileName variable equal to the ResolvedValOut variable
FileName = ResolvedValOut


'Set Rev variable equal to the drawing custom property called Revision
Rev = swDraw.CustomInfo("Revision")

 

'Create an O Drive Released folder titled with the Part Number unless it already exists
PDFPath = "O:\Released\Drawings (Manufacturing)\" & FileName
oFolder = PDFPath
If Dir(oFolder, vbDirectory) = vbNullString Then
MkDir (oFolder)
End If

 

'Set PDFName variable to indicate the soon to be created PDF as an extension of PDFPath
PDFName = PDFPath & "\"

 

'Create and save the new PDF File inside the O Drive Released folder
swDraw.SaveAs (PDFName + ResolvedValOut + "-" + Rev + ".PDF")

 

MsgBox "PDF File created for:" & Chr(13) & Chr(13) & "Drawing: " & (ResolvedValOut) & Chr(13) & "Revision: " & (Rev) & Chr(13) & "Location: O:\Released\Drawings (Manufacturing)\" & (ResolvedValOut) & Chr(13) & Chr(13)

End Sub

Outcomes