This document is archived and information here might be outdated.  Recommended version.


Get MxDocument from ArcMap Snippet (ArcObjects .NET 10.5 SDK)
ArcObjects Library Reference

Get MxDocument from ArcMap Snippet

Get MxDocument from ArcMap.

[C#]
///<summary>Get MxDocument from ArcMap.</summary>
///<param name="application">An IApplication interface that is the ArcMap application.</param>
///<returns>An IMxDocument interface.</returns>
///<remarks></remarks>
public ESRI.ArcGIS.ArcMapUI.IMxDocument GetMxDocumentFromArcMap(ESRI.ArcGIS.Framework.IApplication application)
{

  if (application == null)
  {
    return null;
  }

  ESRI.ArcGIS.Framework.IDocument document=application.Document;
  ESRI.ArcGIS.ArcMapUI.IMxDocument mxDocument=(ESRI.ArcGIS.ArcMapUI.IMxDocument)(document); // Explicit Cast

  return mxDocument;

}
[Visual Basic .NET]
'''<summary>Get MxDocument from ArcMap.</summary>
'''<param name="application">An IApplication interface that is the ArcMap application.</param>
'''<returns>An IMxDocument interface.</returns>
'''<remarks></remarks>
Public Function GetMxDocumentFromArcMap(ByVal application As ESRI.ArcGIS.Framework.IApplication) As ESRI.ArcGIS.ArcMapUI.IMxDocument

  If application Is Nothing Then
    Return Nothing
  End If

  Dim document As ESRI.ArcGIS.Framework.IDocument=application.Document
  Dim mxDocument As ESRI.ArcGIS.ArcMapUI.IMxDocument=CType(document, ESRI.ArcGIS.ArcMapUI.IMxDocument) ' Explicit Cast

  Return mxDocument

End Function

Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • ESRI.ArcGIS.ArcMapUI
  • ESRI.ArcGIS.Framework
  • ESRI.ArcGIS.System