[GIS] MapScale not being persisted properly in mxd when programmatically changed outside of ArcMap environment

arcobjectsc

I am trying to change the scale of a map in an mxd OUTSIDE of ArcMap in a standalone Windows application. The problem is the scale is never persisted – the best code I have come up with is the following:

private void UpdateMapScaleInMxd(double scale, string mxdFullPath)
        {
            IMapDocument mapDocument = new MapDocumentClass();
            mapDocument.Open(mxdFullPath, "");
            IPageLayout pageLayout = (IPageLayout)mapDocument.ActiveView;

            IGraphicsContainer graphicsContainer = (IGraphicsContainer)pageLayout;
            graphicsContainer.Reset();
            IMapFrame mapFrame;

            IActiveView tmpActiveView = null;

            IElement element = graphicsContainer.Next();
            while (element != null)
            {
                if (element is IMapFrame)
                {
                    mapFrame = (IMapFrame)element;
                    tmpActiveView = (IActiveView)mapFrame.Map;

                    mapFrame.ExtentType = esriExtentTypeEnum.esriExtentScale;
                    mapFrame.MapScale = scale;
                    tmpActiveView.Refresh();
                }

                element = graphicsContainer.Next();
            }

            mapDocument.Save(false, false);
            mapDocument.Close();
        }

I check the mxd by opening it up in ArcMap and the map scale has changed i.e the data frame now has a fixed scale and the scale combobox is greyed out.

So now I try to export the map as a PDF in code – and the most frustrating thing is – it exports the map at the previous scale that was set – NOT the scale I just changed it to! So infuriating – if anyone can help me understand where I am going wrong that would be great.

UPDATE:

In the code above I am using the IMapDocument – and I think there are limitations (according to the help):

The IMapDocument interface provides
properties and methods for reading map
document files (*.mxd, *mxt, .pmf)
and writing and saving changes to map
document files (
.mxd). However,
since it is not tied to the ArcMap
application, application-specific
functionality in the MapDocument will
not be persisted. Examples of
application specific functionality are
toolbar settings, UI customizations,
VBA projects, and ArcMap graphs. For
desktop developers who need to use
this functionality, the MxDocument
interface located in the ArcMapUI
library is a better choice.

I am now thinking that changing the map scale can not be persisted properly using the IMapDocument interface.

Best Answer

Solved - and it took Redlands to come back with a slightly unusual fix, it utilises the InteropServices library - hopefully it will be fixed properly in a future release or patch by ESRI.

[DllImport("User32.dll")]
public static extern int GetDesktopWindow(); 

private IMapDocument mapDocument = null;

private void ChangeMapScale()
{
    mapDocument.Open(@"C:\Temp\foo.mxd", null);

    IPageLayout pageLayout = mapDocument.PageLayout;
    IActiveView activeView = (IActiveView)pageLayout;
    IMap map = activeView.FocusMap;

    activeView = (IActiveView)mapDocument.PageLayout;
    activeView.Activate(GetDesktopWindow());

    map.MapScale = value;
    activeView.Refresh();

    mapDocument.Save(true, true);
}
Related Question