Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I used to know how to do this, but I must have lost the code. I tried this:

IMxdContents mxdcontents = mapControl.Map as IMxdContents;
IMapDocument doc = new MapDocumentClass();

But I got a "data is not ready to complete operation".

How can I save my ArcEngine mapcontrol map as an .mxd?

share|improve this question
did you try just setting IMxdContents mxdcontents = mapControl ? – Kirk Kuykendall Dec 2 '11 at 17:32
@Kirk Kuykendall I just tried that it resolved to null – patrick Dec 2 '11 at 19:16
what happens if you don't use as, but instead use mxdcontents = (IMxdContents)mapControl ? Also, call doc.New("test.mxd") before ReplaceContents. – Kirk Kuykendall Dec 2 '11 at 19:31
If you are just saving what you already loaded then: IMapDocument mapDocument = new MapDocumentClass(); mapDocument.Save( true, false ); should work?! (Does for me.) – Chaz Dec 2 '11 at 20:17
@Chaz I get a "Data necessary to complete op is not avail" using that code. :( – patrick Dec 2 '11 at 20:47

You need to do New or Open on the IMapDocument before ReplaceContent is called see the sample for SaveLayerFileCmd.

share|improve this answer
public void saveMap()
    IMapDocument mapDocument = new MapDocumentClass();
    mapDocument.Open(@"[path to your .mxd]", string.Empty);
    if (mapDocument.get_IsReadOnly(@"[path to your .mxd]"))
        System.Windows.Forms.MessageBox.Show("Map document \"" + @"[path to your .mxd]" + "\" is read only!");
    mapDocument.Save(mapDocument.UsesRelativePaths, true);
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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