Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have some code from 9.3 that would check the CurrentContentsView.ContextItem to ensure that the TOC was in Display (or Drawing Order) view. If not it would set it to Display via:

If Not pDoc.CurrentContentsView.ContextItem = 1 Then
    Set pDoc.CurrentContentsView.ContextItem = 1
    pDoc.ContentsView(1).Refresh Nothing
End If
pDoc.ContentsView(1).Refresh Nothing

Now that the TOC is a DockableWindow (which I can access) and there are several IContentsViews, this no longer works. It's surprising that this is impossible to find in the forums and the VBA SDK as as brief and cryptic as could be.

How do I access the ContextItem (which seems stuck at NULL) to set it? Also, is the fact that "currentContentsView" automatically reverts to a lower-case "c" when the object apparently shouldhave an upper-case "C" significant?

Any help would be MUCH appreciated. For some reason, my code runs 3x faster in Display View and I want to automatically set it if the user forgets (or doesn't know).

Oh...and yes, I AM aware of VBA's demise and the existence of Python. Noted.

Thanks in advance.

share|improve this question

This add-in code example (not in VBA format) will check contents view and change to display view (List By Drawling Order).

   Protected Overrides Sub OnClick()


         My.ArcMap.Application.CurrentTool = Nothing
    End Sub

    Private Sub ChangeArcMapTOC_ContentsView(ByVal newContentsViewName As String)

        Dim app As IApplication = CType(Hook, IApplication)
        Dim mxdoc As IMxDocument = CType(My.ArcMap.Application.Document, IMxDocument)

        If mxdoc.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 mxdoc.ContentsViewCount - 1

            pContentView = mxdoc.ContentsView(i)

            If pContentView.Name = newContentsViewName Then

                bFound = True

                Exit For

            End If


        If bFound = False Then

            MessageBox.Show("Contents view '" & newContentsViewName & "' does not exist", "Could not change contents view")

            Exit Sub


            mxdoc.CurrentContentsView = pContentView

        End If


        Exit Sub

    End Sub
share|improve this answer ususal. – Dan Shaffer Feb 27 '12 at 18:28
OK that auto-submit on Enter is killing me! As usual...I missed the simplest possible way to do something! Thanks for getting me pointed back in the right direction. On another I use the ContentsView3 to update the selected button (List by Drawing Order vs. Source)? This script doesn't do that. But it was immensely helpful. Thanks! – Dan Shaffer Feb 27 '12 at 18:31
Interesting...never mind. When inserted into my code and run, the button DOES update after a second or two...maybe I needed a few refreshes or something. Again...many thanks! – Dan Shaffer Feb 27 '12 at 18:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.