none
Need to insert the Bookmark at the specific position in a paragraph. RRS feed

  • Question

  • I need to insert the bookmark at the specific position in a paragraph.

    below is the code snippet. pls help.

    private void btnInsert_Click(object sender, RoutedEventArgs e)
            {

                object start2 = 0;
                object end2 = Globals.ThisAddIn.Application.Selection.Paragraphs[1].Range.End;
                int paraIndex2 = Globals.ThisAddIn.Application.ActiveDocument.Range(ref start2, ref end2).Paragraphs.Count;
                Word.Range wrPara2 = Globals.ThisAddIn.Application.ActiveDocument.Paragraphs[paraIndex2].Range;
                Word.Paragraph PriorParagraph = Globals.ThisAddIn.Application.ActiveDocument.Paragraphs[paraIndex2];
                wrPara2.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdNoHighlight;
                object objFalse = false;
                object oMissing = System.Reflection.Missing.Value;
                Word._Document oDoc;
                oDoc = Globals.ThisAddIn.Application.Documents.Add(ref oMissing, ref oMissing,
                    ref oMissing, ref objFalse);
                oDoc.Activate();

                object objStart = 0;
                object objEnd = oDoc.Content.Text.Length - 1;
                Word.Range currentrange = oDoc.Range(ref objStart, ref objEnd);

                string temp = Htmltext;
                object start1 = 0;
                object end1 = Globals.ThisAddIn.Application.Selection.Paragraphs[1].Range.End;
                int paraIndex = Globals.ThisAddIn.Application.ActiveDocument.Range(ref start1, ref end1).Paragraphs.Count;
                Word.Range wrPara = Globals.ThisAddIn.Application.ActiveDocument.Paragraphs[paraIndex].Range;

                temp = CreateMergeFieldsDisplayonly(temp); // Here text value is assigned to the temp.


                currentrange.Text = temp;
                GetHTMLrendered(currentrange);

                object CollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
                Word.Range rngParaNext = wrPara2.Duplicate;
                object oParaPrev = null;
                wrPara2.InsertParagraphAfter();
               
                rngParaNext.Collapse(ref CollapseEnd);
                oParaPrev = rngParaNext;
                rngParaNext.Bookmarks.Add("B" + strSystemID, ref oParaPrev);
                string strSectionType = Section_Type;
                if (strSectionType.ToLower().Trim() == "new")
                {
                    rngParaNext.ListFormat.ListLevelNumber = PriorParagraph.Range.ListFormat.ListLevelNumber;
                }
                else if (strSectionType.ToLower().Trim() == "sub")
                {

                    rngParaNext.ListFormat.ListLevelNumber = PriorParagraph.Range.ListFormat.ListLevelNumber + 1;
                }
                else
                {
                    rngParaNext.ListFormat.ListLevelNumber = PriorParagraph.Range.ListFormat.ListLevelNumber;
                }
               
                string sBM_Merge;
                for (int i = 0; i < sMergeFields.Length; i++)
                {
                    if (sMergeFields[i] != null)
                    {
                        if (currentrange.Text.IndexOf(sMergeFields[i]) != -1)
                        {
                            object objMStart = currentrange.Text.IndexOf(sMergeFields[i]);
                            object objMEnd = currentrange.Text.IndexOf(sMergeFields[i]) + sMergeFields[i].Length;

                            int iMStart = currentrange.Text.IndexOf(sMergeFields[i]);
                            int iMEnd = currentrange.Text.IndexOf(sMergeFields[i]) + sMergeFields[i].Length;

                            Word.Range Mergerange = oDoc.Range(ref objMStart, ref objMEnd);
                            object oMergeRange = Mergerange;
                            sBM_Merge = "TestMERGE" + i.ToString();

                            Mergerange.Text = Mergerange.Text.Replace("<<>>", "");
                            Mergerange.Bookmarks.Add(sBM_Merge, ref oMergeRange);

                            Word.Range wrselect = Globals.ThisAddIn.Application.Selection.Range;
                            wrselect.SetRange(iMStart, iMEnd - 4);
                            oMergeRange = wrselect;
                            Mergerange.Copy();
                            wrselect.Paste();
                        }
                    }
                }
                rngParaNext.Text = currentrange.Text;
                oDoc.Close(ref objFalse, ref oMissing, ref oMissing);
            }


    Raghavendra babu.

    Friday, October 19, 2012 9:30 AM

Answers

  • That's a lot of code, but no explanation as to its purpose or where your problem lies...

    To insert a bookmark at a specific range, you first need to have the Range object, then use the Bookmarks.Add method, passing the Range to the parameter available for that purpose.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, October 19, 2012 10:24 AM
    Moderator