Python COM for Microsoft Project (the final solution)

Mike Brenner mikeb at mitre.org
Thu Feb 7 18:49:13 EST 2002


Last month the people on this list helped me out (see the history of this thread). 

Thanks. Here is the code that worked to have a Microsoft Project file do a SAVE AS tab-delimited text.

After following the advice from this thread, the next hurdle was to realize that tab-delimited text, which is expressed as 'format="MSProject.TXT"' in Visual Basic, is expressed as 'format=3' in COM.

My next challenge is to figure out why it is not automatically choosing the 'Map="Better Task Information". Instead, it is popping up a window and making the user select it with the mouse. That is the only remaining barrier to do a stream 100% automated SAVE AS COM commands.


def saveAs_tabDelimited(schedule):
    from win32com.client import Dispatch    
    import os
    format="MSProject.TXT";format=3;oo=0
    mp = Dispatch("MSProject.Application");mp.Visible = 0
    mp.Application.MapEdit(Name="Better Task Information",DataCategory=oo,
                           TableName="Task_Data",
                           FieldName="Start", ExternalFieldName="Start_Date",
                           Create=0, OverwriteExisting=1,
                           ExportFilter="All Tasks", ImportMethod=oo, HeaderRow=1,
                           AssignmentData=0, TextDelimiter="\t", TextFileOrigin=0,
                           UseHtmlTemplate=0, IncludeImage=0)
    mp.Application.MapEdit(Name="Better Task Information", DataCategory=oo,
                           FieldName="Unique ID", ExternalFieldName="Unique_ID")
    mp.Application.MapEdit(Name="Better Task Information", DataCategory=oo,
                           FieldName="Name", ExternalFieldName="Task_Name")
    mp.Application.MapEdit(Name="Better Task Information", DataCategory=oo,
                           FieldName="Finish", ExternalFieldName="Finish_Date")
    mp.Application.MapEdit(Name="Better Task Information", DataCategory=oo,
                           FieldName="Actual Start", ExternalFieldName="Actual_Start")
    mp.Application.MapEdit(Name="Better Task Information", DataCategory=oo,
                           FieldName="Actual Finish", ExternalFieldName="Actual_Finish")
    mp.Application.MapEdit(Name="Better Task Information", DataCategory=oo,
                           FieldName="Predecessors", ExternalFieldName="Predecessors")
    mp.Application.MapEdit(Name="Better Task Information", DataCategory=oo,
                           FieldName="% Complete", ExternalFieldName="Percent_Complete")
    print; print "Files to save in tab-delimited text format"
    for sched in schedule:
        filename=sched[0]
        text_filename=sched[1]
        try: os.remove(text_filename)
        except: pass
        print "    "+filename
        mp.FileOpen(filename)
        mp.Application.FileSaveAs(Name=text_filename, Format=format, Map="Better Task Information")
        mp.Application.FileClose()
    mp.Application.FileQuit()
    print "MP application is closed"
    del mp





More information about the Python-list mailing list