ArcObjects Library Reference  (Carto)    

IContentsView, SelectedItem Property Example

[Visual Basic 6.0]
This macro should be run from VBA inside ArcMap. You can either run the macro from the Macros dialog, or follow the steps below.

    1. Paste the routine below into the ThisDocument module in an ArcMap project which contains at least one Map (The default Map is called Layers).

    2. Open the Customize dialog by right-clicking a toolbar in ArcMap. Select the ContextMenus option in the Toolbars tab, and in the ContextMenus toolbar which appears, navigate to the Map View context menu.

    3. Go to the Commands tab in the Customize dialog, and select the Macros category. Click and drag the RenameDataFrame macro to wherever you like on the Map View context menu.

    4. Dismiss the customize dialog.

    5. To run the macro, select a Map in the Table of contents, right-click, and then enter a new name for the Map.


     

    '--------------------------------------------

     

    Public Sub RenameDataFrame()
      On Error GoTo ErrorHandler
     
      '
      ' Get current document.
      '
      Dim pMxDoc As IMxDocument
      Set pMxDoc = ThisDocument
      '
      ' Get the contents view.
      '
      Dim pContView As IContentsView
      Set pContView = pMxDoc.CurrentContentsView
      '
      ' If we have a DisplayView active
      '
      If TypeOf pContView Is TOCDisplayView Then
        If IsNull(pContView.SelectedItem) Then
          '
          ' If we dont have a Map selected then we cant rename it.
          '
          Err.Raise 94, , "SelectedItem is Null" & vbNewLine & "Select a Map in the Table of Contents to rename"
        End If
        '
        ' Get the selected Item.
        '
        Dim pVarSelectedItem As Variant
        Set pVarSelectedItem = pContView.SelectedItem
        '
        ' Selected Item should be a Map coclass if we are running this macro from
        ' the context menu of the Map.
        '
        Dim pCurrName As String
        If TypeOf pVarSelectedItem Is IMap Then
          '
          ' Check name of Map.
          '
          Dim pMap As IMap
          Set pMap = pVarSelectedItem
         
          pCurrName = pMap.Name
        '
        ' If the Selected Item returned was not a map, possibly we are running this macro
        ' from an inappropriate place.
        '
        Else
          Err.Raise 425, , "Cannot change Map name as a single Map" & vbNewLine & "is not selected in the Table of Contents"
        End If
        '
        ' Ask user to change the name.
        '
        Dim pNewName As String
        pNewName = InputBox("Enter a new name below:", "Rename Map", pCurrName)
        '
        ' Check we have a name.
        '
        If pNewName <> "" Then
          pMap.Name = pNewName
          pContView.Refresh (pVarSelectedItem)
        End If
      End If
     
    Exit Sub
    ErrorHandler:
      Select Case Err.Number
        Case Is <> 0
          MsgBox "Error: " & Err.Description, vbCritical, "Error " & Err.Number
          Exit Sub< BR >   End Select < BR >   Err.Clear < BR >   Resume Next < BR > End Sub

    [Visual Basic .NET, C#, C++]
    No example is available for Visual Basic .NET, C#, or C++. To view a Visual Basic 6.0 example, click the Language Filter button Language Filter in the upper-left corner of the page.