locked
Selecting text from textboxes in Word RRS feed

  • Question

  • Hi,

    I am working on a Word 2007 document where I have to highlight text. I already got my code working for header, footer and the main body of the document. However the body of the document contains some textboxes. I need to highlight the text which occurs multiple times in those textboxes.

    I am not sure but right now I'm thinking of using Word.Shapes. It gives me a count of 2 since the document has 2 textboxes in the body. I need to select the range from the textbox since the search contains a starting string with anthing in between and an ending search criteria. Could someone help me in selecting the range and basically get into selecting the textbox?

    Thanks a lot in advance

    Regards,
    May
    Friday, April 10, 2009 6:57 PM

Answers

  • Hi Cindy,

    Thanks a lot for your help. I thought of Sotry Ranges initially but then I found out a way to use Word. Section and it has properties for Headers, Fotters in evenpages, firstpage etc and that worked.

     Word.Range range = doc.Range(ref start, ref end);                
                    doc.ActiveWindow.Selection.WholeStory();
                    Word.Selection selection = doc.ActiveWindow.Selection;
                    Identify(ref selection);
    foreach (Word.Section section in doc.Sections)
    
    {
    
    //Headers
    
    range = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages].Range;
    
    Identify(ref range);
    
    range = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range;
    
    Identify(ref range);
    
    range = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
    
    Identify(ref range);
    
    }
    
    • Marked as answer by May1006 Tuesday, April 14, 2009 4:49 PM
    Tuesday, April 14, 2009 4:49 PM

All replies

  • Hi May

    I only just realized that I put the wrong link in my answer to you of another message. At least, it brought me to the same message when I clicked it. No idea why that should happen, as I'd have had no reason to copy/paste the link of your question's message thread... It's happened to me before with this new forum UI, so I'm wondering if the functionality is subsituting the current message URL for the one I put in the box...

    Anyway, I wanted to point you to this message (and I hope that stays stable this time!). See the code snippet highlighted in orange in that thread. Looping through the Story Ranges, using that method, should pick up everything in the headers, footers, shapes, footnotes...
    Cindy Meister, VSTO/Word MVP
    Saturday, April 11, 2009 7:46 AM
  • Hi Cindy,

    Thanks a lot for your help. I thought of Sotry Ranges initially but then I found out a way to use Word. Section and it has properties for Headers, Fotters in evenpages, firstpage etc and that worked.

     Word.Range range = doc.Range(ref start, ref end);                
                    doc.ActiveWindow.Selection.WholeStory();
                    Word.Selection selection = doc.ActiveWindow.Selection;
                    Identify(ref selection);
    foreach (Word.Section section in doc.Sections)
    
    {
    
    //Headers
    
    range = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages].Range;
    
    Identify(ref range);
    
    range = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range;
    
    Identify(ref range);
    
    range = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
    
    Identify(ref range);
    
    }
    
    • Marked as answer by May1006 Tuesday, April 14, 2009 4:49 PM
    Tuesday, April 14, 2009 4:49 PM
  • Yes, May, but using the StoryRanges approach will also pick up the text boxes and any footnotes or endnotes, which is why I mention it. A lot less coding, if you have to search everything in the document...
    Cindy Meister, VSTO/Word MVP
    Tuesday, April 14, 2009 6:01 PM