Delete All From Layer At The Assembly Level

I have a journal written that when run will ask the user to input a layer number and then delete all objects from that layer (based on code from this site). The journal works fine in a single assembly. The problem I am running into is when I run the journal in a file with multiple sub-assemblies it will only delete objects from the layer in the top most level (Objects on the selected layer in subs are not deleted). Any suggestions?

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpenUI

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim theUISession As UI = UI.GetUI
Dim answer As String = ""
Dim myInt As Integer

Do
answer = NXInputBox.GetInputString("Enter a layer number", "Layer To Be Deleted?", " ")
'if cancel is pressed, exit sub
If answer = "" Then Exit Sub
Loop Until Integer.TryParse(answer, myInt)

theUISession.NXMessageBox.Show("Delete From Layer", NXMessageBox.DialogType.Information, "All objects will be deleted from layer: " & myInt.ToString)

'&&&&& layer number to delete objects from
Dim layerToDelete As Integer = myInt

If displayPart Is Nothing Then
'no part to work on
Exit Sub
End If

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

Dim notifyOnDelete1 As Boolean
notifyOnDelete1 = theSession.Preferences.Modeling.NotifyOnDelete

theSession.UpdateManager.ClearErrorList()

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.AddToDeleteList(workPart.Layers.GetAllObjectsOnLayer(layerToDelete))

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

End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer

'Unloads the image when the NX session terminates
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

End Function

End Module

The code, in its current form, only affects the "work part" which may or may not be the top level assembly. From the description, it sounds like you want it to process all the files that have at least one component, but do not want it to affect piece part files (parts with no components). Below is a link to an article that shows how to process all the components of an assembly:
http://nxjournaling.com/content/creating-subroutine-process-all-componen...

There are comments in the "reportComponentChildren" subroutine that pertain to piece parts and assemblies. You can add the code to delete the objects to the "this is a subassembly..." section of the code. Or, perhaps a better/cleaner way would be to create a subroutine (or function) to delete the objects from a specified layer and call that subroutine from the appropriate place in the "reportComponentChildren" subroutine.

I tried changed the "work part" to "displayed part" and got the same results.
How would I process the assembly if I am looking for objects not components? If I am trying to delete all objects from a layer across multiple components would I need to have the journal cycle through all parts as work parts as to get all of the objects?

In the code of the previous link you will have a reference to a component, the "child" variable, each time through the for loop. You can get the part from the component variable:

dim thePart as Part
thePart = child.Prototype.OwningPart

Once you have a reference to the part object, you can manipulate the objects in that part file (i.e. deleting objects from specific layers). Instead of referencing the "workPart" variable, you would reference the "thePart" variable (or whatever you named it).

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.AddToDeleteList(thePart.Layers.GetAllObjectsOnLayer(layerToDelete))

Note that the components may need to be fully loaded to successfully get the part from the component.