Save MapDocument
SaveMapDocument.cpp
// Copyright 2010 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See the use restrictions.
// 

 
 
#include "SaveMapDocument.h"

// Control Interfaces
IPageLayoutControlPtr g_ipPageLayoutControl;
IToolbarControlPtr g_ipToolbarControl;
ITOCControlPtr g_ipTOCControl;

IMapDocumentPtr g_ipMapDoc;   // the current map document

// Widgets
extern Widget g_currentMapText;

int main(int argc, char* argv[])
{
  // Initialize the Engine
  ::AoInitialize(NULL);
  {
    IAoInitializePtr ipInit(CLSID_AoInitialize);
    if (ipInit == 0)
    {
      std::cerr << "Unable to initialize. This application cannot run!" 
                << std::endl;
      AoExit(0);
    }
    esriLicenseStatus status;
    ipInit->Initialize(esriLicenseProductCodeEngine, &status);
    if (status != esriLicenseCheckedOut)
    {
      std::cerr << "Invalid Licensing." << std::endl;
      AoExit(0);  
    }
  }

  XtAppContext app_context;
  SetUpForm(argc, argv, &app_context);
  
  MwCtlAppMainLoop(app_context);
}

bool OpenDocument(char* fileName)
{
  USES_CONVERSION;

  VARIANT_BOOL varbool;
  g_ipPageLayoutControl->CheckMxFile(CComBSTR(fileName), &varbool);
  if (varbool)
  {
    // Set mouse pointers
    g_ipPageLayoutControl->put_MousePointer(esriPointerHourglass);
    
    // Create a new map document
    g_ipMapDoc.CreateInstance(CLSID_MapDocument);

    // Open the map document selected
    HRESULT hr = g_ipMapDoc->Open(CComBSTR(fileName), CComBSTR(""));
    if (FAILED(hr))
      return false;

    // Set the text box to the file path of the document   
    //   To do so we could use the name passed in -- however, for the 
    //   point of the sample we want to show the USES_CONVERSIONS macro and 
    //   get_DocumentFilename().
    CComBSTR docFileName;
    g_ipMapDoc->get_DocumentFilename(&docFileName);
    char* docName = OLE2A(docFileName);
    XmTextFieldSetString(g_currentMapText, docName);

    // Set the PageLayoutControl's page layout to the map document page layout
    IPageLayoutPtr ipPageLayout;
    g_ipMapDoc->get_PageLayout(&ipPageLayout);
    hr = g_ipPageLayoutControl->putref_PageLayout(ipPageLayout);
    if (FAILED(hr))
      return false;

    // Set mouse pointers
    g_ipPageLayoutControl->put_MousePointer(esriPointerDefault);

    return true;
  }

  ShowMessage("ERROR OPENING", "NOT A VALID ARCMAP DOCUMENT");
  return false;
}

void SaveDocumentAs(char* fileName)
{
  CComBSTR docName;
  g_ipMapDoc->get_DocumentFilename(&docName);
  if (docName == fileName)
  {
    // Save changes to the current document
    SaveDocument();
    return;
  }
  else
  {
    // SaveAs a new document with relative paths
    g_ipMapDoc->SaveAs(CComBSTR(fileName), VARIANT_TRUE, VARIANT_TRUE);
    OpenDocument(fileName);
    ShowMessage("SAVED AS", "Document saved successfully!");
  }
}

void SaveDocument()
{
  // Check that the document isn't read only
  VARIANT_BOOL vb_readOnly;
  CComBSTR docFileName;
  g_ipMapDoc->get_DocumentFilename(&docFileName);
  g_ipMapDoc->get_IsReadOnly(docFileName, &vb_readOnly);
  if (vb_readOnly)
  {
    ShowMessage("SAVE FAILED", "This map document is read only!");
    return;
  }
  
  // Save with current relative path setting
  VARIANT_BOOL vb_RelPath;
  g_ipMapDoc->get_UsesRelativePaths(&vb_RelPath);
  g_ipMapDoc->Save(vb_RelPath, VARIANT_FALSE);
  
  ShowMessage("SAVED", "Changes saved successfully!");
}