使用 COM Interop 在项目中添加 header

Add a header in Project with COM Interop

我使用 COM Interop 自动创建了一个 MS-Project 文件,并在其中添加了一些任务。

我正在尝试在此文件中自动添加一个 header 和一个页脚,这与它在 MS-Word 中的工作方式类似,如下所示:

foreach (Microsoft.Office.Interop.Word.Section section in myDoc.Sections)
{
    Microsoft.Office.Interop.Word.Range headerRange = section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
    headerRange.Fields.Add(headerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage);
    headerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
}

但是我没有找到关于这个主题的文件,是不是至少有可能?

编辑

我试过这个语法:

Application.FilePageSetupHeader , 1, "Date:  &[Date]"

但它看起来像 VBA 语法,我正在使用 C# 和 Interop COM。

我已经尝试使用我在做一些测试时发现的这条指令:

project.Application.FilePageSetupHeader(1, PjAlignment.pjCenter, "Date");

但它总是给我这个错误 System.Runtime.InteropServices.COMException: 'The argument value is not valid.'

有人知道 C# 中的简洁语法或 FilePageSetupHeader 在 C# 中的工作方式吗?

MS Project 中的 header 和页脚是基本的。每个部分(左、中、右)有一个字符串 属性;格式化是通过格式代码完成的。

例如,这会将日期添加到中心 header:

Application.FilePageSetupHeader , PjAlignment.pjCenter, "Date:  &[Date]"

FilePageSetupHeader Documentation

FilePageSetupFooter Documentation

由于函数FilePageSetupHeader的所有参数都是optionals,看来第一个参数view是必须的。所以我不得不这样得到它(我的项目中只有一个视图):

Microsoft.Office.Interop.MSProject.Views views = project.Views;
Microsoft.Office.Interop.MSProject.View view = null;
foreach(Microsoft.Office.Interop.MSProject.View vw in views)
{
  view = vw;
}

然后你可以用它编辑你的 header :

projApp.Application.FilePageSetupHeader(view, Microsoft.Office.Interop.MSProject.PjAlignment.pjCenter,
                                                "Date: " + DateTime.Now.ToString("dd/MM/yyyy"));

注: projApp 是 Microsoft.Office.Interop.MSProject.Application object.