Word VSTO目录

时间:2015-01-19 10:39:01

标签: c# ms-word vsto office-interop tableofcontents

我正在尝试使用Interop.Word库创建Word文件。我想添加Main Page,TOC next和一些章节,但我有TOC的问题。我不能在不同的段落中创建它。当我做它正常我有范围问题和0x800A178C错误。此外,当我更新TOC时,它会创建表格中图像和单元格的链接。

Word.Range tocRange = wordDocument.Range(ref oMissing, ref oMissing);
tocRange.InsertAfter("Table of Content");
object start = wordApplication.ActiveDocument.Content.End - 1;
object oUpperHeadingLevel = "1";
object oLowerHeadingLevel = "3";
tocRange.Font.Size = 12;
tocRange.Font.Name = "Times New Roman";
tocRange = wordDocument.Range(ref start, ref oMissing);
Word.TableOfContents toc = wordDocument.TablesOfContents.Add(tocRange, ref oTrue, ref oUpperHeadingLevel, ref oLowerHeadingLevel, ref oMissing, ref oMissing,
                                                                                       ref oTrue, ref oTrue, ref oMissing, ref oTrue, ref oTrue, ref oTrue);

感谢您的任何建议。

现在我试过了

        //First|Main Page
        Word.Paragraph firstPageLogoParagraph = wordDocument.Content.Paragraphs.Add(ref oMissing);
        Word.InlineShape inlineShape = wordDocument.InlineShapes.AddPicture(AppDomain.CurrentDomain.BaseDirectory + @"..\..\Resources\logo.jpg", ref oMissing, ref oMissing, ref oMissing);
        inlineShape.ScaleHeight = (float)300.00;
        inlineShape.ScaleWidth = (float)300.00;
        firstPageLogoParagraph.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
        firstPageLogoParagraph.Range.InsertParagraphAfter();
        //First|Main Page Title
        Word.Paragraph firstPageParagarph = wordDocument.Content.Paragraphs.Add(ref oMissing);
        object firstPageParagraphStyle = Word.WdBuiltinStyle.wdStyleTitle;
        firstPageParagarph.Range.set_Style(ref firstPageParagraphStyle);
        firstPageParagarph.Range.Text = "\nWojskowa Akademia Techniczna" + softEnter + "im. Jarosława Dąbrowskiego" + softEnter + "w Warszawie";
        firstPageParagarph.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
        firstPageParagarph.Range.InsertParagraphAfter();
        firstPageParagarph.Range.InsertBreak();

        //Second Page|TOC Page
        object tocStart = wordApplication.ActiveDocument.Content.End - 1;
        Word.Range tocRange = wordDocument.Range(ref tocStart, ref oMissing);
        tocRange.InsertAfter("Spis treści\r");
        Word.TableOfContents toc = wordDocument.TablesOfContents.Add(tocRange, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oTrue, ref oTrue, ref oMissing, ref oTrue, ref oMissing, ref oMissing);
        //tocRange.InsertBreak();

        //Some paragraph
        //Add Paragraph after TOC
        Word.Paragraph firstParagraph = wordDocument.Content.Paragraphs.Add(ref oMissing);
        firstParagraph.Range.Text = "Rozdział 1";
        object firstParagraphStyle = Word.WdBuiltinStyle.wdStyleHeading1;
        firstParagraph.Range.set_Style(ref firstParagraphStyle);
        firstParagraph.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
        firstParagraph.Range.InsertParagraphAfter();

        //Add Text after Paragraph 1
        Word.Paragraph firstParagraphText = wordDocument.Content.Paragraphs.Add(ref oMissing);
        firstParagraphText.Range.Text = "To jest tekst pod rozdziałem 1. Taki tekst wstawiłem pod tym tekstem.";
        object firstParagraphTextStyle = Word.WdBuiltinStyle.wdStyleNormal;
        firstParagraphText.Range.set_Style(ref firstParagraphTextStyle);
        firstParagraphText.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify;
        firstParagraphText.Range.InsertParagraphAfter();
        //firstParagraphText.Range.InsertBreak();

        toc.Update();

但是我有一个错误,COM对象被删除....

我不能放置pageBreak并最后更新TOC。我不知道为什么。

1 个答案:

答案 0 :(得分:0)

range.InsertAfter,就像range.Text一样,不要在文字后插入换行符。这就是您需要在文字后添加\r的原因。

更改

tocRange.InsertAfter("Table of Content");

tocRange.InsertAfter("Table of Content\r");

您获得的Range错误是不言自明的:您使用的是无效范围。假设您要在文档开头添加目录,请使用此

    Word.Range tocRange = wordDocument.Range(0, 0);
    tocRange.InsertAfter("Table of Content\r");
    object start = tocRange.End - 1;
    object oUpperHeadingLevel = "1";
    object oLowerHeadingLevel = "3";
    tocRange.Font.Size = 12;
    tocRange.Font.Name = "Times New Roman";
    tocRange = wordDocument.Range(start, start);
    Word.TableOfContents toc = wordDocument.TablesOfContents.Add(tocRange, ref oTrue, ref oUpperHeadingLevel, ref oLowerHeadingLevel, ref oMissing, ref oMissing, ref oTrue, ref oTrue, ref oMissing, ref oTrue, ref oTrue, ref oTrue);