Is this code going to be used exclusively within iLogic rules, or will it be used within something like an Inventor Add-In, or something like a standalone external EXE type application, or 'other'? In my example, and your fist edited version of my example, within the first line of code, we are using the "ThisDoc.Document" code phrase. That is unique to the iLogic add-in, within Inventor, and will only work within iLogic rules. The 'ThisDoc' term being used there is very dynamic, and works differently in different situations, so that it is referring to the most appropriate source. It is what is known as a Rule Object, and is essentially a variable that has been assigned a value that is a ICadDoc Interface. I can't explain 100% about how it works differently in different situations here in this reply, but it is very interesting and useful. If your code will be used anywhere else, then you may have to switch that to something like Application.ActiveDocument or Application.ActiveEditDocument, or something like that, which are Inventor API based, instead of uniquely iLogic based. Everything else in those first couple example codes is entirely Inventor API based, and not unique to iLogic.
If you have an assembly open, then your code is working with other, referenced documents, or perhaps creating other documents, the assembly will likely remain the 'active' document, because it was the 'active' one when the code process started. Every Document type object does have an 'Activate' method, which is supposed to force it to become the 'active' document, and visibly show on your screen, with its own Document tab, but that does not always work in every situation. And any code that either runs remotely, or due to events happening, or is interacting with 'other' documents, the way you refer to, and work with those documents in your code is extremely important, maybe even the most important thing.
Below is another version of that example, which is trying to use the 'active' document. Not sure if that will work for you. If not, try making the 'Sub Main' into its own custom Sub routine within your overall / larger code based project, which accepts a DrawingDocument type input variable, then uses that instead of trying to use the 'active' document. Also, it is 'activating' each sheet as it iterates through them. That may not be necessary, and may not even work in some situations, so you can try commenting or uncommenting that line of code, to see if it changes things for you.
Sub Main
Dim oInvApp As Inventor.Application = ThisApplication
Dim oDDoc As DrawingDocument = TryCast(oInvApp.ActiveDocument, Inventor.DrawingDocument)
If oDDoc Is Nothing Then Return 'may want a feedback message here
'start iterating all sheets
For Each oSheet As Inventor.Sheet In oDDoc.Sheets
'not usually necessary, but depends on what you are doing
oSheet.Activate() '<<< MAYBE NOT BE NEEDED >>>
'start iterating all views on that sheet
For Each oView As DrawingView In oDDoc.ActiveSheet.DrawingViews
IncludeFlatPatternSketchesInView(oView)
Next 'oView
oSheet.Update()
Next 'oSheet
oDDoc.Update2(True)
End Sub
Sub IncludeFlatPatternSketchesInView(oDView As Inventor.DrawingView)
If (oDView Is Nothing) OrElse (Not oDView.IsFlatPatternView) Then Exit Sub
Dim oSheet As Inventor.Sheet = oDView.Parent
Dim oDDoc As DrawingDocument = oSheet.Parent
If Not oDDoc.IsModifiable Then Exit Sub
Dim oSMDoc As PartDocument = oDView.ReferencedDocumentDescriptor.ReferencedDocument
Dim oSMDef As SheetMetalComponentDefinition = oSMDoc.ComponentDefinition
Dim oFlatPattern As Inventor.FlatPattern = oSMDef.FlatPattern
For Each oPSketch As PlanarSketch In oFlatPattern.Sketches
If Not oPSketch.Visible Then Continue For
Try
oDView.SetIncludeStatus(oPSketch, True)
Catch ex As Exception
Logger.Error("Error including sketch named '" & oPSketch.Name & "'," _
& vbCrLf & "into view named '" & oDView.Name _
& vbCrLf & ex.Message)
End Try
Next 'oPSketch
End Sub
Wesley Crihfield

(Not an Autodesk Employee)