I am looking for a way to automatically save and open an .mxd file, be in in ModelBuilder (with ArcMap tools) or Arcpy, but I can't find any functions to support this.

I basically want to save the .mxd in a predefined location and open it from the same location. For example C:\ArcGIS\%Project number%, where the "Project number" is filled in when the model is run. After saving the mxd, it should be able to open by using another model, which would require just the Project number as well.

I have found some posts that discuss this, but no solutions are given. My knowledge of Arcpy/Python is beginner at most, but I am willing to learn. I have not found a tutorial adressing this issue. How can I do this?

I have found the following to open an MXD:

import os

Project_nummer = arcpy.GetParameterAsText(0)

os.startfile(r"G:\\zn\\NM\\GGB\\Applicaties-Admin\\Kerngis\\ExpImp\\{}\\{}.mxd".format(Project_number, Project_number))

The reason I want to achieve this is the following: I have source data which I want to export to a different user, while I do this export I want to create a "project" folder, with the project .mxd inside it (Based on the project number).

After this user makes some changes to the export and sends it back, I want to be able to start a model, fill in a project number and have it open the .mxd and "import" the changes into my source data.


I assume you've created a script tool and you're using that inside the model and the script is failing with invalid syntax because you're confusing ModelBuilder-inline variables with tool-parameters.

The pieces between % % represents the model inline variable. You can't simply pass that into the scripting world. This would be proper if you were passing it into a tool parameter inside the model, but you can't push that into the script inside your model. Instead, you'd need to write your code like:

import arcpy

Project_number = arcpy.GetParameterAsText(0)

mapdoc = "C:\\Arcgis\\{}.mxd".format(Project_number)
# or ... this will work too
mapdoc = "C:\\Arcgis\\" + Project_number + ".mxd"

mxd = arcpy.mapping.MapDocument(mapdoc)
  • Awesome! This was just what I was looking for! However the MapDocument command does not open up the mxd (maybe I am using it wrong). I therefor changed to the following import os os.startfile(G:\\zn\\NM\\GGB\\Applicaties-Admin\\Kerngis\\ExpImp\\%Project nummer%\\%Project nummer%.mxd) Do you perhaps know of a way to save an .mxd aswel?
    – Remko
    Jan 29 '19 at 13:49
  • That command simply creates a variable (reference) to the MXD. It will not open it in the same sense as if you were to double click it and have ArcMap open. At this point its not clear why you're opening a MXD from another MXD to only have it saved? There must be some intermediate steps.
    – KHibma
    Jan 29 '19 at 13:51
  • I apologize, my code got a bit messed up as I posted it, I have editted my main question with some more info and with the new code (which does the trick, although not 100% as I'd like to).
    – Remko
    Jan 29 '19 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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