none
Outlook 2007 Copy Email Address From Email To Field RRS feed

  • Question

  • There is a story how I need to do this. If there is an email that has not been sent yet, but is open, do we have a macro that copies the email address in the To field, and closes the email not sending it? Here is the code I got before with copies from a field in a contact, but not from an email and its field that is open:

    Public Sub CopyAddress()
    Dim oContact As contactItem

    Set oContact = ActiveExplorer().Selection.Item(1)
    Set DataObj = New MSForms.DataObject
    DataObj.SetText oContact.UserProperties("LinkedIn Webpage")
    DataObj.PutInClipboard
    End Sub
    Friday, June 27, 2014 4:11 PM

Answers

  • Public Sub CopyAddress()  Dim item  Dim inspector Dim addresses dim Recip set inspector =  Application.ActiveInspector  Set item = inspector.CurrentItem  addresses = "" for each Recip in item.Recipients if Len(addresses) > 0 Then addresses = addresses & "; " addresses = addresses & Recip.Address next Set DataObj = New MSForms.DataObject DataObj.SetText addresses DataObj.PutInClipboard    inspector.Close 0

    End Sub


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!




    Friday, June 27, 2014 7:19 PM
  • You need to pass the OlInspectorClose value to method:

    Sub CloseItem()  
     Dim myinspector As Outlook.Inspector  
     Dim myItem As Outlook.MailItem 
     
     Set myinspector = Application.ActiveInspector  
     Set myItem = myinspector.CurrentItem  
     myItem.Close olSave  
    End Sub

    Friday, June 27, 2014 6:46 PM

All replies

  • Your code works with the selected message, not open in an inspector. Try something like the following:

    Public Sub CopyAddress()
      Dim item
      Dim inspector
      set inspector =  Application.ActiveInspector
      Set item = inspector.CurrentItem
      item.Recipients.Add "user@company.com" 
      item.Save
      inspector.Close
    End Sub


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!


    Friday, June 27, 2014 4:18 PM
  • Thanks much...does this copy the email address and close the email I don't want to send?  And what does user@company.com mean for purposes of copying the email address in the To field of the email?

    Thanks so much as  I would love to get his done!!

    Friday, June 27, 2014 4:43 PM
  • You can see for yourself what the script does - it adds a recipient to the message (user@company.com means just some value, you would of course use your own value), save the message, and closes the inspector. It is pretty self-documenting, is it not?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, June 27, 2014 4:50 PM
  • Thanks again, but that's not what I am trying to do.  I just want to copy the email address of the recipient and close the email message so it is not sent.
    Friday, June 27, 2014 5:27 PM
  • Public Sub CopyAddress()  Dim item  Dim inspector Dim addresses dim Recip set inspector =  Application.ActiveInspector  Set item = inspector.CurrentItem  addresses = "" for each Recip in item.Recipients if Len(addresses) > 0 Then addresses = addresses & "; " addresses = addresses & Recip.Address next Set DataObj = New MSForms.DataObject DataObj.SetText addresses DataObj.PutInClipboard    inspector.Close

    End Sub



    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!


    Friday, June 27, 2014 5:39 PM
  • I will try it right now and let you know if it works...thanks so so much
    Friday, June 27, 2014 5:48 PM
  • It copies the email address as I pasted it to make sure it did it....but, it does not close the email message and he line inspector.Close shows up as yellow but no other error words
    Friday, June 27, 2014 5:56 PM
  • You need to pass the OlInspectorClose value to method:

    Sub CloseItem()  
     Dim myinspector As Outlook.Inspector  
     Dim myItem As Outlook.MailItem 
     
     Set myinspector = Application.ActiveInspector  
     Set myItem = myinspector.CurrentItem  
     myItem.Close olSave  
    End Sub

    Friday, June 27, 2014 6:46 PM
  • Use of the OlInspectorClose enums as an argument when calling Close:

      olSave = 0
      olDiscard = 1
      olPromptForSave = 2


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!


    Friday, June 27, 2014 6:51 PM
  • I don't understand what to change please.
    Friday, June 27, 2014 7:14 PM
  • I added the second code give above to close, and in the first code given, I changed   inspector.Close to "Call CloseItem"   and it runs the second code at the end.  Thanks very very much.
    Friday, June 27, 2014 7:18 PM
  • Public Sub CopyAddress()  Dim item  Dim inspector Dim addresses dim Recip set inspector =  Application.ActiveInspector  Set item = inspector.CurrentItem  addresses = "" for each Recip in item.Recipients if Len(addresses) > 0 Then addresses = addresses & "; " addresses = addresses & Recip.Address next Set DataObj = New MSForms.DataObject DataObj.SetText addresses DataObj.PutInClipboard    inspector.Close 0

    End Sub


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!




    Friday, June 27, 2014 7:19 PM
  • The next code I just got, does it all as well.  But here is next major problem, and if we can fix this right now, that would be wonderful!!

    When I am on another webpage where I have people that I am connected with through that webpage, for example. LinkedIn, in the contact person area on the webpage there is an email address and when I click on it, it creates an email to that email address, and when I run the code, it closes it, but did not copy the email address in the To field.

    What can we change so it copies from that email that is created when I click on an email address in the other webpage?


    • Edited by lmseaz Friday, June 27, 2014 8:17 PM
    Friday, June 27, 2014 7:31 PM
  • was the email underlined in the Outlook inspector?

    You can either call item.Recipients.ResolveAll first or use Recipient.Name instead of Recipient.Address.

    Have you tried to step through your code?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, June 27, 2014 8:17 PM
  • I don't understand the first thing you just said and what to change, and will try the Recipient.Name.....what is the other thing to try please?
    Friday, June 27, 2014 8:43 PM
  • I just tried the Recipient.Name which is Recip.Name   and it did it....thanks so so much!!!
    Friday, June 27, 2014 8:43 PM
  • This code copies the name of the FullName field if I open up the contact from the folder or from the calendar event. If i click on the email address of the person that sends me the email and it opens up his contact, this code does not work...so what to change here specifically please? Sub CopyFullName() Dim oContact As contactItem 'DataObj As MSForms.DataObject Set oContact = ActiveExplorer().Selection.item(1) Set DataObj = New MSForms.DataObject DataObj.SetText oContact.fullname DataObj.PutInClipboard End Sub
    Thursday, July 3, 2014 4:25 AM
  • Public Sub CopyAddress()  
      Dim item  
      Dim inspector
      dim Recip
      set inspector =  Application.ActiveInspector  
      Set item = inspector.CurrentItem  
      Set DataObj = New MSForms.DataObject 
      DataObj.SetText item.SenderEmailAddress
      DataObj.PutInClipboard 
      inspector.Close 0
    
    End Sub
    
    


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Thursday, July 3, 2014 5:18 AM
  • Based on what you showed above, and all the other help, I have the following two codes.  The first one copies the FullName of the Contact if I open the Contact from the folder, or select from the folder.....and the second code copies the FullName of the Contact when the contact is opened from the email message.....so it there away to make it one code that does all three of these areas I just mentioned?

    Sub CopyFullName()
    Dim oContact As contactItem

     
        Set oContact = ActiveExplorer().Selection.item(1)
        Set DataObj = New MSForms.DataObject
        DataObj.SetText oContact.fullname
        DataObj.PutInClipboard
    End Sub

    Sub CopyFullName2()
    Dim oContact As contactItem
    Dim item
      Dim inspector
      Dim Recip
      Set inspector = Application.ActiveInspector
      Set item = inspector.currentItem
     
       
        Set DataObj = New MSForms.DataObject
        DataObj.SetText item.fullname
        DataObj.PutInClipboard
    End Sub

    Thursday, July 3, 2014 2:42 PM
  • I have user forms and have command buttons in them and when I click on the command button it runs the code that comes to that command button based on the code of that command button. If I want to delete the command button, is there a way to delete the command button and the related code at the same time through a macro?
    Saturday, July 5, 2014 1:01 AM
  • Update from anyone please?
    • Edited by lmseaz Sunday, July 6, 2014 4:48 PM
    Sunday, July 6, 2014 4:47 PM
  • Hi,

    Since the thread has been existing for a long time, I would suggest you reposting a new thread to ask your questions so that other community menbers will join in.

    Good luck!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, July 8, 2014 7:23 AM
    Moderator