ArcPy ArcMap – Delete MS Word-Object in DataView with ArcPy

arcmaparcpydata-frameelementole

My mxd has a dataframe which contains a MS Word-Object ("Insert" – "Object").
The Object was added to data view.

I want to access the MXD dataframe in PyScripter using

arcpy.mapping.ListDataFrames(mxd)[0]

However, I get the following error:

AttributeError: 'NoneType' object has no attribute 'dataFrames'

I'd also like to import the MXD to ArcGIS Pro but it doesn't work, I get an error.

When I delete the Word-Object all these errors do not occur.

(Note: When I use the Python window in ArcMap, the AttributeError does not occur. However, I would like to make it work in PyScripter)

I thought about deleting the Word-Object with ArcPy, is that possible? I have hundreds of MXDs with this Word-Object, so I need to automate this.

I already tried using ListLayoutElements but nothing is returned because the object was added in data view.

Best Answer

I don't think arcpy exposes any methods that allow you to delete OLE objects inserted into a data frame. You can automate this using ArcObjects.

So in screen shot below I have a polyline layer, graphic and an OLE word document in a map view (so NOT layout).

Screen shot

The following VBA code will find any OLE frame and delete it:

Sub test()
    ' This code demonstrates how to find OLE Frames in map and delete them
    ' This code does not save the map to make changes permanent use IApplication to do that
    '
    ' Duncan Hornby, 19/12/2023
    
    Dim pMXD As IMxDocument
    Set pMXD = ThisDocument
    
    Dim pActiveView As IActiveView
    Set pActiveView = pMXD.ActiveView
    
    Dim pMap As IMap
    Set pMap = pActiveView.FocusMap
    
    Dim pGraphicsLayer As IGraphicsLayer
    Set pGraphicsLayer = pMap.ActiveGraphicsLayer
    
    Dim pGraphicsContainer As IGraphicsContainer
    Set pGraphicsContainer = pGraphicsLayer
    pGraphicsContainer.Reset
    
    Dim pElementProp As IElementProperties
    Dim pElement As IElement
    Dim pOLEFrame As IOleFrame
    
    ' Step through elements in map
    Set pElement = pGraphicsContainer.Next()
    Do Until pElement Is Nothing
        Set pElementProp = pElement
        If pElementProp.Type = "OLE Frame" Then
            ' Found an OLE Frame, now delete it
            Set pOLEFrame = pElement
            pGraphicsContainer.DeleteElement pOLEFrame
        End If
        Set pElement = pGraphicsContainer.Next()
    Loop
    
    ' Refresh map
    pActiveView.Refresh
End Sub

You can run this directly inside the VBA editor in ArcMap, or you now have the basic template of code to convert this into VB.Net and run it as outside ArcMap, your choice.

After running this code we see only the Word OLE element has been removed.

Gone

Related Question