AnsweredAssumed Answered

Help! macro to read part/assembly file properties not working

Question asked by Joe Tharion on May 30, 2019
Latest reply on May 31, 2019 by Joe Tharion


I'm trying to read the custom part properties from a part/assembly referenced in a drawing,

My macro opens the part/assembly from the drawing, reads the properties, and then closes the part/assembly.

My issue is that if I have mulitpe parts open, it doesnt go back the original drawing. 

Does anyone know what have I done wrong?


Here is the code I have:





Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim Part As Object
Dim modelDoc As SldWorks.ModelDoc2
Dim swFeat As SldWorks.Feature
Dim swCustProp As CustomPropertyManager
Dim swPackAndGo As SldWorks.PackAndGo
Dim swView As SldWorks.View
Dim swDraw, swDrawModel As SldWorks.DrawingDoc
Dim name, PartNo, Description As String
Dim Rev, MyPath, Wmark As String
Dim intConfig, sConfigName As String
Dim boolstatus, bRebuild, bRet As Boolean
Dim bShowConfig As Boolean
Dim errors As Long
Dim warnings As Long
Dim i As Integer
Dim vStatus, answer As Variant
Dim vConfNameArr As Variant

Sub main()

'Set file export location
MyPath = "\\\solidworksexports\SW_Export" '(use this for set a export directory - otherwise it will save in the same directory)
Set swApp = Application.SldWorks
Set modelDoc = swApp.ActiveDoc

'Check to see if the document is a Drawing
If (modelDoc.GetType = swDocDRAWING) Then 'Steps for drawing (open part/assy populate, then create PDF)
'MsgBox ("DWG!") 'Test code to see if it's a drawing
'Open the active model and add PartNo, Revision, Description
'Read the File properties from the Part/assy
Set swModel = swApp.ActiveDoc
Set modelDoc = swApp.ActiveDoc
'Set the current view as the model to open
Set swDraw = modelDoc
Set swView = swDraw.GetFirstView
Set swView = swView.GetNextView
Set modelDoc = swView.ReferencedDocument
'Get File properties of the Part/assy
PartNo = modelDoc.CustomInfo("PartNo")
Rev = modelDoc.CustomInfo("Revision")
Description = modelDoc.CustomInfo("Description")
swApp.CloseDoc modelDoc.GetTitle

'Go back to the drawing
Set swModel = swApp.ActiveDoc
Set modelDoc = swApp.ActiveDoc