HOW TO
The following code shows how to change focus between one ArcMap Table of Contents (TOC) view and another. The standard views include Data and Source, but this code will also work for custom TOC views.
Note:
Code in the Project's ThisDocument code module will only run in the current map document. If you want to store the code in all your map documents, open the Normal.mxt ThisDocument code module instead.
Code:
Public Sub test()
' change to "Display" if you wish to switch to the Display tab.
ChangeArcMapTOC_ContentsView ("Source")
End Sub
Private Sub ChangeArcMapTOC_ContentsView(newContentsViewName As String)
On Error GoTo cantChangeView
' exit if trying to change to the current view.
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
If pMxDoc.CurrentContentsView.Name = newContentsViewName Then
Exit Sub
End If
' loop thru all ContentsViews to see if newContentsViewName exists.
Dim i As Integer
Dim pContentView As IContentsView
Dim bFound As Boolean
For i = 0 To pMxDoc.ContentsViewCount - 1
Set pContentView = pMxDoc.ContentsView(i)
If pContentView.Name = newContentsViewName Then
bFound = True
Exit For
End If
Next
If bFound = False Then
MsgBox "Contents view '" & newContentsViewName & "' does not exist", _
vbExclamation, "Could not change contents view"
Exit Sub
Else
Set pMxDoc.CurrentContentsView = pContentView
End If
Exit Sub
cantChangeView:
MsgBox Err.Description, vbExclamation, "Could not change contents view"
Exit Sub
End Sub
Get help from ArcGIS experts
Download the Esri Support App