Word.ParagraphFormat属性工作不正常

时间:2018-03-26 18:04:48

标签: c# ms-word office-interop

我有一个问题,Word.ParagraphFormat属性不起作用。这是我的代码:

private void ThisDocument_Startup(object sender, EventArgs e)
{
    Word.ParagraphFormat format = new Word.ParagraphFormat();

    format.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft; 
    // text is as if it were center alignment; behaves like ".wdAlighnParagraphCenter"

    format.OutlineLevel = Word.WdOutlineLevel.wdOutlineLevelBodyText; 
    // text boundaries around every section after line return rather than the entire page text and header/footer; behaves like ".wdOutlineLevelX" (X for 1-9)

    Word.Style stl = Styles[Word.WdBuiltinStyle.wdStyleNormalTable] as Word.Style;
    stl.UnhideWhenUsed = true;
    stl.ParagraphFormat = paragraphFormat;
    stl.NoSpaceBetweenParagraphsOfSameStyle = true;
}

我需要添加或带走一些东西吗?还有其他需要设置或分配的东西吗?或者,以后有什么东西可以写吗?

1 个答案:

答案 0 :(得分:0)

Office对象模型的工作方式与.NET对象模型不同 - 该技术已有二十多年的历史,人们认为的方式也不同。在Office对象模型中,您需要首先创建对象。只有这样才能定义其属性。 (与此相反,在.NET世界中,您可以定义属性,然后创建对象并分配属性。)

因此,对于定义样式,你更喜欢它:

private void ThisDocument_Startup(object sender, EventArgs e)
{
    Word.Style Stl = Styles[Word.WdBuiltinStyle.wdStyleHeading1] as Word.Style;

    Stl.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft; 

    Stl.ParagraphFormat.OutlineLevel = Word.WdOutlineLevel.wdOutlineLevelBodyText; 

    Stl.UnhideWhenUsed = true;
    Stl.NoSpaceBetweenParagraphsOfSameStyle = true;
}

另请注意,您无法为 Table 样式设置上述属性,如问题的示例代码中所示。这些设置仅适用于段落(或链接 - 尽管我强烈建议不要使用链接)样式。

相关问题