Я пытаюсь создать файл Word, используя библиотеку Interop.Word. Я хотел добавить главную страницу, TOC next и некоторые главы, но у меня были проблемы с TOC. Я не могу создать его в другом абзаце. Когда я сделал это нормально, у меня проблемы с диапазоном и ошибка 0x800A178C. Кроме того, когда я обновляю TOC, он создает ссылки на изображения и ячейки в таблице.Word VSTO Содержание
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 в начале документа, но я хочу, что он должен быть расположен на второй странице. Кроме того, уровень заголовка не работает должным образом. – mikroice90
Мои рекомендации все еще стоят. Добавьте '\ r' в конец текста абзаца и убедитесь, что при использовании диапазонов вы не перезаписываете ранее определенный контент. –