Here's my code for block attributes modification on side database. In Vb but close to what you've shown...
Public Shared Sub UpdateAttributesInSideDatabase(ByVal FilePath As String, blockName As String, attbName As String, attbValue As String)
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim currentDb As Database = doc.Database
Dim ed As Editor = doc.Editor
Using targetDb As New Database(False, True)
Try
targetDb.ReadDwgFile(FilePath, System.IO.FileShare.ReadWrite, True, "")
Using tr As Transaction = targetDb.TransactionManager.StartTransaction()
'' Lock the new document
Using acLckDoc As DocumentLock = doc.LockDocument()
Dim layoutDict As DBDictionary = tr.GetObject(targetDb.LayoutDictionaryId, OpenMode.ForRead)
For Each dicEnt As DBDictionaryEntry In layoutDict
Dim lay As Layout = CType(dicEnt.Value.GetObject(OpenMode.ForRead), Layout)
Dim layname As String = lay.LayoutName
''do your stuffs in every layout
If layname <> "Model" Then
Dim btr As BlockTableRecord = tr.GetObject(lay.BlockTableRecordId, OpenMode.ForRead)
For Each id As ObjectId In btr
Dim ent As Entity = TryCast(tr.GetObject(id, OpenMode.ForRead), Entity)
If ent IsNot Nothing Then
Dim br As BlockReference = TryCast(ent, BlockReference)
If br IsNot Nothing Then
Dim bd As BlockTableRecord = DirectCast(tr.GetObject(br.BlockTableRecord, OpenMode.ForRead), BlockTableRecord)
' ... to see whether it's a block with
' the name we're after
If bd.Name.ToUpper() = blockName.ToUpper() Then
For Each arId As ObjectId In br.AttributeCollection
Dim obj As DBObject = tr.GetObject(arId, OpenMode.ForRead)
Dim ar As AttributeReference = TryCast(obj, AttributeReference)
If ar IsNot Nothing Then
' ... to see whether it has
' the tag we're after
If ar.Tag.ToUpper() = attbName.ToUpper() Then
' If so, update the value
' and increment the counter
ar.UpgradeOpen()
ar.TextString = attbValue
'realign attributes after editing their values
ar.AdjustAlignment(targetDb)
ar.DowngradeOpen()
End If
End If
Next
End If
End If
End If
Next
End If
Next
End Using
'end of code from template insert
tr.Commit()
End Using
HostApplicationServices.WorkingDatabase = targetDb
targetDb.SaveAs(FilePath, DwgVersion.Current)
Catch ex As Autodesk.AutoCAD.Runtime.Exception
ed.WriteMessage(vbLf & "Error while running > " + ex.Message)
Finally
HostApplicationServices.WorkingDatabase = currentDb
End Try
End Using
End Sub
I've posted some picture to shown what I mean... The first picture show the updated attributes kind of left aligned, even if justification is set to "Center". The second pics show what happen after i've re-selected "Center" & applied the changes...