More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

Login

login
password
Remember me

You will need to register and log in if you want to download the source code for the Microsoft Outlook Programming book. The forums and code sharing areas are open to both registered and non-registered visitors.

share code 09-Sep-2010 05:59

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

 

Code level: intermediate    Code area: Outlook Expert Techniques Printer Friendly Version
Title: OL2007: Copy formatted body between items
Description: This sample takes advantage of Word being the editor for all Outlook 2007 items except sticky notes and distribution lists.
Date: 03-Jul-2009  10:13
Code level: intermediate
Code area: Outlook Expert Techniques
Posted by: Sue Mosher
Body:
All 6comments
Page [ 1  
  03-Jul-2009  10:14   
Typical usage of this code would be to copy the body from an existing item to a new item, for example, copying from a message to a new task:

Sub TestCopyFullBody()
    Dim objMsg As Outlook.MailItem
    Dim objTask As Outlook.TaskItem
    
    Set objMsg = Application.ActiveExplorer.Selection(1)
    Set objTask = Application.CreateItem(olTaskItem)
    Call CopyFullBody(objMsg, objTask)
    objTask.Display
    
    Set objMsg = Nothing
    Set objTask = Nothing
End Sub
  05-Jul-2009  15:38   
Hi Sue, thanks for the help.. Rookie though.... what am i doing wrong
Sub TestCopyFullBody()
    Dim objMsg As Outlook.MailItem
    Dim objTask As Outlook.TaskItem
    
    Set objMsg = Application.ActiveExplorer.Selection(1)
    Set objTask = Application.CreateItem(olTaskItem)
    Call CopyFullBody(objMsg, objTask)
    objTask.Display
    
    Set objMsg = Nothing
    Set objTask = Nothing
End Sub
Sub CopyFullBody(sourceItem As Object, targetItem As Object)
    Dim objDoc As Word.Document
    Dim objSel As Word.Selection
    Dim objDoc2 As Word.Document
    Dim objSel2 As Word.Selection
    On Error Resume Next
    ' get a Word.Selection from the source item
    Set objDoc = sourceItem.GetInspector.WordEditor
    If Not objDoc Is Nothing Then
        Set objSel = objDoc.Windows(1).Selection
        objSel.WholeStory
        objSel.Copy
        Set objDoc2 = targetItem.GetInspector.WordEditor
        If Not objDoc2 Is Nothing Then
            Set objSel2 = objDoc2.Windows(1).Selection
            objSel2.PasteAndFormat wdPasteDefault
        Else
            MsgBox "Could not get Word.Document for " & _
                   targetItem.Subject
        End If
    Else
        MsgBox "Could not get Word.Document for " & _
               sourceItem.Subject
    End If
    Set objDoc = Nothing
    Set objSel = Nothing
    Set objDoc2 = Nothing
    Set objSel2 = Nothing
End Sub
  05-Jul-2009  16:47   
Kolzw, what actually happens when you run the code in Outlook VBA? You may want to comment out the On Error statement so you can see any errors more easily.
  27-Jul-2009  18:37   
I attempted to use the Sub CopyFullBody, but when I call it I receive a Compile error:

User-defined type not defined

On the first line of the Sub
  Dim objDoc As Word.Document

Any assistance is appreciated.
  27-Jul-2009  18:39   
S, you need to add a reference to the Microsoft Word programming library. In VBA, use Tools | References.
  27-Jul-2009  19:16   
Thank you, that did the trick. I hope I eventually catch on the this stuff.

Love the site.
Page [ 1  
Post your comment



name        email