1

I try to place some paragraphs automaticaly into a Word Document but it always puts it at the end of the document and not to the Bookmark.

public void createStepTable(Word.Document _myDoc, DataGridView dgv, Testcaselist _testcaselist)
    {
        int namecount = 1;
        object oMissing = System.Reflection.Missing.Value;

        Word.Bookmark myBookmark = _myDoc.Bookmarks.get_Item("TextMarkeEinzelheiten");
        MessageBox.Show(myBookmark.Start.ToString());
        Word.Range myRange = _myDoc.Range(myBookmark.Start,myBookmark.End);

        Word.Field myfield = _myDoc.Fields.Add(myRange);
        Word.Selection mySelection = myRange.Se




        foreach (Testchannellist testChannelListToFind in _testcaselist.Testchannellist)
        {

            Word.Paragraph pText = _myDoc.Paragraphs.Add(myRange);         
            pText.Format.SpaceAfter = 10f;
            pText.Range.Text = String.Format("This is headline #{0}",namecount);
            pText.Range.InsertParagraphAfter();

            int stepcount = 0;
            foreach (Teststeplist testStepListToFind in testChannelListToFind.Teststeplist)
            {
                var sText = _myDoc.Paragraphs.Add(myRange);  
                sText.Format.SpaceAfter = 10f;
                sText.Range.Text = String.Format("This is testfall #{0}", stepcount);
                sText.Range.InsertParagraphAfter();


                for (int i = 0; i < testStepListToFind.requirementlist.Count; i++)
                {
                    var rText = _myDoc.Paragraphs.Add(myRange);  
                    rText.Range.ListFormat.ApplyBulletDefault();
                    rText.Range.InsertBefore(testStepListToFind.requirementlist[i].ToString() );
                }

                dgv.DataSource = testStepListToFind.repTest;
                var tText = _myDoc.Paragraphs.Add(myRange);                    
                tText.Format.SpaceAfter = 10f;
                tText.Range.Text = String.Format("This is Tabelle #{0}", stepcount );
                tText.Range.InsertParagraphAfter();
                stepcount++;


            }


        }

    }

All those automaticaly created Paragraphs should be at the "TextMarkeEinzelheiten" Bookmark but every try Ends up in a mess.

1 Answer 1

1

First of all make sure that the bookmark does exist in the document/template you are using.

This is the code that i use to insert tables. Pragraphs should be the same

            Range range = null;
            object pageBookmark = "TextMarkeEinzelheiten";
            if (_myDoc.Bookmarks.Exists(pageBookmark.ToString()))
            {
                range = _myDoc.Bookmarks.get_Item(ref pageBookmark).Range;
                Bookmark bookmark = _myDoc.Bookmarks.get_Item(ref pageBookmark);
                bookmark.Select();
            }
            else
            {
                range = range ?? _myDoc.Range(0, 0);
            }
            Word.Paragraph pText = _myDoc.Paragraphs.Add(myRange);

Try this code first without using the for loop. once you get one paragraph in correctly adjust the code so that the range and selection is updated as you insert the new paragraph in the loop.

Good luck :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.