Legend Button - VBA - A Problem... Sort of

Discussion created by kevinbg07 on Jun 9, 2010

Here's the story: I'm programming a button via VBA to call up a legend that I've set up. It works fine and calls the legend as I want it, though I get this error (see attached PNG). It says "Run-Time Error '91': Object variable or With block variable not set". I'm posting the code as well so that you might be able to figure this out with me. Any help is greatly appreciated.

Kevin Laughlin
Wood County Auditor
Bowling Green, OH


Private Sub Legend_Click()
  Dim pMxDoc As IMxDocument
  Dim pActiveView As IActiveView
  Dim pEnv As IEnvelope
  Dim pID As New UID
  Dim pMapSurround As IMapSurround
  Dim pMapSurFrame As IMapSurroundFrame
  Dim pElement As IElement
  Dim pLegend As ILegend
  Dim pLegendFormat As ILegendFormat
  Dim pLegendItem As ILegendItem
  Set pMxDoc = Application.Document
  Set pActiveView = pMxDoc.PageLayout
  Set pEnv = New Envelope
  pEnv.PutCoords 7.5, 0.2, 8.5, 4
  pID.Value = "esriCore.Legend"
  Set pMapSurround = CreateSurround(pID, pEnv, "Legend", pMxDoc.FocusMap, pMxDoc.PageLayout)
  Set pElement = pMapSurFrame
  Set pLegend = pMapSurround
  With pLegend
    .RemoveItem (4)
    .RemoveItem (6)
    .Title = "Features"
  End With
  Set pLegendFormat = pLegend.Format
  With pLegendFormat
    .TitlePosition = esriTopSide
    .HeadingGap = 10
    .GroupGap = 3
    .HeadingGap = 3
    .TitleSymbol.Font.Underline = True
  End With
  Set pLegend.Format = pLegendFormat
  pActiveView.PartialRefresh esriViewGraphics, pMapSurFrame, pEnv
    Dim l As Long
    For l = 0 To pMxDoc.FocusMap.MapSurroundCount - 1
        If TypeOf pMxDoc.FocusMap.MapSurround(l) Is ILegend Then
            Dim pGC As IGraphicsContainer
            Set pGC = pMxDoc.PageLayout
            Dim pMSF As IMapSurroundFrame
            Set pMSF = pGC.FindFrame(pMxDoc.FocusMap.MapSurround(l))
            Dim pSBG As ISymbolBackground
            Set pSBG = New SymbolBackground
            pSBG.FillSymbol = MakeFillSymbol(vbWhite)
            pMSF.Background = pSBG
            Dim pAV As IActiveView
            Set pAV = pGC
            pAV.PartialRefresh esriViewGraphics, pMSF, Nothing
        End If
    Next l
End Sub