使用COM Interop在项目中添加标题
问题描述:
我使用COM Interop自动创建MS-Project文件,并在其中添加了少量任务。使用COM Interop在项目中添加标题
我想在这个文件中自动添加页眉和页脚比同样工作在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#与COM互操作。
我试过这个指令,我发现,在做一些测试:
project.Application.FilePageSetupHeader(1, PjAlignment.pjCenter, "Date");
但送花儿给人给了我这个错误System.Runtime.InteropServices.COMException:“参数值无效。 “
有人知道C#中的干净语法或FilePageSetupHeader在C#中的工作方式吗?
答
作为功能FilePageSetupHeader的所有参数都自选,似乎所述第一参数,该视图是必需的。因此,我不得不把它像这样(我在我的项目只有一个视图):
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;
}
然后你可以使用它编辑您的标题:
projApp.Application.FilePageSetupHeader(view, Microsoft.Office.Interop.MSProject.PjAlignment.pjCenter,
"Date: " + DateTime.Now.ToString("dd/MM/yyyy"));
注: projApp是微软.Office.Interop.MSProject.Application对象。
答
MS Project中的页眉和页脚是最基本的。每个部分都有一个单独的字符串属性(左,中,右);格式化通过格式代码完成。
例如,这增加的时间到中心标头:
Application.FilePageSetupHeader , PjAlignment.pjCenter, "Date: &[Date]"
注意第一个参数是视图或报告的名称,它是可选的。不要把1作为第一个参数。我更新了我的答案,将第二个参数更改为内部常量(例如1 ='PjAlignment.pjCenter')。 –