changing levels visibility on multiple models in a DGN file

Dear VBA Experts,

I am trying to write some codes which will help me batch process levels visibility on multiple models in several DGN files. The codes I have written is posted below. My codes work perfectly fine when I open the DGN and activate each models at least once. But as a part of the batch process this code only works on the active model but throws error when I refer to other models in the DGN file. I come to realize that in non-active models the references are not loaded. How can I edit this code so I can tell MicroStation to load all references so that the codes will find whats it looking for. In this case the levels in the referenced file.

I tried activating models before it moves on to next model but with no luck. It breaks with an error message saying the reference index were not found.  Any help or thoughts would be great.



Option Explicit
Private Declare Function mdlModelRef_loadReferenceModels Lib "stdmdlbltin.dll" (ByVal modelRef As Long, _
ByVal loadCache As Long, ByVal loadRasterRefs As Long, ByVal loadUndisplayedRefs As Long) As Long
Sub ControlLayerVisibility()

'On Error Resume Next

Dim myDGN As DesignFile
Set myDGN = Application.ActiveDesignFile

Dim myActiveModel As ModelReference
Dim oAtt As Attachment

'Determine the sheet model with msdModelTypeSheet property
Dim modelCount As Integer
Dim myModel As String
Dim tempString As String
Dim myView As View

For modelCount = 1 To myDGN.Models.Count
If myDGN.Models(modelCount).Type = msdModelTypeSheet Then
tempString = myDGN.Models(modelCount).Name
Dim myLevel As String
Dim myModelLogicalName As String
Dim myAttachmentIndex As Integer
Dim i As Integer

If InStr(1, tempString, "RECORD") > 0 Then
myModel = myDGN.Models(modelCount).Name
'Determine the Attachment Index and run the levels.
' For i = 1 To myDGN.Models(myModel).Attachments.Count
' 'Debug.Print myDGN.Models(myModel).Attachments(i).AttachName
' If myDGN.Models(myModel).Attachments(i).AttachName = "50014746_TB 30x42.dgn" Then
' myAttachmentIndex = i
' myModelLogicalName = myDGN.Models(myModel).Attachments(myAttachmentIndex).AttachName
' End If
' Next
Set myActiveModel = myDGN.Models(myModel)
Set myActiveModel = myActiveModel.DesignFile.Models("50014746_TB 30x42.dgn")
mdlModelRef_loadReferenceModels myActiveModel.MdlModelRefP, 1, 1, 1

Set oAtt = myActiveModel.Attachments("default")
'If myActiveModel.IsReadOnly = True Then
For Each myView In myDGN.Views
oAtt.Levels("G-ANNO-TTLB-NO AS BUILT").IsDisplayed = True
oAtt.Levels("G-ANNO-TTLB-NO AS BUILT").IsDisplayedInView(myView) = True
oAtt.Levels("G-ANNO-TTLB-AS BUILT").IsDisplayed = False
oAtt.Levels("G-ANNO-TTLB-AS BUILT").IsDisplayedInView(myView) = False
' myDGN.Models(myModel).Attachments(myAttachmentIndex).Levels("G-ANNO-TTLB-NO AS BUILT").IsDisplayed = True
' myDGN.Models(myModel).Attachments(myAttachmentIndex).Levels("G-ANNO-TTLB-NO AS BUILT").IsDisplayedInView(myView) = True
' myDGN.Models(myModel).Attachments(myAttachmentIndex).Levels("G-ANNO-TTLB-AS BUILT").IsDisplayed = False
' myDGN.Models(myModel).Attachments(myAttachmentIndex).Levels("G-ANNO-TTLB-AS BUILT").IsDisplayedInView(myView) = False
'myLevel = "LEVEL SET DISPLAY ON file:" & myModelLogicalName & " """ & Row.Value & """"
Next myView
'End If
'Close the file appropriately

CadInputQueue.SendCommand "FILEDESIGN"
CadInputQueue.SendCommand "SAVE DESIGN"

End If

End If


End Sub