none
Question about use of Range and Selection in Interop programming RRS feed

  • Question

  • Could someone explain this or point me to a good tutorial?

    I've had to write a rather simple program that creates a Word doc from scratch.  At present it's mostly just create the selection and after that keep doing Selection.TypeText.

    What I think I do not understand, for lack of a better term, is how to manipulate this selection. 

    The code was working fine until I tried to add a single cell table to enclose one piece of text for emphasis.

    After that. everything I TypeText'ed wound up in the box.

    How do I 're-select' if that's the right term? 

    Wednesday, July 6, 2011 2:34 PM

Answers

  • I've been controlling my impulse to lecture you on using Selection and TypeText instead of working directly with the Range object. But since you ask... :-)

    You should avoid Selection and TypeText unless there's simply no other option. If nothing else, that will reduce screen flicker. And on the whole, it's less subject to errors (like if the user should click on the document surface while your code is executing).

    Working with Range can seem more complicated, but the accuracy more than makes up for that IMO.

    So, let's say you're inserting a table, you put something into that one cell, then you need to get out of the table (pseudocode):

    Dim tbl as Word.Table = doc.Tables.Add('params here)
    Dim rng as Word.Range = tbl.Range
    tbl.Cell(1,1).Range.Text = "some text"
    'Now get out of the table:
    rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
    rng.Text = "this text is below the table"

    With Selection, about all you can do is select the END of the document. There will always be a paragraph mark in the document below a table. something like: Doc.Bookmarks("\EndOfDoc").Select or Selection.EndKey Unit:=wdStory


    Cindy Meister, VSTO/Word MVP
    Wednesday, July 6, 2011 4:33 PM
    Moderator

All replies

  • I've been controlling my impulse to lecture you on using Selection and TypeText instead of working directly with the Range object. But since you ask... :-)

    You should avoid Selection and TypeText unless there's simply no other option. If nothing else, that will reduce screen flicker. And on the whole, it's less subject to errors (like if the user should click on the document surface while your code is executing).

    Working with Range can seem more complicated, but the accuracy more than makes up for that IMO.

    So, let's say you're inserting a table, you put something into that one cell, then you need to get out of the table (pseudocode):

    Dim tbl as Word.Table = doc.Tables.Add('params here)
    Dim rng as Word.Range = tbl.Range
    tbl.Cell(1,1).Range.Text = "some text"
    'Now get out of the table:
    rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
    rng.Text = "this text is below the table"

    With Selection, about all you can do is select the END of the document. There will always be a paragraph mark in the document below a table. something like: Doc.Bookmarks("\EndOfDoc").Select or Selection.EndKey Unit:=wdStory


    Cindy Meister, VSTO/Word MVP
    Wednesday, July 6, 2011 4:33 PM
    Moderator
  • Hello,

    I'd recommend that you record a macro while doing the things that your program needs to do. The macro shows you objects/properties/methods involved in the process. It's easy to port the macro to the programming language of your choice.


    Regards from Belarus (GMT + 2),

    Andrei Smolin
    Add-in Express Team Leader
    Friday, July 8, 2011 11:38 AM
  • Hi Chernick,

    Any update on this question?


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, July 13, 2011 10:10 AM
    Moderator