How to save .mxd programatically?

How to save .mxd programatically?

How do I save a .mxd programatically? The project is never saved and on button-click, I want to save the project.

I useSaveFileDialogto get the filepath and then use the following:

IMapDocument mapDocument= ArcMap.Document as IMapDocument; mapDocument.SaveAs(projectPath);

This crashes my ArcMap. Any suggestions?

I was able to achieve results as following:

public void SaveProject() { UID uid = new UIDClass(); //uid.Value = "{119591DB-0255-11D2-8D20-080009EE4E51}"; uid.Value = "esriArcMapUI.MxFileMenuItem"; uid.SubType = 3; ICommandItem item = ArcMap.Application.Document.CommandBars.Find(uid, false); item.Execute(); }

Appreciate your help.

you sure obtain IApplication/IMxApplication hook somehow in your code then you can obtain IMxDocument pointer and then cast it to IMapDocument:

IMxDocument ipMxDoc=(IMxApplication)ipApp; //Suppose ipApp has been obtained via hook passed in OnCreate method of ICommand interface IMapDocument ipMapDoc=(IMapDocument)ipMxDoc; ipMapDoc.SaveAs(fileName); //Suppose fileName obtained from a SaveDialogBox

Watch the video: How to create a MPK file? Compress n share ur MXD project.