使用选择插入Word标题

时间:2017-12-01 07:24:46

标签: c# ms-word

我尝试在特定位置插入一个单词标题,但它总是添加到文档的末尾。我的错误在哪里?:

foreach (Publish p in tempstructure)
{
    if (p.element_type_id == 3)
    {
        Word.Paragraph par = selection.Paragraphs.Add();
        par.Range.Text = "test";
        par.Range.set_Style(Word.WdBuiltinStyle.wdStyleHeading2);
        par.Range.InsertParagraphAfter();
        selection.TypeParagraph();
    }
    else
    {
        if (File.Exists(@Properties.Settings.Default.documentsPath + p.filename + "_" + language + ".docx"))
        {
            selection.InsertFile(@Properties.Settings.Default.documentsPath + p.filename + "_" + language + ".docx");
            selection = word.Selection;
        }
        else
        {
            selection.TypeText("Missing file: " + p.filename + "_" + language + ".docx");
            selection.TypeParagraph();
        }
    }
    selection = word.Selection;
}

最好的问候......

1 个答案:

答案 0 :(得分:0)

简单的解决方案是:

foreach (Publish p in tempstructure)
{
    if (p.element_type_id == 3)
    {                
       selection.set_Style(Word.WdBuiltinStyle.wdStyleHeading2);
       selection.TypeText(p.name);
       selection.TypeParagraph();
    }
    else
    {
        if (File.Exists(@Properties.Settings.Default.documentsPath + p.filename + "_" + language + ".docx"))
        {
            selection.InsertFile(@Properties.Settings.Default.documentsPath + p.filename + "_" + language + ".docx");
            selection = word.Selection;
        }
        else
        {
            selection.TypeText("Missing file: " + p.filename + "_" + language + ".docx");
            selection.TypeParagraph();
        }
    }
    selection = word.Selection;
}
相关问题