Hi @sebastien.forman, this code is doing something really interesting. It's not using the occurrence's active View Rep (oCompOcc.ActiveDesignViewRepresentation) to determine which one to activate next; it's using the occurrence's document's active View Rep (oCompOcc.Definition.RepresentationsManager.ActiveDesignViewRepresentation). It actually activating the next sequential View Rep from the one that's currently active within the occurrence's document. And it activate this View Rep for both the occurrence (Call oCompOcc.SetDesignViewRepresentation()) and the occurrence's document (Call oDesignVR(i).Activate).
Basically what it's doing is ignoring the active View Rep for the occurrence altogether, and just assuming the document's active View Rep (the one you would see if you right-clicked the occurrence and hit "Open") is the current View Rep for the occurrence (which may not be true). This circumvents the ComponentOccurrence.ActiveDesignViewRepresentation and ignores it altogether, thereby circumventing the null string issue.
On one hand, this is a bad idea, for two reasons: 1) It ignores the actual active View Rep for the occurrence (if it's set associative to one); and 2) If there are X occurrences of a given document, the code will step through them sequentially and each occurrence will have a different View Rep.
On the other hand, though, this is actually a really good idea as a way to decide which View Rep to activate and associate if one isn't currently.
What I would do is modify that code to have the following logic: If the occurrence "IsAssociativeToDesignViewRepresentation", then activate the next sequential View Rep for it; however, if it's NOT associative, activate the same View Rep that's currently active in the occurrence's document.
Unfortunately, this isn't as simple as it sounds, because you'll have to handle the fact that the Master view rep cannot be set associatively. For Assemblies, you probably want to just skip the Master view rep altogether, because it's usually just garbage. But for parts, you probably want to use it. You'll also need to take into account if an assembly ONLY has the Master view rep and no others. And you'll also need to handle the situation where the active View Rep for a part's document is "Master", in which case you'll want to active and associate to the next View Rep, if there is one. There just a bunch of little corner cases that you'll need to account for, unfortunately.
The following code should get you started in that process. It's in iLogic rather than VBA. This may not exactly match you intent, but hopefully it gets you closer.
Dim doc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oAsmCompDef As AssemblyComponentDefinition = doc.ComponentDefinition
' Get select set.
Dim sSet As SelectSet = ThisApplication.ActiveDocument.selectset
' Save select set to object collection so it can be re-selected after code completion.
Dim oCol As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
For Each oCompOcc In sSet
oCol.Add(oCompOcc)
Next
' Iterate through selected occurrences.
For Each obj As Object In oCol
If TypeOf obj Is ComponentOccurrence Then
Dim oCompOcc As ComponentOccurrence = obj
If oCompOcc.Visible = True Then
If oCompOcc.IsAssociativeToDesignViewRepresentation Then
' Activate the next sequential View Rep.
Dim occActiveVR As String = oCompOcc.ActiveDesignViewRepresentation
Dim docVRs As DesignViewRepresentations = oCompOcc.Definition.RepresentationsManager.DesignViewRepresentations
Dim i As Integer
For i = 1 To docVRs.Count
If docVRs(i).Name = occActiveVR Then
' Determine which view rep to activate next.
If i = docVRs.Count Then
' If a Part, go back to 1 ("Master"). If an assembly, skip Master (unless there are no others).
If oCompOcc.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then
i = 1
ElseIf oCompOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
If docVRs.Count >= 2 Then
i = 2
Else
i = 1
End If
End If
Else
i += 1
End If
' Determine whether to set Associative.
Dim assoc As Boolean
If i = 1 Then
assoc = False ' ("Master" cannot be set associatively).
Else
assoc = True
End If
' Activate View Rep.
oCompOcc.SetDesignViewRepresentation(docVRs(i).Name, , assoc)
Exit For
End If
Next
Else
' Activate an arbitrarily-chosen View Rep. In this case, choose the document's active View Rep.
' NOTE: Need to fix this to handle if the document is a Part, in which case activate the next View Rep after Master, if it exists.
Dim docActiveVR As String = oCompOcc.Definition.RepresentationsManager.ActiveDesignViewRepresentation.Name
oCompOcc.SetDesignViewRepresentation(docActiveVR, , True)
End If
End If
End If
Next
ThisApplication.ActiveDocument.selectset.SelectMultiple(oCol)