2pietjuh2 February 2016

Display value of custom property Solidworks VBA Macro

I want to create a macro which exports the drawing as PDF, in the format: drawing name + title + date.

I created a macro which does exactly what I want when I enter a value for title. However in this project I want the title to be equal to the part name. Therefore I use this code for the title value:

$PRPSHEET:"SW-File Name(File Name)"

The drawing sheet displays Title just fine. However the macro does not work. My macro it does not convert this code to the part name. I cannot figure out why. Any tips?

This is how I get the title:

Dim swCustProp As CustomPropertyManager

Function FormatFileLocation() As String

Dim Title As String
Dim bool As Boolean
Dim val As String

bool = swCustProp.Get4("title", True, val, Title) 'title is the name of the 
                                                  'property and has the value: $PRPSHEET:"SW-File Name(File Name)"
end function

EDIT: I tried using both Title and val, but both give the same faulty result.

Answers


AndrewK February 2016

If you are using SOLIDWORKS 2014 or later, you should use swCustProp.Get5(). Get4() is obsolete per their documentation.

This code worked for me:

Dim swApp As Object
Dim swCustProp As CustomPropertyManager
Dim mde As ModelDocExtension
Dim swModel As ModelDoc2

Sub main()

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set mde = swModel.Extension
Set swCustProp = mde.CustomPropertyManager("")
Call FormatFileLocation

End Sub

Function FormatFileLocation()

Dim Title As String
Dim bool As Boolean
Dim val As String
Dim resolved As Boolean

bool = swCustProp.Get5("title", True, val, Title, resolved) 'title is the name of the
'property and has the value: $PRPSHEET:"SW-File Name(File Name)"
End Function

Here are my drawing custom properties:

enter image description here


AndrewK February 2016

Instead of reading from the custom properties. You could read it from the active drawing view.

Dim swApp As Object
Dim swModel As ModelDoc2
Dim swDraw As DrawingDoc

Sub main()

    Set swApp = Application.SldWorks
    Set swDraw = swApp.ActiveDoc
    Call FormatFileLocation

End Sub

Function FormatFileLocation()
    Dim Title As String
    Dim v As View
    Set v = swDraw.ActiveDrawingView
    Set swModel = v.ReferencedDocument
    Title = swModel.GetTitle()
End Function

Post Status

Asked in February 2016
Viewed 3,423 times
Voted 8
Answered 2 times

Search




Leave an answer