none
Insert Several Texts to a Range? RRS feed

  • Question

  • Hi,

    I am using Visual C++ to implement Word automation. Now what I need is to insert multiple texts to the end of a specific range, one by one. I read https://docs.microsoft.com/en-us/office/vba/api/overview/word/object-model and find there are a lot of methods to do so. Assuming the range is called MyRange:

    1. I can use MyRange.put_Text() method:

    MyRange.put_Text(_T(“Hello”));

    MyRange.put_Text(_T(“Word”));

              Then “Word” will replace “Hello” instead of being inserted after “Hello”.

    2. I can use MyRange.InsertAfter()

    MyRange.InsertAfter(_T(“Hello”));

    MyRange.InsertAfter(_T(“World”));

    3. I can also use MyRange.Select() followed by Selection.TypeText();

    MyRange.Select();

    Selection.TypeText(_T(“Hello”));

    Selection.TypeText(_T(“World”));

    It seems that method 1 is not feasible since the next text will replace the current one. While 2 and 3 should be feasible, but what are the differences between these two methods?

    Thanks

    Wednesday, August 22, 2018 7:29 AM

Answers

  • Method 1: For this you'd need to "collapse" the Range (think of it like pressing the right-arrow key as a user, when you have a selection you don't want to over-type). I don't know the C++ syntax, but it appears the basic object model is there so I expect you may find it? C# would be

         object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
        MyRange.Collapse(ref oCollapseEnd);
        MyRange.Text = "new text";

    Method 2: Will always insert the new text at the end of the contiguous content of MyRange and include it in the range. So if you want to apply different formatting to the new text, for example, you wouldn't be able to do so because there would be no way to distinguish it.

    Method 3: This mimics user actions. Basically the same problem as Method 1 - it will replace the selection, which means it will replace the content of MyRange.

    Beyond these basic differences, my personal preference is to use Method 1. Method 3 will execute more slowly because Word needs to update the screen. Selection is not as reliable since, theoretically, the user could change what's selection during code execution. Also, when you read your code, Selection isn't as clear about what's happening (not self-documenting). In addition, you can work with multiple Range objects, but there can be only one Selection.


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    • Marked as answer by chcw Tuesday, August 28, 2018 4:07 AM
    Thursday, August 23, 2018 10:43 AM
    Moderator

All replies

  • According to my research, you can refer to the following two articles:

    Range.InsertAfter Method (Word)

    Selection.TypeText Method (Word)

    Hopefully it helps you.

    Best Regards,

    Simon


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Wednesday, August 22, 2018 10:03 AM
    Moderator
  • Method 1: For this you'd need to "collapse" the Range (think of it like pressing the right-arrow key as a user, when you have a selection you don't want to over-type). I don't know the C++ syntax, but it appears the basic object model is there so I expect you may find it? C# would be

         object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
        MyRange.Collapse(ref oCollapseEnd);
        MyRange.Text = "new text";

    Method 2: Will always insert the new text at the end of the contiguous content of MyRange and include it in the range. So if you want to apply different formatting to the new text, for example, you wouldn't be able to do so because there would be no way to distinguish it.

    Method 3: This mimics user actions. Basically the same problem as Method 1 - it will replace the selection, which means it will replace the content of MyRange.

    Beyond these basic differences, my personal preference is to use Method 1. Method 3 will execute more slowly because Word needs to update the screen. Selection is not as reliable since, theoretically, the user could change what's selection during code execution. Also, when you read your code, Selection isn't as clear about what's happening (not self-documenting). In addition, you can work with multiple Range objects, but there can be only one Selection.


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    • Marked as answer by chcw Tuesday, August 28, 2018 4:07 AM
    Thursday, August 23, 2018 10:43 AM
    Moderator
  • Did you resolved your issue now ? Welcome you feedback for this. 

    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Monday, August 27, 2018 3:29 AM
    Moderator
  • Did you resolved your issue now ? Welcome you feedback for this. 

    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Sorry for the delay. Actually I am waiting for my answers. Cindy's answer is comprehensive but I think method 2 is better.
    Tuesday, August 28, 2018 4:07 AM
  • I'm not sure what you mean by "waiting for my answers". You asked about the differences between the methods, which I did explain in detail...

    You did not ask "which is better", although I did mention and explain my preference. But "better" is always a matter of opinion and depends on the exact situation. So there's really no exact answer to "better"...


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Tuesday, August 28, 2018 5:35 AM
    Moderator
  • Thanks for you feedback. 

    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Tuesday, August 28, 2018 6:13 AM
    Moderator
  • Thanks Cindy.

    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Tuesday, August 28, 2018 6:13 AM
    Moderator
  • I'm not sure what you mean by "waiting for my answers". You asked about the differences between the methods, which I did explain in detail...

    You did not ask "which is better", although I did mention and explain my preference. But "better" is always a matter of opinion and depends on the exact situation. So there's really no exact answer to "better"...


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Hi, Cindy,

    Sorry for my typo. What I means is "waiting for more answers". Since which method is better is a personal preference, I just want to see if others can provide their suggestions or advice on which method they will use or prefer to use for my case.

    Thursday, August 30, 2018 8:38 AM