Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question
1  
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
show 1 more comment

2 Answers 2

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

share|improve this answer
add comment
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();
}
share|improve this answer
add comment

Your Answer

 
discard

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.