none
Pastespecial without replacing the last pastespecial RRS feed

  • Question

  • Hi,

    I´m using the pastespecial method, pasting the email body of selected items in outlook into a word document. I´m automating word from outlook, not the other way around.

    I want to do this repeatedly, i.e. for all selected emails in an outlook folder. However when the second email body is pasted, it replaces the first text in the word document, so I can never hav more than one email in the word document.

    How do I solve this? I´ve searched the documentation but can´t find it. Is there a pasteafter or pastebefore parameter??

    I´ve also tried the selection.moveend for instance, but it doesn´t work.

    Thanks!

    Saturday, February 15, 2014 9:28 AM

Answers

  • Instead of simply setting the paste range to the document, try setting it to the last character in the document. You may also need to insert a paragraph break between each paste sequence. For example, something along the lines of the following pseudo-code:

    For Each EmailBody In EmailCollection
      EmailBody.Copy
      With wdDoc.Range
        .Characters.Last.PasteSpecial
        .Collapse 0 'wdCollapseEnd
        .InsertAfter vbCr
      End With
    Next


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Black Santa Saturday, February 15, 2014 4:55 PM
    Saturday, February 15, 2014 10:06 AM

All replies

  • Hi,

    I´m using the pastespecial method, pasting the email body of selected items in outlook into a word document. I´m automating word from outlook, not the other way around.

    I want to do this repeatedly, i.e. for all selected emails in an outlook folder. However when the second email body is pasted, it replaces the first text in the word document, so I can never hav more than one email in the word document.

    How do I solve this? I´ve searched the documentation but can´t find it. Is there a pasteafter or pastebefore parameter??

    Thanks!

    • Merged by Marvin_Guo Sunday, February 23, 2014 6:57 AM The same
    Thursday, February 13, 2014 4:44 PM
  • Hello Black Santa,

    The Word Object Model provides the Selection object. A selection represents either a selected (or highlighted) area in the document, or it represents the insertion point if nothing in the document is selected. There can be only one Selection object per document window pane, and only one Selection object in the entire application can be active.

    It looks like you need to use the MoveStart method of the Selection class for moving the start position of the specified selection. I'd recommend asking such question in the Word for Developers forum instead. There you will get the most qualified answers related to the Word object mode.

    Finally, you may find the How to automate Microsoft Word to create a new document by using Visual C# useful.

    Thursday, February 13, 2014 5:05 PM
  • Thanks Eugene,

    I will try the word forum.

    Saturday, February 15, 2014 9:21 AM
  • Instead of simply setting the paste range to the document, try setting it to the last character in the document. You may also need to insert a paragraph break between each paste sequence. For example, something along the lines of the following pseudo-code:

    For Each EmailBody In EmailCollection
      EmailBody.Copy
      With wdDoc.Range
        .Characters.Last.PasteSpecial
        .Collapse 0 'wdCollapseEnd
        .InsertAfter vbCr
      End With
    Next


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Black Santa Saturday, February 15, 2014 4:55 PM
    Saturday, February 15, 2014 10:06 AM
  • Thanks macropod,

    The characters.last.pastespecial worked.

    Saturday, February 15, 2014 4:55 PM