Я пытаюсь создать файл Word с помощью библиотеки Interop.Word. Я хотел добавить главную страницу, следующий оглавление и несколько глав, но у меня возникли проблемы с оглавлением. Я не могу создать это в другом абзаце. Когда я сделал это нормально, у меня были проблемы с диапазоном и ошибка 0x800A178C. Кроме того, когда я обновляю оглавление, он создает ссылки на изображения и ячейки в таблице.
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 и обновить оглавление в конце. Не знаю почему.