1

Refactoring VBA to VB.NET and most parts of the code is working expect for the part where I want to show the layername with today's date in the layout mode (pTextElement.Text)

Any ideas why the text is not showing up?

Public Sub PlotTag()

        Dim x As Long
        Dim y As Long

        Dim pMxDoc As IMxDocument
        Dim pActiveView As IActiveView
        Dim pGraphicsContainer As IGraphicsContainer
        Dim pLayer As ILayer
        Dim pMaps As IMaps
        Dim pMap As IMap

        pMxDoc = My.Document

        If Not pMxDoc.ActiveView Is pMxDoc.PageLayout Then Exit Sub

        pActiveView = pMxDoc.ActiveView
        pLayer = pMxDoc.SelectedLayer

        If pLayer Is Nothing Then
        MsgBox("Please choose a layer!")
            Exit Sub
        End If

        pMaps = pMxDoc.Maps
        pMap = pActiveView.FocusMap
        Dim strLayerName As String
        Dim strMapName As String
        strLayerName = pLayer.Name  
        strMapName = pMap.Name 

        MsgBox("Data Frame is: " & pMap.Name & " " & pLayer.Name)

        Dim pTextElement As ITextElement
        pTextElement = New TextElement
        Dim dteDate As Date
        dteDate = Today
        pTextElement.Text = "Layout" & strLayerName & ". Plotted " & dteDate & "."

        Dim pElement As IElement
        pElement = pTextElement
        pGraphicsContainer = pMxDoc.PageLayout
        Dim pGraphicsContainerSelect As IGraphicsContainerSelect
        Dim pGraphicsContainerProp As IGraphicsContainerProperty

        pGraphicsContainerSelect = pGraphicsContainer
        pGraphicsContainerSelect.UnselectAllElements()

        pElement.Geometry = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y)
        pGraphicsContainer.AddElement(pTextElement, 0)
        pGraphicsContainerSelect.SelectElement(pTextElement)

        Dim pPageLayoutGraphicsSelect As IGraphicsContainerSelect
        pPageLayoutGraphicsSelect = pMxDoc.PageLayout
        pPageLayoutGraphicsSelect.UnselectAllElements()
        pMxDoc.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, Nothing, Nothing)
    End Sub
1

I think your problems stem from this part of the code:

If Not pMxDoc.ActiveView Is pMxDoc.PageLayout Then Exit Sub
pMaps = pMxDoc.Maps
pMap = pActiveView.FocusMap
Dim strLayerName As String
Dim strMapName As String
strLayerName = pLayer.Name 

You get pMap from the focusmap but you have exited the procedure in the previous line if it was not in page layout, so the active view is the pagelayout which does not have a focusmap. I would replace that line with pMap = pMaps.Item(0).

You then attempt to get the layer name strLayerName = pLayer.Name. You have declared pLayer but you never set it. I would expect your code to be failing here before it ever gets to the pElement section.

Similarly you declare X and Y but never set them.

  • Thanks for your comment! I see now that I forgot a few lines of codes (as I didn't want to add unrelevant code). I added the lines now to my original question. So should I replace the line pMap = pActiveView.FocusMap to pMap = pMaps.Item(0)? – Anna3anana Aug 1 at 12:05
  • Yes replace that line to get a handle on the Map. – Hornbydd Aug 1 at 14:30
  • Replacing the pMap = pActiveView.Focus to pMap = pMaps.Item(0) did unfortunately not work. It works up to the code MsgBox("Data Frame is: " & pMap.Name & " " & pLayer.Name), as it show's the correct pMap.Name and pLayer.Name, but it's still not plotting it on the layout. :( – Anna3anana Aug 21 at 11:59
  • 1
    You need to amend your question with your new code as maybe you have introduced a new error? – Hornbydd Aug 21 at 12:55
  • Thanks! The above code actually worked, so I will create a new question if I can't find the solution (to click on a location and add text elements). Thanks for the help though @hornbydd ! – Anna3anana Aug 22 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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