【发布时间】: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