locked
problem in selecting and deleting a Range object RRS feed

  • Question

  • Hi,

    In my program, sometimes I need to select a Range and then delete it. On some specific files, when I select the Range of the last sentence of the file, the whole file will be selected and by deleting the selection the whole text of the file will be deleted. How could I handle this kind of files? In other words, how could I select a Range and be sure that in the cases described, the whole text of the file would not be selected?

    Regards,

    Hamidreza Ghader

    Wednesday, January 19, 2011 2:04 PM

Answers

  • Hi Hamidreza

    Hmmm. This isn't something I can recall ever seeing or experiencing...

    The only time I'd expect something outside the selected range to also be selected would be IF that selection is within a field. Under some circumstances, with some versions of Word or certain settings, this will select the entire field. And if the entire document is a single field, then it would be selected.

    So I think we need to understand more about "the internals" of these Word documents in order to figure out what's happening. How are these documents being contstructed? Are they typed by a user? Generated by automation of the Word application? Generated as OOXML? Converted from some other file format?

    Is this final sentence perhaps not in the main body of the document?


    Cindy Meister, VSTO/Word MVP
    • Proposed as answer by Bruce Song Thursday, January 20, 2011 5:24 AM
    • Unproposed as answer by Hamidreza G Thursday, January 20, 2011 6:47 AM
    • Marked as answer by Hamidreza G Thursday, January 20, 2011 9:09 PM
    Wednesday, January 19, 2011 3:29 PM
  • Hi Hamidreza

    Mmm, I wonder if that selection actually extends beyond the end of the sentence, into the table structure itself?

    Can you have your code stop at this point, then you look at the document. Press the keyboard combination Shift+Left Arrow once, maybe twice. This should take the end of the selection out of the table structure, back into that last cell's boundaries?

    If yes, you want to use Selection (or Range) .MoveEnd with Units set to wdUnitCharacter and Count to -1 (or whatever number of times you need the key combination in the UI to reduce the selection).


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Hamidreza G Thursday, January 20, 2011 9:09 PM
    Thursday, January 20, 2011 8:53 AM

All replies

  • Hi Hamidreza

    Hmmm. This isn't something I can recall ever seeing or experiencing...

    The only time I'd expect something outside the selected range to also be selected would be IF that selection is within a field. Under some circumstances, with some versions of Word or certain settings, this will select the entire field. And if the entire document is a single field, then it would be selected.

    So I think we need to understand more about "the internals" of these Word documents in order to figure out what's happening. How are these documents being contstructed? Are they typed by a user? Generated by automation of the Word application? Generated as OOXML? Converted from some other file format?

    Is this final sentence perhaps not in the main body of the document?


    Cindy Meister, VSTO/Word MVP
    • Proposed as answer by Bruce Song Thursday, January 20, 2011 5:24 AM
    • Unproposed as answer by Hamidreza G Thursday, January 20, 2011 6:47 AM
    • Marked as answer by Hamidreza G Thursday, January 20, 2011 9:09 PM
    Wednesday, January 19, 2011 3:29 PM
  • Hi Cindy,

    As you correctly guessed, the entire document is a table and the whole text of the document is within a table cell. Now, I ask again. How could I handle selection of the last sentence in a field and be sure that the entire text would not be selected.

    Regards,

    Hamidreza Ghader

    Thursday, January 20, 2011 6:44 AM
  • Hi Hamidreza

    Mmm, I wonder if that selection actually extends beyond the end of the sentence, into the table structure itself?

    Can you have your code stop at this point, then you look at the document. Press the keyboard combination Shift+Left Arrow once, maybe twice. This should take the end of the selection out of the table structure, back into that last cell's boundaries?

    If yes, you want to use Selection (or Range) .MoveEnd with Units set to wdUnitCharacter and Count to -1 (or whatever number of times you need the key combination in the UI to reduce the selection).


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Hamidreza G Thursday, January 20, 2011 9:09 PM
    Thursday, January 20, 2011 8:53 AM
  • Yes, it extends beyond the end of the sentence into the table structure. I added it some code in which I used Selection.IsEqual(Range) to see if the selection is equal to the intended Range. Then if it is not, I move the end of the range one wdCharacter unit back and select it again. I do it until Selection.IsEqual(Range) return true. This works and solves the problem of selection, but I think this is not a reliable way.

    Regards,

    Hamidreza Ghader

    Thursday, January 20, 2011 9:19 AM
  • Hi Hamidreza,

    Instead of using something like .Range.Delete, try .Range.Text = vbNullString.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Thursday, January 20, 2011 10:38 AM