+ Reply to Thread
Results 1 to 16 of 16

Copy an Outlook Email Contents and Paste in a New Email Keeping Format

  1. #1
    Forum Contributor
    Join Date
    09-23-2015
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    187

    Copy an Outlook Email Contents and Paste in a New Email Keeping Format

    Hello,

    I am running a macro that needs to do 2 things. Copy the contents of an email in Outlook Inbox, Pasting these contents in a new email while keeping all formating, tables, colors, and pictures and lastly adding a text beneath the email, some sort of disclaimer I need to include.

    Right now my code below will use a UserForm, user will paste the subject of their email they are looking for and macro will run trying to find that email. Please see commented parts of the code.

    Please Login or Register  to view this content.
    right now this code works, but it does not keep formating. Also I need it to keep the pictures and keep all as if it's original
    here is a screenshot done with a test email (ad for McDonald)
    test.PNG
    As you can see, none of the logos, colors, formats stayed

    Additionally I would like to add a text at the very bottom of the email.
    For illustrative purposes you can put this if you decide to help me, ill replace it with the proper text"
    text = "this is an example text"

    I would really appreciate any help there is out there.

    Thank you in advance
    Last edited by lougs7; 11-13-2020 at 06:52 PM.

  2. #2
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Copy an Outlook Email Contents and Paste in a New Email Keeping Format

    more than likely your copy statement is not grabbing the format of the content you want because the RETURN is associated with an ms word document object model. see here:

    https://docs.microsoft.com/en-us/off...tor.wordeditor

    furthermore, you've got inconsistencies with these 2 blocks of code:
    Please Login or Register  to view this content.
    and even further, these 2 lines are redundant:
    Please Login or Register  to view this content.
    lastly, I would seriously doubt that this line:
    Please Login or Register  to view this content.
    would have any compatibility with this line:
    Please Login or Register  to view this content.
    formatted text in a word document is not HTML. it has no tags. so, a little work to do here.

    on a side note, I've worked for many large companies in my lifetime, and I can tell you, if you do work for McDonalds, that you folks could benefit greatly from a migration away from MS office products. Ever worked with Python? It has a huge amount of resources inside of it that allows it to work with almost any email client in the world. It is widely recognized the world over, and for good reason. If you want to know what it can do, are curious about it, or anything else, let me know and I'll give you some insight. You don't even have to pay me.

  3. #3
    Forum Contributor
    Join Date
    09-23-2015
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    187
    Thanks for your reply! I don’t work for McDonalds btw, that email was in my junk as an example, because it’s similar to the emails I want to forward.

    There is clearly inconsistencies in my code, unfortunately I can’t code very well and found a few things online but clearly they don’t apply here.

    Can you suggest a way I can Copy all the content of an email, and can I call it back to the body of a new email? Can that be done?

    FYI thank you for offering your time if I get this I may have questions for you

  4. #4
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Copy an Outlook Email Contents and Paste in a New Email Keeping Format

    Quote Originally Posted by lougs7 View Post
    Can you suggest a way I can Copy all the content of an email, and can I call it back to the body of a new email? Can that be done?
    more than likely, yes. see below. (and keep in mind that any code in this post that has ''tags'' in it, which are "<" and ">" symbols,
    have been spaced out to get around this forum's security software. If you use these examples, take the spaces out of the code. also images 4, 5 and 6 are in the next post.

    based on the image you showed in post #1, it is clear, more than likely, the reason that the images are not showing is because you are doing what I said you were doing, and that was using the inspector. the first thing you need to do is determine whether the images in the email you originally received were embedded in the body of the message, or if they were linked. based on the fact that your forward attempt shows them as broken bitmap icons, more than likely they are embedded images. and there is no way for me to give you an answer on this because I do not know where you received the email from (client used, company you got it from (I assume McDonalds?), type of server that sent it, etc, etc....). so, the best I could do to help you in this regard was to run my own test. image #1 below shows where you can switch the type of email you are sending. image #2 shows you that you cannot embed images when sending plain text new mail items (that is the default format). and, per the first response in this thread: https://answers.microsoft.com/en-us/...5-48b3d41a2638

    image #3 shows that you can embed images into a new mail item when you switch to HTML-based composition. image #4 shows you what an HTML email with an embedded image looks like when received through outlook. image #5 shows what results when the code below is run on the email received in image #4. Obviously the picture is not copied. So what that tells us is that the broken images seen in post #1 that come from you attempting to forward the original email, are a result of linked images, not embedded ones. furthermore, you can test what I've just said by printing out the HTML code of the original email you received by running code like this (modified from your original code):
    Please Login or Register  to view this content.
    and when I printed the HTML source out for the email I used in this test, using the .HTMLbody property of a mail item object, what I got (in part) is in attachment #1.

    so, obviously, you can see that the embedded image I used for the test is read by the email client as the code that is in attachment 2.

    and thus, as a last resort to test what needs to be tested, in order to help you accomplish your goal, the following new code produced what you see in image #6:
    Please Login or Register  to view this content.
    Quote Originally Posted by lougs7 View Post
    FYI thank you for offering your time if I get this I may have questions for you
    sure.

    so....with everything I've just given you, do you think you can run with it? Why not give it a try and then post back if you can't get the last little bit. deal?
    Attached Images Attached Images
    Attached Files Attached Files

  5. #5
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Copy an Outlook Email Contents and Paste in a New Email Keeping Format

    images 4, 5 and 6 attached here:
    Attached Images Attached Images

  6. #6
    Forum Contributor
    Join Date
    09-23-2015
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    187

    Re: Copy an Outlook Email Contents and Paste in a New Email Keeping Format

    Hi Phil,

    Wow thank you for taking the time to explain in detail the mechnaics of how this works. I did the edits mentioned to arrive at this edited code, the code breaks on me, perhaps I am not setting it up correctly?

    Please Login or Register  to view this content.
    imgHTML = Mid(strHTML, imgStart, imgEnd - imgStart) this line returns the runtime error 5: Invalid procedure call or argument. Am I doing this right?

    Lastly I commented on this part, I wasn't sure if I take this out since olInsp is a Word thing, as you explained
    Set olInsp = .GetInspector ' do I delete this from my old code or keep?
    Set wdDoc = olInsp.WordEditor 'do I delete this from my old code or keep?
    Set rng = wdDoc.Range.FormattedText 'do I delete this from my old code or keep?

    If you can help me cross the finish line and tutor me on this last bit I would be tremendously grateful! Thanks Phil waiting to hear back

  7. #7
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Copy an Outlook Email Contents and Paste in a New Email Keeping Format

    Quote Originally Posted by lougs7 View Post
    Hi Phil,
    before I help you more, I thought I would point out the fact that my name is NOT ''Phil''. was that a typo, or does my signature line really say that!? Are Ali and Dibbins playing tricks on me or what? I'm not that much of a jerk.
    Last edited by vba_php; 11-14-2020 at 03:37 PM.

  8. #8
    Forum Contributor
    Join Date
    09-23-2015
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    187

    Re: Copy an Outlook Email Contents and Paste in a New Email Keeping Format

    Wow, im sorry, why was I under the impression your name was VBA Phil, it's definitly Adam lol its clearly in the comments you put. I apologize for that Adam :O ... Any further help is tremendously appreciated.

  9. #9
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Copy an Outlook Email Contents and Paste in a New Email Keeping Format

    Quote Originally Posted by lougs7 View Post
    Wow, im sorry, why was I under the impression your name was VBA Phil.
    not too bad. here's some funny info for you, in a variety of languages:

    PHP
    ""VBA Phil"" != ""vba_php"", however ""VBA_P"" = ""vba_p""
    https://www.php.net/manual/en/langua...comparison.php

    C#.NET
    ""VBA Phil"" != ""vba_php"", and ""VBA_P"" != ""vba_p""
    https://docs.microsoft.com/en-us/dot...lity-operators

    VB.NET
    ""VBA Phil"" <> ""vba_php"", and ""VBA_P"" <> ""vba_p""
    https://docs.microsoft.com/en-us/dot...ison-operators

    JAVASCRIPT
    ""VBA Phil"" != ""vba_php"", however ""VBA_P"" = ""vba_p""
    https://devdocs.io/javascript/operators/inequality

    PYTHON
    ""VBA Phil"" != ""vba_php"", however ""VBA_P"" == ""vba_p""
    https://docs.python.org/3/library/op...s-to-functions

    C++
    ""VBA Phil"" != ""vba_php"", however ""VBA_P"" == ""vba_p""
    https://docs.microsoft.com/en-us/cpp...d-exclpt-equal

    C
    ""VBA Phil"" != ""vba_php"", however ""VBA_P"" == ""vba_p""
    https://docs.microsoft.com/en-us/cpp...?view=msvc-160

    JAVA
    ""VBA Phil"" != ""vba_php"", however ""VBA_P"" = ""vba_p""
    https://docs.oracle.com/javase/tutor...opsummary.html

    Gosh, I hope I got all those right! Good learning material there. OK OK, enough time wasted. I will look at your issue now....

  10. #10
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Copy an Outlook Email Contents and Paste in a New Email Keeping Format

    Quote Originally Posted by lougs7 View Post
    imgHTML = Mid(strHTML, imgStart, imgEnd - imgStart) this line returns the runtime error 5: Invalid procedure call or argument. Am I doing this right?
    Nope. see below images:

    image 1: you kept the image-based HTML code that I used for my test. every image generates different HTML code silly! ha ha ha. thus, change it via this process:

    print the HTML content out like I mentioned in a previous post by running through the HTMLbody property and using debug.print. either that, or run the code you just tried and when it breaks, write ?strHTML in the immediate window. copy all of it into notepad. see image 2. copy that code into this line as a replacement for the old. you should know how by now:
    Please Login or Register  to view this content.
    image 2: me searching for the actual image code that outlook spit out when I created a test email and manually embedded an image into it using outlook's interface menu.

    image 3: my replacement code, put into VBA.

    image 4: the test email I created manually.

    image 5: the result of running the code I gave you on my test email.

    Quote Originally Posted by lougs7 View Post

    Lastly I commented on this part, I wasn't sure if I take this out since olInsp is a Word thing, as you explained
    Set olInsp = .GetInspector ' do I delete this from my old code or keep?
    Set wdDoc = olInsp.WordEditor 'do I delete this from my old code or keep?
    Set rng = wdDoc.Range.FormattedText 'do I delete this from my old code or keep?
    I kept all of that in my example, but none of those lines are really needed.

    Keep in mind that I just run this new test using the following rules:

    1) ran the test in a single instance of outlook. did not run it from excel. and thus, had to do this:
    Please Login or Register  to view this content.
    2) added new code:
    Please Login or Register  to view this content.
    you do not have to do any of that because you are running things on your end from inside excel.

    3 images are attached here. 2 are found in the following post.
    Attached Images Attached Images

  11. #11
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Copy an Outlook Email Contents and Paste in a New Email Keeping Format

    ......................
    Attached Images Attached Images

  12. #12
    Forum Contributor
    Join Date
    09-23-2015
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    187

    Re: Copy an Outlook Email Contents and Paste in a New Email Keeping Format

    Adam thank you for the reply. Bare with me as your dealing with an amature coder. Look im not sure I am going about this correctly. Perhaps this whole copy the Email and creating a new one is not the ideal route for what I am trying to do. Having to debug print copy results in a notepad, etc mayb be a pain point of doing it this way, would liek it to be automatic. If I go back to square 1 and tell you my intentions maybe that is easier.

    Basically what I am trying to do, a user in Excel uses UserForm, copy pastes Email subject they would like to forward + adds To: list they want. And this macro will Launch a new Forwarded email to a designated list. What I need to do is to input a sort of disclaimer at the bottom of orignal email.

    Right now I have a functional code that forwards a new email. This technique keeps all my formating and tables etc. Perhaps I think its easier if I keep using Forward instead of what I was trying with you to copy the contents and creating a new email. The issue is that the disclaimer, which I have added to my signature, gets added to the top blank portion, where I want to include it beneath the text of the orginal Contents. Note: I am a noob, instead of coding the disclaimer/custome text, as a work around I added it to my signature, that one the signature gets added my default displays my disclaimer, however I don't need the signature, just the text.

    (Note: I am not on my work computer so used this in my outlook, the exmaple is a random Facebook email, but in reality I will be forwarding company memos, research with all their specific formats and images etc)

    Here is what the code currently produces (see screenshot 1)
    Screenshot 1
    Capture.PNG

    However as you can see the signature or the disclaimer I want to add is being added ot the top blank poriton, where I need it beneath the original email.

    I am trying to accomplish this:
    sample.png


    Here is my orignal code, do you have any reccomendations of what edits need to be done to it to add this custom string beneath original email(ill copy paste the disclaimer later for illustractive purposes I put "my disclaimer goes here" )

    Current orginal functioning code:
    Please Login or Register  to view this content.
    I owe you a beer or 2, im so sorry to go back on things that we alreayd did, but I feel like I should have told you this from the start. I promise this is the last time I pester you, your help has been really appreciated so far! I understand its not your job to figure this out for me but ive been trying for days and your the closest thing that has helped me get this done. Really would appreciate if you reply with any further edits I can do here. Thnaks a lot in advance

    Edit: ignore the mcdonalds screenshot don't know how to delete that
    Attached Images Attached Images
    Last edited by lougs7; 11-14-2020 at 05:51 PM.

  13. #13
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Copy an Outlook Email Contents and Paste in a New Email Keeping Format

    Well I looked up how to insert a signature object into an Outlook email message using code and everyone says it's not possible. For some reason that surprises the heck out of me because Outlook with every other software program in the world has its own object model and every little entity in it should be included in code somewhere. But apparently this is a rare case.

    So it seems like you have two options here to get a custom string inserted at the end of the message:

    1) you use the default signature that you set manually and then do what they said in that stack overflow link, or

    2) you append the string to the end of the HTML body property of the email object like I showed you two posts ago. Do you remember me doing that? That was part of the printing out of the HTML source code as a teaching mechanism for you.

    So those two methods are readily available to you if all you want to do is insert your signature at the bottom of the message and everything else is working for you at this time. By the way I don't drink beer it will have to be tea or something else that I can't think of at the moment.

  14. #14
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Copy an Outlook Email Contents and Paste in a New Email Keeping Format

    and I'm sorry about talking about a stack overflow Link in my last post but I can't post links to that forum because the moderators here say that's breaking the rules because it's a competing forum to this one. So if you want to see that link, Google these terms:

    Www.google.com/search?q=outlook+vba+signature

  15. #15
    Forum Contributor
    Join Date
    09-23-2015
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    187

    Re: Copy an Outlook Email Contents and Paste in a New Email Keeping Format

    Okay thanks a lot, it seems that adding the signature will only add it to the top part of the Forward email, not beneath the original email contents being forwarded. Appending the string to the HTML body property too, I believe will add the text to the new body. This is awfully confusing given my low level of expertise coding. Im going to keep trying with what you originally gave me, I wasn't sure if I was editing the code correctly, not going to lie the copying to the Notepad part confused me a bit, ideally the code runs on its own without someone copying to note pad. I will keep trying... You have been really helpful thanks for that

  16. #16
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Copy an Outlook Email Contents and Paste in a New Email Keeping Format

    the copy process to notepad was strictly for the purpose of teaching you how to do things and solve problems on your own :-)

    there are a million different programs that are compatible with the Windows operating system and you can use virtually any of them to do work around methods regardless of what you're doing via automation or coding work. I use programs all the time for purposes that they are not intended for strictly to get work done especially for corporate people because corporate people have no idea what they're doing and most of them fight endlessly all day long because they're bored to tears. :-) and that's what gives people like me work to do because they constantly contact consultants to try and help fix the issues that they cause themselves due to their political infighting on an endless basis LOL

    But back on point here..... You are very close to the solution anyway and I could probably produce one for you with regard to code that does exactly what you want that runs from inside an Excel application but I would probably have to be paid a little money for it because that's a lot of work. But then again you've already got most of it produced yourself. That's obvious based on the screenshots that you've posted after your test runs on your end. So if you need any more help just ask

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Email Macro paste image in to email and sent to list in designated range Outlook
    By aaron061883 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-12-2020, 10:51 AM
  2. Copy Cell Contents into outlook email
    By ahalliwell in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-01-2018, 01:43 PM
  3. [SOLVED] Adding a Copy and Paste VBA into Outlook Email
    By burcha22 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 04-05-2017, 05:05 PM
  4. Macro - copy contents of word doc to outlook email inc jpeg
    By sipa in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-02-2016, 10:14 AM
  5. With AutoFilter - copy email address in active cell & paste in To field of Outlook email
    By carmen.swanson in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-05-2015, 09:40 PM
  6. Replies: 2
    Last Post: 08-01-2012, 02:47 PM
  7. Email using Excel VBA and Keeping Outlook Signature on the Email
    By Shama in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-13-2009, 07:39 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1