'Ms Project: How to make Gantt chart focus on entire project using C#?

  • I'm trying to create an MPP file using C#.

  • I succeeded but I had a small problem with the Gantt chart after finishing create MPP file I found the time scale of the Gantt chart in the present date so I make scroll to the first date time's task to find resources and predecessors of tasks

  • I want to make the file open and focus automatically on the entire project of the Gantt chart.

namespace of ms- project is:

using Microsoft.Office.Interop.MSProject;

    static void Main(string[] args)
    {
        //connect to API 
        var srv = new PMSgatewayClient();

        var projectuid = Guid.Parse("54d5bc97-19dd-e911-8102-00155d6b1204");

        //Get project from database
        var ProjectData = srv.Project_Get_Details(projectuid, Guid.Empty, false, false, Guid.Empty, App_Code.BasicClasses.BasicData.CurrentUser.Id, null, out ErrorCode, out ErorMessage);

        object missing = System.Reflection.Missing.Value;

        //Create the Application object to interact with MS project
        Microsoft.Office.Interop.MSProject.Application appclass = new Application();
        appclass.Projects.Add(false, missing, missing);

        //Get the name of project from database
        string fileName = ProjectData.BasicInfo.DisplayName + DateTime.Now.ToString("yyyy MM mm ss ").Trim();

        //Create file in folder call temp with the name of project 
        appclass.ActiveProject.SaveAs($"{Server.MapPath("~/Temp")}/{fileName}.mpp", PjFileFormat.pjMPP, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);

        //Get the active project in the file
        Project project = appclass.ActiveProject;

        Microsoft.Office.Interop.MSProject.Resources resources = project.Resources;

        //We want to go through all the elements in the list
        Tasks tasks = project.Tasks; 

        // here i'm trying to make the Gantt chart focus on time scale of the project or focus on entire project
        project.LevelEntireProject = true;
        tasks.Application.ZoomTimescale("Days", true, false, false);

    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source