none
TOC VSTO creating and updating problems RRS feed

  • Question

  • Hey,

    i'm tring to create Word document contains: Main Page(image and title), TOC at second page and some Chapters with text next. (each chapter a new page)

    I have some problems :

    when i add InsertBreak, i cant update TOC becouse Update() cant see toc.

    I can create toc at the end of document but how move it at secnd page when document is ready?

    Thank you for any advice.

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




    • Edited by mikroice90 Friday, January 23, 2015 8:38 AM
    Friday, January 23, 2015 8:36 AM

Answers

  • Hi mikroice90,

    The reason of that issue is that the paragraphs (Rozdzial 1, To jest…) replace the table of content, so you can’t call Update() method. (You could use Select() method to check the tocRange)

    To deal with this issue, I suggest that you could specify the range end and create the table of content after insert paragraphs.

     object toEnd = wordApplication.ActiveDocument.Content.End - 1;
                Word.Range tocRange = wordDocument.Range(ref tocStart, toEnd);
                tocRange.InsertAfter("Spis treści\r");
                object oTrue = true;
                //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);
    ......
     firstParagraphText.Range.InsertParagraphAfter();
                //firstParagraphText.Range.InsertBreak();
                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);
                //toc.Update();

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 26, 2015 4:23 AM
    Moderator