<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Load another mxd in current document in ArcObjects SDK Questions</title>
    <link>https://community.esri.com/t5/arcobjects-sdk-questions/load-another-mxd-in-current-document/m-p/391315#M10408</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Duncan,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for your reply.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It's strange though, the &lt;A href="http://resources.arcgis.com/en/help/arcobjects-net/componenthelp/index.html#//000v000001p2000000"&gt;documentation&lt;/A&gt; clearly indicates that MxDocument implements IMapDocument. But maybe they never bothered to implement that particular method.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Copying layers is the easy part, and actually a oneliner:&lt;/P&gt;&lt;PRE __default_attr="c#" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14358461307343395 jive_text_macro" data-renderedposition="176_8_912_16" jivemacro_uid="_14358461307343395" modifiedtitle="true"&gt;&lt;P&gt;pMXD.FocusMap.AddLayers(pMXDContents.Map.Layers, true);&lt;/P&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;but there's more to it than just that: Adding standalone tables, restoring relationshipclasses and joins, maybe more? It's not that it cannot be done, but I'd prefer to use IMapDocument, if at all possible &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/sad.png" /&gt;.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 02 Jul 2015 14:15:50 GMT</pubDate>
    <dc:creator>BerendVeldkamp</dc:creator>
    <dc:date>2015-07-02T14:15:50Z</dc:date>
    <item>
      <title>Load another mxd in current document</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/load-another-mxd-in-current-document/m-p/391313#M10406</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am trying to write an addin that loads content (tables, featurelayers, relationships, ...) from an external mxd into the current ArcMap session. I do not want to actually load the mxd, because it would confuse users (they may be working on some mxd of their own, but after running my code it would point to &lt;EM&gt;my&lt;/EM&gt; document).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is my code so far:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;var reader = new MapReaderClass();
reader.Open(mxd.FullName);
var contents = reader.Map[0] as IMxdContents;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
// Test: write contents to external mxd, this works!
var mapOut = new MapDocumentClass();
mapOut.Open(@"D:\temp\untitled.mxd");
mapOut.ReplaceContents(contents);
mapOut.Save();

// Write contents to current document, this does not work!
var thisMapDoc = ArcMap.Document as IMapDocument; 
thisMapDoc.Map[0].ReplaceContents(contents);

reader.Close();

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My problem is that copying the contents to another mxd works flawlessly, but copying it into the current document does not. Line 13 throws an exception, for some documents: &lt;STRONG&gt;Not implemented (Exception from HRESULT: 0x80004001 (E_NOTIMPL))&lt;/STRONG&gt;, and for others &lt;STRONG&gt;The table does not have an OID Field. [NAME_OF_MY_TABLE]&lt;/STRONG&gt; (One table does in fact have no OID field, but why that causes an error is beyond me).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I tried in both 10.1 and 10.3.1 with the same results.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:56:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/load-another-mxd-in-current-document/m-p/391313#M10406</guid>
      <dc:creator>BerendVeldkamp</dc:creator>
      <dc:date>2021-12-11T17:56:23Z</dc:date>
    </item>
    <item>
      <title>Re: Load another mxd in current document</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/load-another-mxd-in-current-document/m-p/391314#M10407</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Berend,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I think the problem comes from&lt;STRONG&gt; IMapDocument&lt;/STRONG&gt; and its method &lt;EM&gt;ReplaceContents&lt;/EM&gt;. It seems to be a method implemented by &lt;STRONG&gt;MapDocument&lt;/STRONG&gt; class but not the &lt;STRONG&gt;MXDocument&lt;/STRONG&gt; class.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Below is some VBA that transfers layers from one MapDocument into the currently running MXDocument.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;Public Sub TransferLayers()
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #7ed529;"&gt;''' Get a handle on existing Map Document&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim pMapDocument As IMapDocument
&amp;nbsp;&amp;nbsp;&amp;nbsp; Set pMapDocument = New MapDocument
&amp;nbsp;&amp;nbsp;&amp;nbsp; pMapDocument.Open "C:\Scratch\Untitled.mxd"
&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #7ed529;"&gt;''' Get a handle on the contents of this Map Document (assumed to be only 1 DataFrame)&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim pMXDContents As IMxdContents
&amp;nbsp;&amp;nbsp;&amp;nbsp; Set pMXDContents = pMapDocument.Map(0)
&amp;nbsp;&amp;nbsp;&amp;nbsp; Debug.Print pMXDContents.Map.LayerCount
&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #7ed529;"&gt;''' Transfer layers into current MXD&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim pMXD As IMxDocument
&amp;nbsp;&amp;nbsp;&amp;nbsp; Set pMXD = ThisDocument
&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim pLayer As ILayer
&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim i As Integer
&amp;nbsp;&amp;nbsp;&amp;nbsp; For i = 0 To pMXDContents.Map.LayerCount - 1
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Set pLayer = pMXDContents.Map.Layer(i)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Debug.Print pLayer.Name
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pMXD.FocusMap.AddLayer pLayer
&amp;nbsp;&amp;nbsp;&amp;nbsp; Next i
&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #7ed529;"&gt;''' Refresh&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; pMXD.UpdateContents
End Sub&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:56:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/load-another-mxd-in-current-document/m-p/391314#M10407</guid>
      <dc:creator>DuncanHornby</dc:creator>
      <dc:date>2021-12-11T17:56:26Z</dc:date>
    </item>
    <item>
      <title>Re: Load another mxd in current document</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/load-another-mxd-in-current-document/m-p/391315#M10408</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Duncan,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for your reply.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It's strange though, the &lt;A href="http://resources.arcgis.com/en/help/arcobjects-net/componenthelp/index.html#//000v000001p2000000"&gt;documentation&lt;/A&gt; clearly indicates that MxDocument implements IMapDocument. But maybe they never bothered to implement that particular method.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Copying layers is the easy part, and actually a oneliner:&lt;/P&gt;&lt;PRE __default_attr="c#" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14358461307343395 jive_text_macro" data-renderedposition="176_8_912_16" jivemacro_uid="_14358461307343395" modifiedtitle="true"&gt;&lt;P&gt;pMXD.FocusMap.AddLayers(pMXDContents.Map.Layers, true);&lt;/P&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;but there's more to it than just that: Adding standalone tables, restoring relationshipclasses and joins, maybe more? It's not that it cannot be done, but I'd prefer to use IMapDocument, if at all possible &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/sad.png" /&gt;.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 Jul 2015 14:15:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/load-another-mxd-in-current-document/m-p/391315#M10408</guid>
      <dc:creator>BerendVeldkamp</dc:creator>
      <dc:date>2015-07-02T14:15:50Z</dc:date>
    </item>
    <item>
      <title>Re: Load another mxd in current document</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/load-another-mxd-in-current-document/m-p/391316#M10409</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;In case anyone is interested, I ended up loading the map using the MapReader class:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;var reader = new MapReaderClass();
reader.Open(mxd);
var source = reader.Map[0];&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then copying the layers (IMap.AddLayers() messes up the original order):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for (var i = source.LayerCount; i &amp;gt; 0; i--)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; target.AddLayer(source.Layer[i - 1]);
}&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;And tables:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for (var i = 0; i &amp;lt; source.StandaloneTableCount; i++)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; target.AddStandaloneTable(source.StandaloneTable&lt;I&gt;);&lt;/I&gt;
}&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This way, any relationshipclass defined in the template mxd is imported automatically.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When loading the layers, the map will zoom out to the entire extent of the added layers. In order to avoid that, you need to set the map's spatial reference and extent (or scale, depending on what you want to do exactly) &lt;EM&gt;before&lt;/EM&gt; adding any layers:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;target.SpatialReference = source.SpatialReference;
target.MapScale = source.MapScale;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Also, to speed thing up a little:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;ArcMap.Document.CurrentContentsView.ProcessEvents = false;

try
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; // Load map here...
}
finally
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; ArcMap.Document.CurrentContentsView.ProcessEvents = true;
&amp;nbsp;&amp;nbsp;&amp;nbsp; ArcMap.Document.CurrentContentsView.Refresh(null);
}&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:56:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/load-another-mxd-in-current-document/m-p/391316#M10409</guid>
      <dc:creator>BerendVeldkamp</dc:creator>
      <dc:date>2021-12-11T17:56:29Z</dc:date>
    </item>
  </channel>
</rss>

