none
Getting all contenct of a document, inclduding text boxes. RRS feed

  • Question

  • Hi,

    I have an addIn that tracks copy/paste in between documents and also 'remembers' were content was pasted to in a document, e.g. the start and end character position of the pasted range.

    I now found that the code I have to read out all the text (to get start and end positions for the whole document) does not include text boxes.

    Word.Range wholerng = Globals.ThisAddIn.Application.Selection.Document.Content;

    Is there any way to include ALL content in the range? I assume text boxes are not the only thing I might fail to grab.

    Thanks for any help in advance!

    Wednesday, April 10, 2013 4:54 AM

Answers

  • Hi Rinmic

    <<Now after all this confusion, my actual question should be: Is there any way to include a text box properly? meaning the first character position of the text box follows the last character position outside?

    I fear the answer is that character positions are not fit to be used to localize specific content parts in documents.>>

    Your last statement is correct. Character positions are definitely not the right thing to use in order to localize content in a document, even if text boxes are not involved. There are to many "hidden" things going on.

    Historically, bookmarks are used for this purpose. These days, content controls would also be an option. Both can be picked up directly using the object model. And a Textbox (created using the Drawing tools) also has a Name and, in more recent versions, a Tag property that can be used for identification.

    Based on the limited knowledge I have about what you're doing, I'd guess selecting the Signature and placing it in a Bookmark (Insert/Bookmark) would work as you should be able to pick up the Range directly: Document.Bookmarks("BookmarkName").Range


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Rinmic Thursday, April 11, 2013 7:45 AM
    Thursday, April 11, 2013 6:05 AM
    Moderator
  • 1. As long as the user does not delete the entire range inside the bookmark, the bookmark will continue to exist. Depending on where the user starts typing, it could be that new content will end up outside the bookmark (at the end).

    2. Yes, you can create new bookmarks "on-the-fly". If you use the Range.Paste() method, however, that usually doesn't result in what you pasted being within a Range object, which can be tricky. Selection.Paste() should result in the pasted content being in a Selection object, so you can get Selection.Range. If you use Range.Text = Range.Text or Range.FormattedText = Range.FormattedText the content you bring across will be in a Range.

    3. Yes, bookmarks are completely accessible to the user through the Insert/Bookmark command. They can also choose in the Options to hide or see bookmarks in the text.

    4. Yes. Range.Bookmarks(1).Name, for example, would return the name of the first bookmark in which the Range is located. Note that a Range can be inside of multiple bookmarks - bookmarks can overlap.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Rinmic Thursday, April 11, 2013 7:50 AM
    Thursday, April 11, 2013 7:36 AM
    Moderator

All replies

  • Hi Rinmic

    Using the technique you show (and BTW Globals.ThisAddin.Application.ActiveDocument would also be a way to get the Document in question) you won't get anything associated with the "last paragraph mark", which is also a Section Break. That would mean anything associated with the headers and footers of the last section in the document won't be included in the Range. It would also drop the section formatting (newspaper columns, for example).

    I would expect any textboxes that are outside a header/footer to be included, however.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, April 10, 2013 9:34 AM
    Moderator
  • Hello Cindy,

    Is there a bullet proof way to get all the content from a document into a range? I am not interested in formatting, just the actual content.

    Thanks for the quick reply!

    Wednesday, April 10, 2013 10:21 AM
  • Hi Rinmic

    Your best bet would be something like

      Document.Content.Select()
      Selection.Copy()

    As that should include the last paragraph mark.

    But no guarantees as you have never told us exactly where the text boxes are located that you aren't picking up.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, April 10, 2013 1:29 PM
    Moderator
  • Hi Cindy,

    my apologies. The document I was talking about is build of a few space characters and line breaks followed by a text box filled with one page worth of text. I know found out that the text box is indeed caught by the range.

    I am storing (as you might remember from my other posts :)) a signature representing the content of the document as .docx custom part. The error I have triggers when I want to get the appropriate part of that signature to attach to copied content.

    If I select and copy a range of content, my addIn adds the signature by looking up the appropriate part depending on the start and end character position of the copied selection. I know learned that text boxexs are only "1 character long" , no matter how much text is in them. They start at character position x and end at x+1.

    Now within the text box, characters positions seem to function normally, but they function without the context of the document, starting at 1 no matter how much content is placed before the text box.

    Now after all this confusion, my actual question should be: Is there any way to include a text box properly? meaning the first character position of the text box follows the last character position outside?

    I fear the answer is that character positions are not fit to be used to localize specific content parts in documents.

    Thanks for all the help!



    • Edited by Rinmic Wednesday, April 10, 2013 8:15 PM spelling
    Wednesday, April 10, 2013 8:14 PM
  • Hi Rinmic

    <<Now after all this confusion, my actual question should be: Is there any way to include a text box properly? meaning the first character position of the text box follows the last character position outside?

    I fear the answer is that character positions are not fit to be used to localize specific content parts in documents.>>

    Your last statement is correct. Character positions are definitely not the right thing to use in order to localize content in a document, even if text boxes are not involved. There are to many "hidden" things going on.

    Historically, bookmarks are used for this purpose. These days, content controls would also be an option. Both can be picked up directly using the object model. And a Textbox (created using the Drawing tools) also has a Name and, in more recent versions, a Tag property that can be used for identification.

    Based on the limited knowledge I have about what you're doing, I'd guess selecting the Signature and placing it in a Bookmark (Insert/Bookmark) would work as you should be able to pick up the Range directly: Document.Bookmarks("BookmarkName").Range


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Rinmic Thursday, April 11, 2013 7:45 AM
    Thursday, April 11, 2013 6:05 AM
    Moderator
  • Edited Post:

    Thanks Cindy,

    I am not quite convinced bookmarks are robust enough, but I'll run some tests with code to see if I can make it work!

    edited to late, here my original questions:

    1. Will bookmarks adapt when users manipulate content? (add/delete content)
    2. If I paste content, can I assign a new bookmark to the pasted content?
    3. Are the users able to access the bookmarks and manipulate them?
    4. Can I access the bookmark according to the range I have?


    • Edited by Rinmic Thursday, April 11, 2013 7:50 AM
    Thursday, April 11, 2013 6:45 AM
  • 1. As long as the user does not delete the entire range inside the bookmark, the bookmark will continue to exist. Depending on where the user starts typing, it could be that new content will end up outside the bookmark (at the end).

    2. Yes, you can create new bookmarks "on-the-fly". If you use the Range.Paste() method, however, that usually doesn't result in what you pasted being within a Range object, which can be tricky. Selection.Paste() should result in the pasted content being in a Selection object, so you can get Selection.Range. If you use Range.Text = Range.Text or Range.FormattedText = Range.FormattedText the content you bring across will be in a Range.

    3. Yes, bookmarks are completely accessible to the user through the Insert/Bookmark command. They can also choose in the Options to hide or see bookmarks in the text.

    4. Yes. Range.Bookmarks(1).Name, for example, would return the name of the first bookmark in which the Range is located. Note that a Range can be inside of multiple bookmarks - bookmarks can overlap.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Rinmic Thursday, April 11, 2013 7:50 AM
    Thursday, April 11, 2013 7:36 AM
    Moderator