none
Find footnote text in Word paragraph and modify footnote marker in Paragraph RRS feed

  • Question

  • How to find footnote location in a paragraph in Microsoft Word using C# so that I can insert footnote text in paragraph where we have footnote marker?

    Currently I retrieve footnote text in a paragraph using

         foreach (Microsoft.Office.Interop.Word.Paragraph para in doc.Paragraphs)
            {
                Microsoft.Office.Interop.Word.Range range = para.Range.Duplicate;

                string x = "";
                for (int i = 1; i <= para.Range.Footnotes.Count; i++)
                {
                    x = para.Range.Footnotes[i].Range.Text;
                 }
            }
    But the text "x" I want to append next to footnote script in the paragraph for processing. I would like to know if there is anyway to navigate using location or split the paragraph using any delimiter for footnote marker so that I can recreate the text with body and footnote text appended to it.


    Sunday, October 20, 2013 8:52 PM

Answers

  • Hi Arun

    Ah, that wasn't clear from your original message...

    You can try something like the following, which is written off the top of my head (pseudocode), so there might be some typos - but it should give you the idea to test

    Word.Range rng = para.Range;
    Word.Range rngFootRef = para.Range.Footnotes[i].Reference;
    rng.End = rngFootRef.Start;
    MessageBox.Show(rng.Text + para.Range.Footnotes[ï].Range.Text);


    Cindy Meister, VSTO/Word MVP, my blog

    • Proposed as answer by George HuaModerator Friday, October 25, 2013 8:49 AM
    • Marked as answer by Arun33 Friday, October 25, 2013 12:04 PM
    Tuesday, October 22, 2013 6:25 AM
    Moderator

All replies

  • Hi Arun33,

    Thank you for posting in MSDN forum.

    As far as I know, there is no way to navigate using location or split the paragraph using any delimiter for footnote marker.

    The Footnote object is a member of the Footnotes collection. The Footnotes collection represents the footnotes in a selection, range, or document.

    Footnote is binding to selection, range or document, and there is no location property, so we only can find a footnote by its selection, range or document.

    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, October 21, 2013 9:58 AM
    Moderator
  • Hi Arun

    The Footnotes collection represent that actual notes. A Footnote object has a Reference property, and this refers to the "marker" in the text. For example, the following (pseudocode!) should insert some text before the footnote reference (little superscripted number) in the paragraph:

     for (int i = 1; i <= para.Range.Footnotes.Count; i++)
                {
                     Word.Range rngFootnoteRef = para.Range.Footnotes[i].Reference;
                     rngFootnoteRef.InsertBefore("some text");
                  }


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, October 21, 2013 2:10 PM
    Moderator
  • Hi Cindy,

    Thank you very much for your response. I agree InsertBefore method can used to add text before the marker. But the problem I am facing is, it changes the original document, which I don't want to.

    Here is what I wanted to do.

    Take the text before the footnote marker (footnote reference) and then get the text of the corresponding footnote and append to it. I want to do this for whole paragraph. Then not modify the paragraph, but store the details in a variable that I would use it to process downstream.

    I thought if I use 

        Word.Range rngFootnoteRef = para.Range.Footnotes[i].Reference

         rngFootnoteRef.Text might give me text before the marker, but it doesn't seem to,

        para.Range.Footnotes[i].Range.Text gives me text of the footnote.

    If I can get can method than give me text up to the marker then I concatenate the footnote text and reconstruct the whole para into my variable that would be great.

    Thanks again for your response!

    Arun

    Monday, October 21, 2013 6:28 PM
  • Hi Arun

    Ah, that wasn't clear from your original message...

    You can try something like the following, which is written off the top of my head (pseudocode), so there might be some typos - but it should give you the idea to test

    Word.Range rng = para.Range;
    Word.Range rngFootRef = para.Range.Footnotes[i].Reference;
    rng.End = rngFootRef.Start;
    MessageBox.Show(rng.Text + para.Range.Footnotes[ï].Range.Text);


    Cindy Meister, VSTO/Word MVP, my blog

    • Proposed as answer by George HuaModerator Friday, October 25, 2013 8:49 AM
    • Marked as answer by Arun33 Friday, October 25, 2013 12:04 PM
    Tuesday, October 22, 2013 6:25 AM
    Moderator
  • Thanks, Cindy for your answer. It was very useful.
    Tuesday, October 22, 2013 3:07 PM