none
How to insert ContentContol in footnote with specic range word.interop? RRS feed

  • Question

  • Hi Experts,

    How to go add content control in specific start and end range of footnote? I have added content control in document.range, but i unable to add in footnote, I have pasted my coding in below, and i want know where i was mistake.

    Please help to do this, if any one reply quickly, i will be proved of you.

    public void FindItalicFootnote(String FindText)
            {
                foreach (Word.Footnote footNote in Word.Document.Footnotes)
                {
                    Word.Range RngFind = footNote.Range;
                    RngFind.Find.Forward = true;
                    if (RngFind.Find.Execute(FindText))
                    {
                        while (RngFind.Find.Found)
                        {
                            RngFind.Select();
                            object strtRange = Word.selection.Range.Start;
                            object endRange = Word.selection.Range.End;
                            string placeHolder = "";
                            bool findCase = false;
                            if (Word.selection.Range.ParentContentControl == null && Word.selection.Range.ContentControls.Count == 0)
                            {
                                RngFind.Select();
    
                                while (Word.selection.Previous(Unit: Word.WdUnits.wdWord, Count: 1).Font.Italic == -1)
                                {
                                    Word.selection.Previous(Unit: Word.WdUnits.wdWord, Count: 1).Select();
                                    strtRange = VWord.selection.Range.Start;
                                    placeHolder = "{VerifiedBy='Italic'}";
                                    findCase = true;
                                }
    
                                Word.ContentControl CC = RngFind.ContentControls.Add(Word.WdContentControlType.wdContentControlRichText,
                                        footNote.range(strtRange, endRange));
    //my query is, how to say footNote.range(start, end), in main part I wrote as Word.Document.range(startRange, endRange)
                                CC.Title = "Case Reference";
                                CC.Tag = Guid.NewGuid().ToString();
                                CC.SetPlaceholderText(Text: placeHolder);
                            }
                            RngFind.Find.Execute(FindText);
                        }
                    }
                }
            }

    Regards,

    Saran

    Wednesday, July 23, 2014 3:06 AM

Answers

  • Hi Saran

    See if this works:

    Word.ContentControl CC = RngFind.ContentControls.Add(Word.WdContentControlType.wdContentControlRichText, RngFind);

    My suggestion assumes that RngFind is actually IN a Footnote (that Find.Execute was successful).

    If this doesn't work we need more information:

    1) What are you using Find for - I can't make it out from skimming your code?
    2) When it doesn't work, what happens? Do you get an error message (and what is it)?


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, July 23, 2014 3:59 PM
    Moderator
  • Hi,

    After testing your code, I find there are some syntax Error such as "Word.Document.Footnotes" and "Word.selection". I have edit it as followed. As Cindy suggested, if you just want to put the found string into the content control, the code below works well.

    public void FindItalicFootnote(String FindText)
    {
        Word.Application app = Globals.ThisAddIn.Application;
        Word.Document doc = app.ActiveDocument;
        foreach (Word.Footnote footNote in doc.Footnotes)
        {
            Word.Range RngFind = footNote.Range;
            RngFind.Find.Forward = true;
            if (RngFind.Find.Execute(FindText))
            {
                while (RngFind.Find.Found)
                {
                    RngFind.Select();
    
                    string placeHolder = "";
    
                    if (app.Selection.Range.ParentContentControl == null && app.Selection.Range.ContentControls.Count == 0)
                    {
                        RngFind.Select();
    
                        while (app.Selection.Previous(Unit: Word.WdUnits.wdWord, Count: 1).Font.Italic == -1)
                        {
                            app.Selection.Previous(Unit: Word.WdUnits.wdWord, Count: 1).Select();
                            placeHolder = "{VerifiedBy='Italic'}";
                        }
    
                        Word.ContentControl CC = RngFind.ContentControls.Add(Word.WdContentControlType.wdContentControlRichText,
                                RngFind);
                        //my query is, how to say footNote.range(start, end), in main part I wrote as Word.Document.range(startRange, endRange)
                        CC.Title = "Case Reference";
                        CC.Tag = Guid.NewGuid().ToString();
                        CC.SetPlaceholderText(Text: placeHolder);
                    }
                    RngFind.Find.Execute(FindText);
                }
            }
        }
    }



    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.

    Thursday, July 24, 2014 12:32 PM
    Moderator

All replies

  • Hi Saran

    See if this works:

    Word.ContentControl CC = RngFind.ContentControls.Add(Word.WdContentControlType.wdContentControlRichText, RngFind);

    My suggestion assumes that RngFind is actually IN a Footnote (that Find.Execute was successful).

    If this doesn't work we need more information:

    1) What are you using Find for - I can't make it out from skimming your code?
    2) When it doesn't work, what happens? Do you get an error message (and what is it)?


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, July 23, 2014 3:59 PM
    Moderator
  • Hi,

    After testing your code, I find there are some syntax Error such as "Word.Document.Footnotes" and "Word.selection". I have edit it as followed. As Cindy suggested, if you just want to put the found string into the content control, the code below works well.

    public void FindItalicFootnote(String FindText)
    {
        Word.Application app = Globals.ThisAddIn.Application;
        Word.Document doc = app.ActiveDocument;
        foreach (Word.Footnote footNote in doc.Footnotes)
        {
            Word.Range RngFind = footNote.Range;
            RngFind.Find.Forward = true;
            if (RngFind.Find.Execute(FindText))
            {
                while (RngFind.Find.Found)
                {
                    RngFind.Select();
    
                    string placeHolder = "";
    
                    if (app.Selection.Range.ParentContentControl == null && app.Selection.Range.ContentControls.Count == 0)
                    {
                        RngFind.Select();
    
                        while (app.Selection.Previous(Unit: Word.WdUnits.wdWord, Count: 1).Font.Italic == -1)
                        {
                            app.Selection.Previous(Unit: Word.WdUnits.wdWord, Count: 1).Select();
                            placeHolder = "{VerifiedBy='Italic'}";
                        }
    
                        Word.ContentControl CC = RngFind.ContentControls.Add(Word.WdContentControlType.wdContentControlRichText,
                                RngFind);
                        //my query is, how to say footNote.range(start, end), in main part I wrote as Word.Document.range(startRange, endRange)
                        CC.Title = "Case Reference";
                        CC.Tag = Guid.NewGuid().ToString();
                        CC.SetPlaceholderText(Text: placeHolder);
                    }
                    RngFind.Find.Execute(FindText);
                }
            }
        }
    }



    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.

    Thursday, July 24, 2014 12:32 PM
    Moderator