Problems accessing notes or attributes of blocks or block instances in VBA

Hello, I'm looking to read and edit notes or attributes in VBA blocks or instances of blocks in SolidWorks drawings.
I can retrieve the block objects or block instances, but after the following functions I create errors:
swBlockDef.GetNotes
swBlockDef.GetNoteCount
with swBlockDef as SldWorks.BlockDefinition
or
swlockInst.GetAnnotation
swlockInst.GetAttributes
swlockInst.GetAttributeCount
swlockInst.GetAttributeValue
swlockInst.SetAttributeValue
with swlockInst as SldWorks.BlockInstance

In the past, I have already used swBlockDef.GetNotes successfully, but now, it doesn't work anymore.
Does anyone have a solution for me?
Thanks in advance

Hello and welcome;

It would be easier if you posted (between tags) your entire macro...

It seems that SldWorks.BlockDefinition is deprecated since the 2007 version:
image

https://help.solidworks.com/2022/english/api/sldworksapi/get_block_information_example_vb.htm?verRedirect=1

extract:

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' OLD BLOCKS: Obsolete and not supported block
    '             interfaces as of SOLIDWORKS 2007
    'Dim swBlockDef                  As SldWorks.BlockDefinition
    'Dim swBlockInst                 As SldWorks.BlockInstance
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' NEW BLOCKS: New block interfaces as of SOLIDWORKS 2007
    Dim swBlockDef                  As SldWorks.SketchBlockDefinition
    Dim swBlockInst                 As SldWorks.SketchBlockInstance
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

It seems necessary to replace it with:
SldWorks.SketchBlockDefinition

with the attributes:
https://help.solidworks.com/2022/english/api/sldworksapiprogguide/Overview/Block_Definitions_and_Block_Instances.htm

Kind regards.

Hello

I use SldWorks.SketchBlockDefinition and SldWorks.SketchBlockInstance
I can get the block and Blockinstance objects.
It's after that that I have problems...

Hello

You have to look at this API example: Get Block Information Example (VBA) - 2024 - SOLIDWORKS API Help
The notes contained in a block are accessed in a variable of type variant.

You should try this code that has been around for a very long time.
On my side, it doesn't work anymore.

This doesn't mean much " it doesn't work anymore ":

  • Have error messages? If yes, which ones + screenshot.
  • What changes have been made between " it works " and " it doesn't work anymore ".
  • You did not specify the current version of Solidworks.

The macro I proposed to you (which is the same as the one in @Cyril_f ( ... nasty copier VAS :grin:) works on my Solidworks 2022 sp4:

Here is the result...

File = XXXXXXctif_Brosses.SLDDRW
      Block definition linked to file? Faux
         File name: 

  Name of block instance: Orientation-3
    Block instance(0):
      Angle            = 0 deg
      Scale            = 0,7
      TextDisplay      = 3
  ------------------------------------
      Block definition linked to file? Faux
         File name: 

  Name of block instance: logo revtech-3
    Block instance(0):
      Angle            = 0 deg
      Scale            = 0,7
      TextDisplay      = 3
  ------------------------------------
1 Like

Oops, sorry. Exhausted at the moment, I didn't pay attention to the link in your reply.

Hello
Below is the code I use for my tests
When vNote is vNote() as sldworks.note, then runtime error code 13 type mismatch
When vNote as variant, the line vNote = swBlockDef.GetNotes gives vNote equals empty
And, the line vNote = swlockInst.GetAttributes, then error code ‹ - 670099352 (d80f1868) ›: Error Automation

Sub main()
    Dim swApp                       As SldWorks.SldWorks
    Dim swModel                     As SldWorks.ModelDoc2
    Dim SwSketchMgr                 As SldWorks.SketchManager
    Dim vBlockDef()                 As SldWorks.BlockDefinition
    Dim swBlockDef                  As SldWorks.BlockDefinition
    Dim vBlockInst()                As SldWorks.BlockInstance
    Dim swlockInst                  As SldWorks.BlockInstance
    Dim vNote                       As Variant
    'Dim vNote()                     As SldWorks.Note
    Dim swNote                      As SldWorks.Note
    Dim i                           As Long
    Dim j                           As Long
    Dim k                           As Long
    Dim Text                        As String
    
    Set swApp = CreateObject("SldWorks.Application")
    Set swModel = swApp.ActiveDoc
    Set SwSketchMgr = swModel.SketchManager
    vBlockDef = SwSketchMgr.GetSketchBlockDefinitions
    
    If Not IsEmpty(vBlockDef) Then
        For i = 0 To UBound(vBlockDef)
            Set swBlockDef = vBlockDef(i)
            
            Text = swBlockDef.Name
            
            vNote = swBlockDef.GetNotes   'vide ou erreur
            
            If Not IsEmpty(vNote) Then
                For j = 0 To UBound(vNote)
                    swNote = vNote(j)
                    Text = swNote.GetText
                Next
            End If
            
            vBlockInst = swBlockDef.GetInstances
            
            If Not IsEmpty(vBlockInst) Then
                For j = 0 To UBound(vBlockInst)
                    Set swlockInst = vBlockInst(j)
                    
                    Text = swlockInst.Name
                    vNote = swlockInst.GetAttributes  'Erreur d'exécution '-670099352 (d80f1868)': Erreur Automation
                    If Not IsEmpty(vNote) Then
                        For k = 0 To UBound(vNote)
                            Set swNote = vNote(k)
                            Text = swNote.TagName
                            
                        Next
                    End If
                Next
            End If
        Next i
    End If
End Sub

Hello
So a lot of errors in the code.
Below is the corrected code that should work (works on 2024 SP5), taken from the API help code.

Sub main()
    Dim swApp                       As SldWorks.SldWorks
    Dim swModel                     As SldWorks.ModelDoc2
    Dim SwSketchMgr                 As SldWorks.SketchManager
    Dim vBlockDef                   As Variant
    Dim swBlockDef                  As SldWorks.SketchBlockDefinition
    Dim vBlockInst                  As Variant
    Dim swBlockInst                 As SldWorks.SketchBlockInstance
    Dim vNote                       As Variant
    Dim swNote                      As SldWorks.Note
    Dim i                           As Long
    Dim j                           As Long
    Dim k                           As Long
    Dim Text                        As String
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set SwSketchMgr = swModel.SketchManager
    vBlockDef = SwSketchMgr.GetSketchBlockDefinitions
    
    If Not IsEmpty(vBlockDef) Then
        For i = 0 To UBound(vBlockDef)
            Set swBlockDef = vBlockDef(i)
            
            vNote = swBlockDef.GetNotes   'vide ou erreur
            
            If Not IsEmpty(vNote) Then
                For j = 0 To UBound(vNote)
                    Set swNote = vNote(j)
                    Text = swNote.GetText
                Next
            End If
            
            vBlockInst = swBlockDef.GetInstances
            
            If Not IsEmpty(vBlockInst) Then
                For j = 0 To UBound(vBlockInst)
                    Set swBlockInst = vBlockInst(j)
                    
                    Text = swBlockInst.Name
                    vNote = swBlockInst.GetAttributes
                    If Not IsEmpty(vNote) Then
                        For k = 0 To UBound(vNote)
                            Set swNote = vNote(k)
                            Text = swNote.TagName
                            
                        Next
                    End If
                Next
            End If
        Next i
    End If
End Sub

Basically, declaring variables that are incorrect:

 Dim vBlockDef()                 As SldWorks.BlockDefinition 

Variable type Variant and no parentheses to put on the name of this variable.
Missing a set of the swNote variable and many others.

1 Like

Hello
You're right about the missing sets on swNote = vNote(i)
On the other hand, when Dim vBlockDef As Variant, I get an error at the line Set swBlockDef = vBlockDef(i), that I am when Dim vBlockDef() As SldWorks.BlockDefinition

Which version of SW?
I have no mistakes on SW 2024.

The @Cyril_f macro also works in Solidworks 2022.
When you talk about an error, please indicate its number (or take a screenshot).

I'm in SW2022 sp5.0
It's strange from my side:
When vNote as variant, then vNote = swBlockDef.GetNotes or vNote = swlockInst.GetAttributes are empty
When vNote() As SldWorks.Note, then the same functions create a runtime error 13: Type mismatch

We should look at the references (Tools-> References).
Mine are these, but my code contains different macros that need more references than necessary for the needs of the block macro.
image
Then another problem that has already happened to me is the version of VBA which is not the right one and generates macOS operating errors

I just did the test of the @Cyril_f macro on an old file before 2007...
In this case, the variables are effectively empty.
=> In this situation you have to use the information provided in the example that we have already presented to you twice:
https://help.solidworks.com/2022/english/api/sldworksapi/get_block_information_example_vb.htm?verRedirect=1

with:

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' OLD BLOCKS: Obsolete and not supported block
    '             interfaces as of SOLIDWORKS 2007
    'Dim swBlockDef                  As SldWorks.BlockDefinition
    'Dim swBlockInst                 As SldWorks.BlockInstance
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' NEW BLOCKS: New block interfaces as of SOLIDWORKS 2007
    Dim swBlockDef                  As SldWorks.SketchBlockDefinition
    Dim swBlockInst                 As SldWorks.SketchBlockInstance
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Without wanting to offend you, @Patrick_CHARDON , you don't seem very comfortable with variable declarations. I recommend that you trust @Cyril_f, moreover its macro works with us.
What do you want to get as information from your bock exactly, we can probably help you but if we don't really know what you're trying to do it's complicated... And now we're going around in circles...

1 Like