HOW TO
This sample code performs the same functionality as the Switch Selection button in ArcMap.
Note:
Code in ThisDocument code module will only run in the current map document. To store the code in all map documents, open the Normal.mxt ThisDocument code module.
Code:
Sub SwitchSelection()
Dim pDoc As IMxDocument
Set pDoc = ThisDocument
Dim pMap As IMap
Set pMap = pDoc.FocusMap
'Get the Feature Selection
Dim pFLayer As IFeatureLayer
Set pFLayer = pMap.Layer(0)
Dim pFSel As IFeatureSelection
Set pFSel = pFLayer
'Get the set of selected features
Dim pSelFeats As ISelectionSet
Set pSelFeats = pFSel.SelectionSet
Debug.Print "Selected Features " & pSelFeats.Count
'Get a selection set of all features
Dim pAllFeats As ISelectionSet
Set pAllFeats = pFLayer.FeatureClass.Select(Nothing, _
esriSelectionTypeIDSet, esriSelectionOptionNormal, Nothing)
Debug.Print "Total Features " & pAllFeats.Count
'Switch the Selection
Dim pNewSel As ISelectionSet
pAllFeats.Combine pSelFeats, esriSetDifference, pNewSel
Debug.Print "New Selection " & pNewSel.Count
Set pFSel.SelectionSet = pNewSel
pDoc.ActiveView.Refresh
End Sub
Get help from ArcGIS experts
Download the Esri Support App