Macro insertion annotation favorites

Hello everyone,

I'm looking to insert an annotation from the solidworks design library.

I constantly load the library notes and use a userform to select  the desired note

 

I'm stuck on inserting the note, I used the InsertAnnotationFavorite method  

I would have to be able to insert  it into a specific  layer: layers are created in the macro layer

A subscript note in A subscript layer

B subscript note in B subscript tracing

C index note in C index layer

anyone have an idea?

Thank you in advance for your help

 

Kind regards

 

Yannick

 


note.zip

Hello Try this:

Option Explicit
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAnn As SldWorks.Annotation
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swAnn = swModel.Extension.InsertAnnotationFavorite("C:\monDossierDeNotes\Indice A.sldnotestl", 0.2, 0.2, 0)
swAnn.Layer = "Indice A"
Set swAnn = swModel.Extension.InsertAnnotationFavorite("C:\monDossierDeNotes\Indice B.sldnotestl", 0.2, 0.2, 0)
swAnn.Layer = "Indice B"
Set swAnn = swModel.Extension.InsertAnnotationFavorite("C:\monDossierDeNotes\Indice C.sldnotestl", 0.2, 0.2, 0)
swAnn.Layer = "Indice C"
swModel.WindowRedraw
End Sub

or:

Option Explicit
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAnn As SldWorks.Annotation
Dim NoteNames As Variant
Dim NoteFolder As String
Dim i As Integer
NoteNames = Array("Indice A", "Indice B", "Indice C")
NoteFolder = "C:\monDossierDeNotes\"
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
For i = 0 To 2
    Set swAnn = swModel.Extension.InsertAnnotationFavorite(NoteFolder & NoteNames(i) & ".sldnotestl", 0.2, 0.2, 0)
    swAnn.Layer = NoteNames(i)
Next
swModel.WindowRedraw
End Sub

 

Hello Jerome,

Thank you for your feedback

I integrated your macro into my loop. The note is not inserted.

When I go through the macro, Set swAnn doesn't load anything.

If template = "Indice B" Then 

Set swAnn = swModel.Extension.InsertAnnotationFavorite("C:\sw-parameter\Blocks\Revision Index\B.sldnotestl Index", 0.2, 0.2, 0)
swAnn.layer = "B-Index"
swModel.WindowRedraw

 

Do you know where this can come from?

Thanks in advance

 

Yannick


insert_indice.swp

swModel is not declared as a global variable.
so despite being assigned in Layer(), it sucks in ChgtComboBox1()
we must add: Dim swModel as sldworks.modeldoc2 before main()

In fact, there are a lot of undeclared variables.
My suggestion, adds: Explicit Option
at the very beginning of the macro and corrects any reporting errors.

Also declare Layer() with: Sub Layer(void)
and calls it with: Call Layer(Empty) or: Layer Empty
Same for ChgtComboBox1()
It will force the macro to launch from main()

Thank you Jerome, the macro works perfectly when you declare all the variables

For my information, what does (void) and (empty) mean?

Thanks for everything.

 

void => empty, empty => nothing

It is important that a macro is only one main function (usually main()).

A function is secondary if it has a parameter like (ByVal swModel As ModelDoc2). If a function is secondary but does not need a parameter, the convention is to assign it an empty parameter. (Void)