Edit attribute of all sheet body in an assembly

I've been trying to write a journal that searches all sheet bodies in an assembly with a specific attribute (material) and value (steel) and than changes the value of an other attribute (density).
Here is what I got so far but I really can't get it to work.
I found an example of the "setuserattribute" method on this forum but don't know how make it work for sheetbodies in stead of parts.
Any ideas?

Many thanks for your help!!

Option Strict Off
Imports System
Imports System.Collections
Imports NXOpen

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
If IsNothing(theSession.Parts.BaseWork) Then
'active part required
Return
End If

Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

Const SA_MATERIAL As String = "SA_MATERIAL"
Const A_32 As String = "A_32"
Const SA_DESITY As String = "SA_DENSITY"
Const DENSITY As String = "0"

For Each solid As Body In workPart.Bodies
If solid.HasUserAttribute(SA_MATERIAL, NXObject.AttributeType.String, A_32)
then solid.SetUserAttribute(SA_DENSITY, -1, Not (solid.SetUserAttribute(attributeName, DENSITY)), Update.Option.Now)
End If
Next

lw.Close()

End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer

'Unloads the image immediately after execution within NX
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

End Function

End Module

I would suggest that you start by recording a journal while assigning an attribute to a sheet body. The journal recorder will give you a framework of code to start with. It will actually give you more than you need; I'd suggest cutting out the cruft and turning the rest into a subroutine. You can then reuse the subroutine code in your other journals.

This is what I've got so far, it works but because I used a recorded journal it probably contains a lot of unnecessary code.
Any suggestions on how to simplify my journal?

Option Strict Off

Imports System
Imports System.Collections.Generic
Imports NXOpen

Module Module33
Sub Main()

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work

Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

Dim theBodies As New List(Of Body)

Const attributeTitle As String = "SA_MATERIAL"
Dim attributeValue As String
Dim targetValue As String = "A_32"

'gather the sheet bodies from the work part
For Each tempBody As Body In workPart.Bodies
If tempBody.IsSheetBody

If tempBody.HasUserAttribute(attributeTitle, NXObject.AttributeType.String, -1) Then

theBodies.Add(tempBody)
lw.WriteLine("test")
End If
End if
Next

For Each tempBody As Body In theBodies
'***************************************

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")

Dim objects1(0) As NXObject
Dim body1 As Body = tempBody

objects1(0) = body1
Dim attributePropertiesBuilder1 As AttributePropertiesBuilder
attributePropertiesBuilder1 = theSession.AttributeManager.CreateAttributePropertiesBuilder(workPart, objects1, AttributePropertiesBuilder.OperationType.None)

Dim objects2(0) As NXObject
objects2(0) = body1
Dim objectGeneralPropertiesBuilder1 As ObjectGeneralPropertiesBuilder
objectGeneralPropertiesBuilder1 = workPart.PropertiesManager.CreateObjectGeneralPropertiesBuilder(objects2)

Dim selectNXObjectList1 As SelectNXObjectList
selectNXObjectList1 = objectGeneralPropertiesBuilder1.SelectedObjects

objectGeneralPropertiesBuilder1.NameLocationSpecified = False

objectGeneralPropertiesBuilder1.Index = 1

Dim objects3(0) As NXObject
objects3(0) = body1
attributePropertiesBuilder1.SetAttributeObjects(objects3)

theSession.SetUndoMarkName(markId1, "Sheet Body Properties Dialog")

Dim selectNXObjectList2 As SelectNXObjectList
selectNXObjectList2 = objectGeneralPropertiesBuilder1.SelectedObjects

attributePropertiesBuilder1.Title = "SA_DENSITY"

attributePropertiesBuilder1.StringValue = "0"

Dim nXObject1 As NXObject
nXObject1 = attributePropertiesBuilder1.Commit()

Dim markId3 As Session.UndoMarkId
markId3 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")

Dim objects4(0) As NXObject
objects4(0) = body1
Dim attributePropertiesBuilder2 As AttributePropertiesBuilder
attributePropertiesBuilder2 = theSession.AttributeManager.CreateAttributePropertiesBuilder(workPart, objects4, AttributePropertiesBuilder.OperationType.None)

attributePropertiesBuilder2.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.String

Dim objects5(0) As NXObject
objects5(0) = body1
Dim objectGeneralPropertiesBuilder2 As ObjectGeneralPropertiesBuilder
objectGeneralPropertiesBuilder2 = workPart.PropertiesManager.CreateObjectGeneralPropertiesBuilder(objects5)

Dim selectNXObjectList3 As SelectNXObjectList
selectNXObjectList3 = objectGeneralPropertiesBuilder2.SelectedObjects

Dim objects6(0) As NXObject
objects6(0) = body1
attributePropertiesBuilder2.SetAttributeObjects(objects6)

Dim selectNXObjectList4 As SelectNXObjectList
selectNXObjectList4 = objectGeneralPropertiesBuilder2.SelectedObjects

Dim markId4 As Session.UndoMarkId
markId4 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Sheet Body Properties")

theSession.DeleteUndoMark(markId4, Nothing)

Dim markId5 As Session.UndoMarkId
markId5 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Sheet Body Properties")

Dim id2 As Session.UndoMarkId
id2 = theSession.GetNewestUndoMark(Session.MarkVisibility.Visible)

Dim nErrs2 As Integer
nErrs2 = theSession.UpdateManager.DoUpdate(id2)

theSession.DeleteUndoMark(markId5, Nothing)
theSession.SetUndoMarkName(id2, "Sheet Body Properties")
attributePropertiesBuilder2.Destroy()
objectGeneralPropertiesBuilder2.Destroy()
theSession.CleanUpFacetedFacesAndEdges()

'**************************

Next

End Sub

End Module

When looking for ways to clean up recorded code, I like to start at the bottom and work up. If a variable is created and given a value but never referenced again in the code, it is a good candidate for deletion. For example, selectNXObjectList4 is created and assigned a value, but never referenced again in the code. In other words, it has no effect on the operation. Comment out the two lines referencing the selectNXObjectList4 variable and test your code again to see if there are any side effects. If the code runs correctly, delete those lines and look for another candidate to remove.

Look for builder objects that are created but never used. For instance, attributePropertiesBuilder2 is created and various properties are set, but the .Commit method is never called. Comment out the lines referencing attributePropertiesBuilder2 and test for side effects.

The journal recorder likes to pepper in extra undo marks. Comment these out and test your code. Most of the time they can be removed without any ill effects.

Thanks for your advice!