MACRO LIBRARY

Find text in blocks made from notes

Free public access. No account or subscription required.

Files

Find text in blocks made from notes

Code

'Demonstrates how to search blocks created from notes for a particular piece of text.
    
'Preconditions: drawing is open
'Results: if no block made from a note containing specified text is found, user is alerted
    
'Written by Keith Rice
'CADSharp LLC
'www.cadhsharp.com
    
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim vBlockDefs As Variant
Dim swBlockDef As SldWorks.SketchBlockDefinition
Dim vNotes As Variant
Dim swNote As SldWorks.Note
Dim i As Integer
Dim j As Integer
Dim blnFound As Boolean
    
Sub main()
    blnFound = False
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    vBlockDefs = swModel.SketchManager.GetSketchBlockDefinitions
    For i = 0 To UBound(vBlockDefs)
        Set swBlockDef = vBlockDefs(i)
        vNotes = swBlockDef.GetNotes
        If IsEmpty(vNotes) <> True Then
            For j = 0 To UBound(vNotes)
                Set swNote = vNotes(j)
                If InStr(1, swNote.GetText, "Material") <> 0 Then
                    If InStr(1, swNote.GetText, "EN23") <> 0 Then blnFound = True
                End If
            Next j
        End If
    Next i
    If blnFound = False Then swApp.SendMsgToUser "Material not found."
End Sub
    
'Notes
'   IBlockDefinition was superceded by ISketchBlockDefinition
'   IBlockInstance was superceded by ISketchBlockInstance
'   Many instances can exist of one definition. Use ISketchBlockInstance::Select to select _
        an individual instance.
'   Blocks are retrieved from the FeatureManager tree, not the sketch, so the search of all _
        blocks occurs regardless of whether the sheet containing any blocks is active.