Results 1 to 13 of 13
  1. #1
    2 Star Lounger
    Join Date
    Feb 2001
    Posts
    195
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Copy an object in the body of another one? (2003)

    With Outlook it is possible to drag and drop an item in the body of another item. For instance to put an e-mail in the body of a task.
    How can we program it? I have try to use the e-mail item but ther is not a method to copy the entire item. So I suppose that we can do it using the object element but once again I haven't found a method to do it.
    Any suggestion?
    Thank you and regards,
    Marcel

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Copy an object in the body of another one? (2003)

    Dragging an item into the body of another item results in an attachment, so the method to do this in code is Attachments.Add. The following example is from the Outlook VBA help:
    <hr>This Visual Basic for Applications example creates a new mail message and attaches the first contact in the default Contacts folder.

    Set myOlApp = CreateObject("Outlook.Application")
    Set myNameSpace = myOlApp.GetNameSpace("MAPI")
    Set myFolder = _
    myNameSpace.GetDefaultFolder(olFolderContacts)
    Set myFirstContact = myFolder.Items(1)
    Set myItem = myOlApp.CreateItem(olMailItem)
    Set myAttachments = myItem.Attachments
    myAttachments.Add myFirstContact<hr>
    I hope you can adapt this for your situation. If you run the code from within Outlook, you don't need to create an Outlook.Application object, just use Application instead of myOlApp.

  3. #3
    2 Star Lounger
    Join Date
    Feb 2001
    Posts
    195
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copy an object in the body of another one? (2003)

    Hans,
    Thank you, I was able to use your recommendation. I have written the code below to transfer an e-mail in a new task, maybe could be useful to somebody.
    However, I have tried to find a way to replicate the copy to folder command when you use it with the e-mail opened. This way outlook copy the e-mail as attachment but also copy all the message with the header on text form. Unfortunately, I haven't succeed on this research. Do you have any ideas?

    Sub CopyToTask()

    Dim myOlApp As Outlook.Application
    Dim itmTask As TaskItem
    Dim itmMail As MailItem
    Dim myExplorer As Explorer
    Dim oFolder As Outlook.MAPIFolder
    Dim oSpace As Outlook.NameSpace
    Dim appOutlk As Outlook.Application
    Dim myFolder As Outlook.MAPIFolder


    Set myOlApp = Outlook.Application
    Set oSpace = myOlApp.GetNamespace("MAPI")
    Set selMail = Application.ActiveExplorer.Selection
    Set myExplorer = Application.ActiveExplorer


    If myExplorer.CurrentFolder.DefaultItemType = olTaskMail Then
    Set myFolder = oSpace.GetDefaultFolder(olFolderTasks)
    Set itmMail = selMail.Item(1)
    Set itmTask = myOlApp.CreateItem(olTaskItem)
    itmTask.Subject = itmMail.Subject
    itmTask.StartDate = Now()
    itmTask.TotalWork = 15
    itmTask.Attachments.Add itmMail
    itmTask.Display
    Else
    MsgBox "The folder must be a mail folder.", vbCritical, "Mail transfer"
    End If
    End Sub

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Copy an object in the body of another one? (2003)

    I'm sorry, I don't understand your new question. Could you try to explain again? (If it's easier, you can put it in French)

  5. #5
    2 Star Lounger
    Join Date
    Feb 2001
    Posts
    195
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copy an object in the body of another one? (2003)

    I have attached a screen copy of the result when I do the following:
    - open the e-mail
    - use copy to folde
    - select Task folder
    Then I have the attachment but also the text of the e-mail and some information from the header.
    May be I can do it by creating the body of the task with strings (From:, Date and some properties (itmmail.body and so on). But I was wondering if there was a way to copy the e-mail in one shot.
    Regards,
    Marcel

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Copy an object in the body of another one? (2003)

    The VBA equivalent of the Copy to Folder method is like this:

    Dim nsp As NameSpace
    Dim fldSource As MAPIFolder
    Dim fldTarget As MAPIFolder
    Dim itm As MailItem

    Set nsp = Application.GetNamespace("MAPI")
    Set fldSource = nsp.GetDefaultFolder(olFolderInbox)
    Set itm = fldSource.Items(1).Copy
    Set fldTarget = nsp.GetDefaultFolder(olFolderTasks)
    itm.Move fldTarget

    In other words, first copy the item, then move the copy to another folder.

  7. #7
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Copy an object in the body of another one? (2003)

    Don't forget to

    itmTask.Save

    I often forget to code it. One other subtle thing. Since selMail is declared as a Mailitem

    Set selMail = Application.ActiveExplorer.Selection

    will error out if ActiveExplorer.Selection(1) is not a MailItem

    I usually declare selMail as an Object and then test for its Outlook Class:

    If selMail.Class = olMail Then

    That kind of test becomes more important when you loop through all Items in a Folder and there's some unexpected Items of another Class.
    -John ... I float in liquid gardens
    UTC -7ąDS

  8. #8
    2 Star Lounger
    Join Date
    Feb 2001
    Posts
    195
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copy an object in the body of another one? (2003)

    I ahve tried your suggestion and I got two issues:
    1.- a copy of the e-mail stay in the e-mail folder. I have try to use the delete method but it didn't work
    2.- how to open the moved item in orrder to complete the requested information

    This is the code:
    Sub CopyToTask()

    Dim myOlApp As Outlook.Application
    Dim itmTask As TaskItem
    Dim itmMail As MailItem
    Dim myExplorer As Explorer
    Dim oFolder As Outlook.MAPIFolder
    Dim oSpace As Outlook.NameSpace
    Dim appOutlk As Outlook.Application
    Dim myFolder As Outlook.MAPIFolder
    Dim fldTarget As MAPIFolder
    Dim inti As Integer


    Set myOlApp = Outlook.Application
    Set oSpace = myOlApp.GetNamespace("MAPI")
    Set selMail = Application.ActiveExplorer.Selection
    Set myExplorer = Application.ActiveExplorer


    If (myExplorer.CurrentFolder.DefaultItemType = olTaskMail) And (selMail.Count <> 0) Then
    For i = 1 To selMail.Count
    Set myFolder = oSpace.GetDefaultFolder(olFolderTasks)
    Set itmMail = selMail.Item(i).Copy 'crate a physical copy?
    Set fldTarget = oSpace.GetDefaultFolder(olFolderTasks)
    itmMail.Move fldTarget
    Set myExplorer = fldTarget.GetExplorer
    myExplorer.Display
    Next i
    Else
    MsgBox "The selection must be a mail.", vbCritical, "Mail transfer"
    End If
    End Sub

    Thanks and regards,
    Marcel

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Copy an object in the body of another one? (2003)

    In the first place, your code shows that you don't require variables to be declared explicitly. I STRONLGY recommend the following:
    a) Turn on 'Require variable declaration' in Tools | Options (in the Visual Basic Editor)
    [img]/forums/images/smilies/cool.gif[/img] Insert a line Option Explicit at the top of existing modules.
    This will be irritating at first, but it will save you a lot of grief in the end. For example, you declare a variable inti but use i in the code, and you use a non-existing constant olTaskMail.

    If you use Copy, the copied item remains behind despite the Move method. If you don't use Copy, the original mail is moved to the Deleted Items folder. This seems a bug in Outlook VBA to me, and I don't know how to work around it (I always feel I am lost in an alien world when trying to write Outlook code)
    The following version doesn't use copy. It opens the task item:

    Sub MoveToTask()
    Dim oSpace As NameSpace
    Dim myExplorer As Explorer
    Dim selMail As Selection
    Dim itmMail As MailItem
    Dim itmTask As TaskItem
    Dim fldTarget As MAPIFolder
    Dim i As Integer

    Set oSpace = Application.GetNamespace("MAPI")
    Set selMail = Application.ActiveExplorer.Selection
    Set myExplorer = Application.ActiveExplorer

    If (myExplorer.CurrentFolder.DefaultItemType = olMailItem) And _
    (selMail.Count <> 0) Then
    For i = 1 To selMail.Count
    Set itmMail = selMail.Item(i) 'create a physical copy?
    Set fldTarget = oSpace.GetDefaultFolder(olFolderTasks)
    Set itmTask = itmMail.Move(fldTarget)
    itmTask.Display
    Next i
    Else
    MsgBox "The selection must be a mail.", vbCritical, "Mail transfer"
    End If
    End Sub

  10. #10
    2 Star Lounger
    Join Date
    Feb 2001
    Posts
    195
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copy an object in the body of another one? (2003)

    OK this time I think I have reached my target by adding just one line: I have added the copy line after the move one and then the mail is moved and stay in the mail folder.
    Thank you for your help.
    Regards,
    Marcel

    Sub CopyToTask()

    Dim myOlApp As Outlook.Application
    Dim itmTask As TaskItem
    Dim itmMail As MailItem
    Dim myExplorer As Explorer
    Dim oFolder As Outlook.MAPIFolder
    Dim oSpace As Outlook.NameSpace
    Dim appOutlk As Outlook.Application
    Dim myFolder As Outlook.MAPIFolder
    Dim fldTarget As MAPIFolder
    Dim selMail As Selection
    Dim i As Integer


    Set myOlApp = Outlook.Application
    Set oSpace = myOlApp.GetNamespace("MAPI")
    Set selMail = Application.ActiveExplorer.Selection
    Set myExplorer = Application.ActiveExplorer


    If (myExplorer.CurrentFolder.DefaultItemType = olMailItem) And (selMail.Count <> 0) Then
    For i = 1 To selMail.Count
    Set fldTarget = oSpace.GetDefaultFolder(olFolderTasks)
    Set itmMail = selMail.Item(i)
    Set itmTask = itmMail.Move(fldTarget)
    Set itmMail = selMail.Item(i).Copy
    itmTask.Display
    Next i
    Else
    MsgBox "The selection must be a mail.", vbCritical, "Mail transfer"
    End If
    End Sub

  11. #11
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Copy an object in the body of another one? (2003)

    These Dims are never used:

    Dim oFolder As Outlook.MAPIFolder
    Dim myFolder As Outlook.MAPIFolder
    Dim appOutlk As Outlook.Application (and you already declared the Outlook.Application object)

    And you can change the order of these so as use one to help get the other:

    Set myExplorer = Application.ActiveExplorer
    Set selMail = myExplorer.Selection

    Reusing declared and set Objects that way will speed up your code. But you are really getting the hang of it!

    As written, the code places both the message as an attachment, and the message text, into the Task Body, and creates a new task for each message selected. Is that what you want?
    -John ... I float in liquid gardens
    UTC -7ąDS

  12. #12
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Copy an object in the body of another one? (2003)

    Here's a way to get the message as an attachment, but not the message text in the body:

    using your existing code ...
    If (myExplorer.CurrentFolder.DefaultItemType = olMailItem) And (selMail.Count <> 0) Then
    For i = 1 To selMail.Count ' for each message
    Set itmTask = myOlApp.CreateItem(olTaskItem) ' create a new task
    With itmTask
    .Subject = selMail.Item(i).Subject ' make the Task Subject the same as the Message Subject
    .Attachments.Add selMail.Item(i) ' add the Message as an attachment
    .Save
    .Display
    End With
    Set itmTask = Nothing
    Next i
    ... etc

    this way also makes a couple of other Declarations unneccessary, though that's not the objective. I hope my comment are helping some.
    -John ... I float in liquid gardens
    UTC -7ąDS

  13. #13
    2 Star Lounger
    Join Date
    Feb 2001
    Posts
    195
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copy an object in the body of another one? (2003)

    John,
    Thank you for your optimization. I have applied it to my code.
    Concerning the results, it is exactly what I was looking for. That is, duplicate the copy to folder command when the e-mail was opened. As I transfer quite a lot of e-mails to my task folder I was looking to do this transfer almost completely automatic. I have assign the code to a shortcut key.
    Thank you for your help.
    Regards,
    Marcel

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •