<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Edit attributes of existing block in VBA Forum</title>
    <link>https://forums.autodesk.com/t5/vba-forum/edit-attributes-of-existing-block/m-p/8784187#M5594</link>
    <description>&lt;P&gt;Here is some code that update a block reference's attribute in this scenario:&lt;/P&gt;
&lt;P&gt;1. the code asks user to select a block reference in drawing;&lt;/P&gt;
&lt;P&gt;2. the code then loop through the block's attributes to identify the target attribute by its tag;&lt;/P&gt;
&lt;P&gt;3. once the target attribute is found, the code ask user to enter desired value for the attribute;&lt;/P&gt;
&lt;P&gt;4. If user enters a text value for the attribute, the attribute value is updated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Option Explicit

Public Sub EditAttribute()

    Dim blk As AcadBlockReference
    Set blk = SelectBlockReference()
    If Not blk Is Nothing Then
        SetAttributeValue blk, "PartNumber"
    End If
    
End Sub

Private Function SelectBlockReference() As AcadBlockReference

    Dim ent As AcadEntity
    Dim pt As Variant
    Dim blk As AcadBlockReference
    Dim repick As Boolean
    
    On Error Resume Next
    
    Do
        If repick Then
            ThisDrawing.Utility.GetEntity ent, pt, _
                vbCrLf &amp;amp; "Invalid selection - not a block!" &amp;amp; vbCrLf &amp;amp; "Select a block:"
        Else
            ThisDrawing.Utility.GetEntity ent, pt, _
                vbCr &amp;amp; "Select a block:"
        End If
        
        If Err.Number &amp;lt;&amp;gt; 0 Then Exit Do
        repick = False
        If TypeOf ent Is AcadBlockReference Then
            Set blk = ent
            Exit Do
        Else
            repick = True
        End If
    Loop
    
    Set SelectBlockReference = blk
    
End Function

Private Sub SetAttributeValue(blk As AcadBlockReference, attTag As String)
    
    If Not blk.HasAttributes Then
        MsgBox "Seelected block does not have attribute!"
        Exit Sub
    End If
    
    Dim atts As Variant
    Dim att As AcadAttributeReference
    Dim i As Integer
    Dim found As Boolean
    Dim inputValue As String
    
    atts = blk.GetAttributes()
    For i = 0 To UBound(atts)
        Set att = atts(i)
        If UCase(att.TagString) = UCase(attTag) Then
            found = True
            inputValue = GetUserInput(attTag)
            If Len(inputValue) &amp;gt; 0 Then
                att.TextString = inputValue
            End If
            Exit For
        End If
    Next
    
    If Not found Then
        MsgBox "The selected block does not have attribute with tag """ &amp;amp; attTag &amp;amp; """!"
    Else
        blk.Update
    End If

End Sub

Private Function GetUserInput(attTag As String) As String

    On Error Resume Next
    
    Dim text As String
    text = ThisDrawing.Utility.GetString(vbCrLf &amp;amp; "Enter value for """ &amp;amp; attTag &amp;amp; """:")
    GetUserInput = text
    
End Function
&lt;/PRE&gt;
&lt;P&gt;As you can see, 2 things the process needs to do correctly is identify target block reference (by user picking here) and identify target attribute in a block reference (by looping through attributes' tag here).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have multiple block references to a block, instead of letting user to pick one by one, you can use code to loop through entire drawing (such as ModelSpace) for identify block references by its Name/EffectiveName (so you know these block references have the target attribute). The code would look like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Dim ent As AcadEntity&lt;/P&gt;
&lt;P&gt;Dim blk As AcadBlockReference&lt;/P&gt;
&lt;P&gt;For Each ent In ThsiDrawing.ModelSpace&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; If TypeOf ent Is AcadBlockReference Then&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Set blk = ent&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;If UCase(blk.EffectiveName)="MY_TARGET_BLOCK" Then&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SetAttributeValue blk, "PartNumber"&amp;nbsp; &amp;nbsp;' ' This is the method shown in code above&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;End If&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; End If&lt;/P&gt;
&lt;P&gt;Next&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since you said "yes" to&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/14801"&gt;@Ed__Jobe&lt;/a&gt;&amp;nbsp;, I trust you could easily adapt the code here to your need.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;HTH&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 10 May 2019 19:34:30 GMT</pubDate>
    <dc:creator>norman.yuan</dc:creator>
    <dc:date>2019-05-10T19:34:30Z</dc:date>
    <item>
      <title>Edit attributes of existing block</title>
      <link>https://forums.autodesk.com/t5/vba-forum/edit-attributes-of-existing-block/m-p/8783390#M5589</link>
      <description>&lt;P&gt;First i'm not a programmer! I need to edit the value of existing attributes within a block. I have looked at many examples and everything I find incorporates inserting a new block into the drawing. I want to select the block (by name preferably) and simply change the "PARTNUM" value. Any direct code would be greatly appreciated.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 519px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/635153i9C7B3195B8667C91/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2019 13:45:30 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/vba-forum/edit-attributes-of-existing-block/m-p/8783390#M5589</guid>
      <dc:creator>snichols</dc:creator>
      <dc:date>2019-05-10T13:45:30Z</dc:date>
    </item>
    <item>
      <title>Re: Edit attributes of existing block</title>
      <link>https://forums.autodesk.com/t5/vba-forum/edit-attributes-of-existing-block/m-p/8783520#M5590</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/494794"&gt;@snichols&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;First i'm not a programmer!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If we did give you code, would you know what to do with it?&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2019 14:28:51 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/vba-forum/edit-attributes-of-existing-block/m-p/8783520#M5590</guid>
      <dc:creator>Ed__Jobe</dc:creator>
      <dc:date>2019-05-10T14:28:51Z</dc:date>
    </item>
    <item>
      <title>Re: Edit attributes of existing block</title>
      <link>https://forums.autodesk.com/t5/vba-forum/edit-attributes-of-existing-block/m-p/8783565#M5591</link>
      <description>&lt;P&gt;Depends on the code, if it's vague and cryptic probably not. If it's well documented as to what it is doing then sure.&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2019 14:46:15 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/vba-forum/edit-attributes-of-existing-block/m-p/8783565#M5591</guid>
      <dc:creator>snichols</dc:creator>
      <dc:date>2019-05-10T14:46:15Z</dc:date>
    </item>
    <item>
      <title>Re: Edit attributes of existing block</title>
      <link>https://forums.autodesk.com/t5/vba-forum/edit-attributes-of-existing-block/m-p/8783569#M5592</link>
      <description>&lt;P&gt;I don't mean would you understand it. Would you know how to transfer it to the vbaide and run it?&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2019 14:49:01 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/vba-forum/edit-attributes-of-existing-block/m-p/8783569#M5592</guid>
      <dc:creator>Ed__Jobe</dc:creator>
      <dc:date>2019-05-10T14:49:01Z</dc:date>
    </item>
    <item>
      <title>Re: Edit attributes of existing block</title>
      <link>https://forums.autodesk.com/t5/vba-forum/edit-attributes-of-existing-block/m-p/8783597#M5593</link>
      <description>yes&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 10 May 2019 14:56:32 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/vba-forum/edit-attributes-of-existing-block/m-p/8783597#M5593</guid>
      <dc:creator>snichols</dc:creator>
      <dc:date>2019-05-10T14:56:32Z</dc:date>
    </item>
    <item>
      <title>Re: Edit attributes of existing block</title>
      <link>https://forums.autodesk.com/t5/vba-forum/edit-attributes-of-existing-block/m-p/8784187#M5594</link>
      <description>&lt;P&gt;Here is some code that update a block reference's attribute in this scenario:&lt;/P&gt;
&lt;P&gt;1. the code asks user to select a block reference in drawing;&lt;/P&gt;
&lt;P&gt;2. the code then loop through the block's attributes to identify the target attribute by its tag;&lt;/P&gt;
&lt;P&gt;3. once the target attribute is found, the code ask user to enter desired value for the attribute;&lt;/P&gt;
&lt;P&gt;4. If user enters a text value for the attribute, the attribute value is updated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Option Explicit

Public Sub EditAttribute()

    Dim blk As AcadBlockReference
    Set blk = SelectBlockReference()
    If Not blk Is Nothing Then
        SetAttributeValue blk, "PartNumber"
    End If
    
End Sub

Private Function SelectBlockReference() As AcadBlockReference

    Dim ent As AcadEntity
    Dim pt As Variant
    Dim blk As AcadBlockReference
    Dim repick As Boolean
    
    On Error Resume Next
    
    Do
        If repick Then
            ThisDrawing.Utility.GetEntity ent, pt, _
                vbCrLf &amp;amp; "Invalid selection - not a block!" &amp;amp; vbCrLf &amp;amp; "Select a block:"
        Else
            ThisDrawing.Utility.GetEntity ent, pt, _
                vbCr &amp;amp; "Select a block:"
        End If
        
        If Err.Number &amp;lt;&amp;gt; 0 Then Exit Do
        repick = False
        If TypeOf ent Is AcadBlockReference Then
            Set blk = ent
            Exit Do
        Else
            repick = True
        End If
    Loop
    
    Set SelectBlockReference = blk
    
End Function

Private Sub SetAttributeValue(blk As AcadBlockReference, attTag As String)
    
    If Not blk.HasAttributes Then
        MsgBox "Seelected block does not have attribute!"
        Exit Sub
    End If
    
    Dim atts As Variant
    Dim att As AcadAttributeReference
    Dim i As Integer
    Dim found As Boolean
    Dim inputValue As String
    
    atts = blk.GetAttributes()
    For i = 0 To UBound(atts)
        Set att = atts(i)
        If UCase(att.TagString) = UCase(attTag) Then
            found = True
            inputValue = GetUserInput(attTag)
            If Len(inputValue) &amp;gt; 0 Then
                att.TextString = inputValue
            End If
            Exit For
        End If
    Next
    
    If Not found Then
        MsgBox "The selected block does not have attribute with tag """ &amp;amp; attTag &amp;amp; """!"
    Else
        blk.Update
    End If

End Sub

Private Function GetUserInput(attTag As String) As String

    On Error Resume Next
    
    Dim text As String
    text = ThisDrawing.Utility.GetString(vbCrLf &amp;amp; "Enter value for """ &amp;amp; attTag &amp;amp; """:")
    GetUserInput = text
    
End Function
&lt;/PRE&gt;
&lt;P&gt;As you can see, 2 things the process needs to do correctly is identify target block reference (by user picking here) and identify target attribute in a block reference (by looping through attributes' tag here).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have multiple block references to a block, instead of letting user to pick one by one, you can use code to loop through entire drawing (such as ModelSpace) for identify block references by its Name/EffectiveName (so you know these block references have the target attribute). The code would look like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Dim ent As AcadEntity&lt;/P&gt;
&lt;P&gt;Dim blk As AcadBlockReference&lt;/P&gt;
&lt;P&gt;For Each ent In ThsiDrawing.ModelSpace&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; If TypeOf ent Is AcadBlockReference Then&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Set blk = ent&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;If UCase(blk.EffectiveName)="MY_TARGET_BLOCK" Then&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SetAttributeValue blk, "PartNumber"&amp;nbsp; &amp;nbsp;' ' This is the method shown in code above&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;End If&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; End If&lt;/P&gt;
&lt;P&gt;Next&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since you said "yes" to&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/14801"&gt;@Ed__Jobe&lt;/a&gt;&amp;nbsp;, I trust you could easily adapt the code here to your need.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;HTH&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2019 19:34:30 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/vba-forum/edit-attributes-of-existing-block/m-p/8784187#M5594</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2019-05-10T19:34:30Z</dc:date>
    </item>
    <item>
      <title>Re: Edit attributes of existing block</title>
      <link>https://forums.autodesk.com/t5/vba-forum/edit-attributes-of-existing-block/m-p/8784235#M5595</link>
      <description>&lt;P&gt;Ahh, see that wasn't so hard&amp;nbsp;&lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://forums.autodesk.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sub ChangeAtt()&lt;/P&gt;
&lt;P&gt;' Select the block by name&lt;BR /&gt;Dim ent As AcadEntity&lt;BR /&gt;Dim blk As AcadBlockReference&lt;BR /&gt;For Each ent In ThisDrawing.PaperSpace&lt;BR /&gt;If TypeOf ent Is AcadBlockReference Then&lt;BR /&gt;Set blk = ent&lt;BR /&gt;If UCase(blk.EffectiveName) = "B_BORD_ATTRIBUTES" Then&lt;/P&gt;
&lt;P&gt;Dim varAttributes As Variant&lt;BR /&gt;varAttributes = blk.GetAttributes&lt;BR /&gt;&lt;BR /&gt;' Change the value of the attributes. These will be fed data from Inventor iproperties.&lt;BR /&gt;varAttributes(0).TextString = "123654" 'PARTNUM&lt;BR /&gt;varAttributes(1).TextString = "0.25" 'THK&lt;BR /&gt;varAttributes(2).TextString = "a36" 'MATERIAL&lt;BR /&gt;varAttributes(3).TextString = "blk" 'FINISH&lt;BR /&gt;varAttributes(4).TextString = "tiny" 'WELD&lt;BR /&gt;varAttributes(5).TextString = "" 'TOLERANCE&lt;BR /&gt;varAttributes(6).TextString = "" 'PROD&lt;BR /&gt;varAttributes(7).TextString = "" 'DATE&lt;BR /&gt;varAttributes(8).TextString = "" 'DRAWN&lt;BR /&gt;varAttributes(9).TextString = "" 'CHECKED&lt;BR /&gt;varAttributes(10).TextString = "" 'PRODCODE&lt;BR /&gt;varAttributes(11).TextString = "" 'CLASSCODE&lt;BR /&gt;varAttributes(12).TextString = "" 'CLASSNO&lt;BR /&gt;varAttributes(13).TextString = "" 'WEIGHT&lt;BR /&gt;varAttributes(14).TextString = "" 'DDD_04&lt;BR /&gt;varAttributes(15).TextString = "" 'DDD_37&lt;BR /&gt;varAttributes(16).TextString = "" 'DDD_38&lt;BR /&gt;varAttributes(17).TextString = "" 'DDD_40&lt;BR /&gt;varAttributes(18).TextString = "None" 'SCALE&lt;BR /&gt;blk.Update&lt;BR /&gt;End If&lt;BR /&gt;End If&lt;BR /&gt;Next&lt;/P&gt;
&lt;P&gt;End Sub&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2019 19:59:57 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/vba-forum/edit-attributes-of-existing-block/m-p/8784235#M5595</guid>
      <dc:creator>snichols</dc:creator>
      <dc:date>2019-05-10T19:59:57Z</dc:date>
    </item>
  </channel>
</rss>

