2

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();
doc.ReplaceContents(mxdcontents);
doc.SaveAs("C:\\test.mxd"); 

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

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

6
  • 1
    did you try just setting IMxdContents mxdcontents = mapControl ? Commented Dec 2, 2011 at 17:32
  • @Kirk Kuykendall I just tried that it resolved to null
    – patrick
    Commented Dec 2, 2011 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. Commented Dec 2, 2011 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
    Commented Dec 2, 2011 at 20:17
  • @Chaz I get a "Data necessary to complete op is not avail" using that code. :(
    – patrick
    Commented Dec 2, 2011 at 20:47

2 Answers 2

2

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

1
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.Close();
        return;
    }
    mapDocument.ReplaceContents((IMxdContents)m_mapControl.Map);
    mapDocument.Save(mapDocument.UsesRelativePaths, true);
    mapDocument.Close();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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