Select a part to add a SolidWorks VBA note

Hello

I want to add a note via a macro related to the property of the component to which it is attached in a SolidWorks drawing.

I tried using the SolidWorks registration method but the generated code doesn't work for me. This is because it selects the part based on its coordinates and if I want to apply this macro to another drawing where the part is, it doesn't work if the part isn't placed the same way on the sheet.

Here is the code generated by SolidWorks:

Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()

Set swApp = _
Application.SldWorks

Set Part = swApp.ActiveDoc
boolstatus = Part.Extension.SelectByID2("", "EDGE", 0.418950851731392, 0.261697604464967, -499.977072134691, False, 0, Nothing, 0)

Dim myNote As Object
Dim myAnnotation As Object
Dim myTextFormat As Object
Set myNote = Part.InsertNote("$PRPMODEL:""Landmark""")
If Not myNote Is Nothing Then
   myNote.LockPosition = False
   myNote.Angle = 0
   boolstatus = myNote.SetBalloon(0, 0)
   Set myAnnotation = myNote.GetAnnotation()
   If not myAnnotation is nothing then
      longstatus = myAnnotation.SetLeader3(swLeaderStyle_e.swSTRAIGHT, 0, True, False, False, False)
      boolstatus = myAnnotation.SetPosition(0.304559076486096, 0.311886588479215, 0)
      boolstatus = myAnnotation.SetTextFormat(0, True, myTextFormat)
   End If
End If
Part.ClearSelection2 True
Part.WindowRedraw
End Sub

 

I tried replacing boolstatus = Part.Extension.SelectByID2("", "EDGE", 0.418950851731392, 0.261697604464967, -499.977072134691, False, 0, Nothing, 0)

by

boolstatus = Part.Extension.SelectByID2("Part2-1", "COMPONENT", 0.418950851731392, 0.261697604464967, -499.977072134691, False, 0, Nothing, 0)

But it doesn't work.

Does anyone know of a solution?

Thank you

Hello

I propose an idea; But if youdisplayed the origins and re-did your macro with the hook of the origin of the piece it wouldn't work??? I'm new to macros....

Hello manu67,

Thank you for your answer which almost works.

I originally added my note by recording my macro which gives me a line of code corresponding to what I wanted, i.e. the part is selected by its name and not by its coordinates.

On the other hand, it only works with parts without configuration. I'm looking to do it with configured parts.

Hello,

Wouldn't going through a note that you put in the library be the easiest?

Here is the procedure in attached file

I hope I helped you.


note_en_bibliotheque.docx

In the end, it wasn't a problem of configuration but of language in the room. I have a downloaded part that is in English, so the origin is called "Origin". That's why the macro didn't macrch for this part.

@fliard: thank you for your answer, I'll keep the tip that can be useful to me later. On the other hand, for this application it does not correspond to what I expect because I already have a macro that adds dimensions and I complete it to add notes. Also, the notes I add aren't always on the same piece.

Have a good day:)

Hello

Is it possible to post the macro that works in a text file for example?

Thank you!

Attached is the macro that works


macronote.txt

Hello

thank you for selecting me as the best answer but I don't think I was of much help to you but thank you...