【问题标题】:Word VSTO Table of ContentsWord VSTO 目录
【发布时间】:2015-03-17 08:14:46
【问题描述】:

我正在尝试使用 Interop.Word 库创建一个 Word 文件。我想添加主页、下一个目录和一些章节,但我遇到了目录问题。我不能在不同的段落中创建它。当我正常操作时,我遇到范围问题和 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 和更新目录放在最后。我不知道为什么。

【问题讨论】:

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


    【解决方案1】:

    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);
    

    【讨论】:

    • 我不想在文档的开头添加目录,但我希望它应该位于第二页。此外,标题级别无法正常工作。
    • 我的建议仍然有效。在段落文本的末尾添加\r,并确保在使用 Ranges 时不会覆盖之前定义的内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    • 1970-01-01
    • 2018-11-12
    相关资源
    最近更新 更多