Dim oAssyDoc As AssemblyDocument Dim oAssyCompDef As AssemblyComponentDefinition Dim oPartDoc As PartDocument Dim oPartCompDef As PartComponentDefinition Public Sub Hide() Call Initialize Dim oVisSurfs() As WorkSurface Dim tempWkSurf As WorkSurface Dim lngNumSurf As Long Dim lngTempCount As Long lngTempCount = 1 lngNumSurf = oPartCompDef.WorkSurfaces.Count ReDim oVisSurfs(lngNumSurf) For Each tempWkSurf In oPartCompDef.WorkSurfaces If tempWkSurf.Visible = True Then Set oVisSurfs(lngTempCount) = tempWkSurf 'oVisSurfs(lngTempCount).Visible = False lngTempCount = lngTempCount + 1 End If Next Call SwapVis(oVisSurfs()) End Sub Public Sub SwapVis(visSurfs As Variant) Dim tempSurf As WorkSurface For Each tempSurf In visSurfs If tempSurf.Visible = True Then tempSurf.Visible = False Else: tempSurf.Visible = True End If Next End Sub Public Sub Initialize() Set oPartDoc = ThisApplication.ActiveDocument Set oPartCompDef = oPartDoc.ComponentDefinition End Sub
I have this code to swap the visibility of surfaces in the model. Essentially shutting all off and on based on their current status. However I only want to swap the surfaces that are currently visible, not any in hiding.
I get a Run-Time error '424': Object required error when the program reaches
For Each tempSurf In visSurfs
I'm not sure what's wrong.
There are a few of things not quite right because of VBA behavior. The first is your resizing of the array. Let's say there are 5 work surfaces. The Redim statement below will create an array for 6 items. In VBA (and VB) when you declare the size of the array you're not specifying the number of elements but instead the index of the last element. Because by default the first item is 0 you end up with one more than you think you should.
ReDim oVisSurfs(lngNumSurf)
So instead of the line above you can just do this:
ReDim oVisSurfs(lngNumSurf -1)
and you'll need to initialize lngTempCount to 0 instead of 1.
lngTempCount = 0
The next issue is that VBA doesn't support using a For Each statement to iterate over the contents of an array, (VB does). So instead of For Each you'll need to use a For To as shown below. Notice that I also changed the declaration of the Sub.
Public Sub SwapVis(visSurfs() As WorkSurface)
Dim tempSurf As WorkSurface
Dim i As Integer
For i = 0 To UBound(visSurfs)
Set tempSurf = visSurfs(i)
If tempSurf.Visible = True Then
tempSurf.Visible = False
Else: tempSurf.Visible = True
End If
Next
End Sub
When calling SwapVis it should be like below:
Call SwapVis(oVisSurfs)