+ Reply to Thread
Results 1 to 15 of 15

How can I send a sheet in an Automated email with VBA?

  1. #1
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Question How can I send a sheet in an Automated email with VBA?

    I posted this question here originally http://www.excelforum.com/excel-prog...ml#post4128075 and realized that What I am asking may not be clear so I am basically starting over with the original unmodified codes.

    I have a macro (1st code below) that after the "Before" sheet has been updated, it will create a new sheet named "End of Shift Report" and copy the headers over from the before sheet. Then it searches in column A for currentuser and then column B for current date. Once it finds those matches, it pastes only those rows to the end of shift report sheet. All of this works absolutely wonderful.

    I have a RangetoHTML function (2nd code below) that I would like to put the end of shift report sheet into the body of the email as HTML. All it does right now is send a blank email with a blank attachment. I don't want an attachment, I want ot to be placed in the email body.

    1st code (macro)
    Please Login or Register  to view this content.
    2nd code (function)
    Please Login or Register  to view this content.
    Is there a way to integrate the macro code into the function code? Or am I going about this all wrong?
    1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG
    You don't have to add Rep if I have helped you out (but it would be nice), but please mark the thread as SOLVED if your issue is resolved.

    Tom

  2. #2
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: How can I send a sheet in an Automated email with VBA?

    Anyone have an idea of what I can do here, or if its even possible?

  3. #3
    Forum Contributor Gregor y's Avatar
    Join Date
    10-24-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2010 32-Bit
    Posts
    280

    Re: How can I send a sheet in an Automated email with VBA?

    hopefully this one will get you one step closer to your destination

    Please Login or Register  to view this content.

  4. #4
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: How can I send a sheet in an Automated email with VBA?

    Gregor Y,
    Thank you for you assistance with this, I really do appreciate it. I have run into some issues with the above code and am having problems figuring them out. at the following line, I get a Compile error: Sub or Function not defined,
    Please Login or Register  to view this content.
    With the RangetoHTML part selected. I have tried to declare is as String, Object, Variant, Range, pretty much everything I can think of which does allow the Function Macro1a code to run, After the function runs and the new sheet is created and it returns to the Private sub I get a Run-Time Error 91: Object variable or with block variable not set (at the above line). I have also tried to add With before the EmailHTMLBody line but that doesn't work either. My knowledge of VBA is very, VERY limited so now I am at a loss. Also, If I do get it to go past that line, it runs the finction and tries to create the sheet again, which of course errors out as 2 sheets can not have the same name.
    If I comment out the line, I get errors in the function code........
    Last edited by gmr4evr1; 07-24-2015 at 12:26 PM. Reason: Corrected typos

  5. #5
    Forum Contributor Gregor y's Avatar
    Join Date
    10-24-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2010 32-Bit
    Posts
    280

    Re: How can I send a sheet in an Automated email with VBA?

    hmm, try splitting up that line as follows and see which errors out.

    Please Login or Register  to view this content.

  6. #6
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: How can I send a sheet in an Automated email with VBA?

    That didn't work either. I'm probably doing something on my end that is wrong. With the Private Sub and Function codes that you provided me, how do I call them? What I did was change the Private Sub Macro1a_Example() to Private Sub CloseButton_Click(), as that is how my original macro.functions were called. SO, with you last changes, this is how the codes look...
    Please Login or Register  to view this content.
    I still get a Compile error: Sub or Function not defined with RangetoHTML(r) highlighted in blue.

    Edit* I had removed the original RangetoHTML code that I had. I put it back in and now I get another error - Compile error: Expected Function or variable with EmailHTMLBody highlighted in blue
    Last edited by gmr4evr1; 07-24-2015 at 06:49 PM.

  7. #7
    Forum Contributor Gregor y's Avatar
    Join Date
    10-24-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2010 32-Bit
    Posts
    280

    Re: How can I send a sheet in an Automated email with VBA?

    Private makes a sub/function invisible to the outside world including the Excel runtime environment which is what associates the button event to the VBA code.

    The Macro1a_examples() sub was meant to be an example listing of four different ways you could call the Macro1a function to suit your needs.

    I'd suggest something along the lines of:
    Please Login or Register  to view this content.
    to try out the first example from Macro1a_Examples sub

  8. #8
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: How can I send a sheet in an Automated email with VBA?

    I did try but lost where I am because now I have about 8 different codes/functions and don't know what to keep, delete, change or whatever it is I am supposed to do. I'm thinking that I might just have to start all over from a blank workbook, which is the worst option available to me...I think. I will have to try this tomorrow. I leave work in less than an hour. Again, thank you for your help with this. Even if we cant get it figured out (which is probably because of something I've done, or will do lol), I still appreciate you helping me.

  9. #9
    Forum Contributor Gregor y's Avatar
    Join Date
    10-24-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2010 32-Bit
    Posts
    280

    Re: How can I send a sheet in an Automated email with VBA?

    i believe this should more or less be a clean set

    Please Login or Register  to view this content.
    i also have some aprehensions about the size of a string in office 2010/2007, but we can save that for a different day if it ends up cropping up here.

  10. #10
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: How can I send a sheet in an Automated email with VBA?

    Thank you, I will check this out first thing tomorrow morning.

  11. #11
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: How can I send a sheet in an Automated email with VBA?

    Gregor Y,
    Thank you for the code, I did get it to work with some minor tweaks. However, I am getting an odd MsgBox that I haven't seen before. I am assuming it is supposed to pop up per the Public Sub CloseButton_Click() code. If this is the case, then all is well?? Now, If I replace your Public Sub CloseButton_Click() code with my email code, I get a different error that I had before and had it figured out, now I have to figure it out again Oh happy happy joy joy.

  12. #12
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: How can I send a sheet in an Automated email with VBA?

    Gregor Y,
    On my way to JUMPING for JOY!!!!!!! After a lot of trial and error with putting the right codes in after you supplied me with the clean version, it is finally doing what I want it to do with one kind of minor but important exception. The format in the email isn't correct, see attached Sheet 1 for an explanation. The sample on the left is exactly what is being sent in the email (YAY! for getting this part finally), the sample on the right is the way it should be sent.
    Gergor Y clean code Formatting issue.xlsx
    It doesn't seem to be auto adjusting the cells according to the text in the cells. If I need to create a new thread for this, let me know.

    Update* I figured out how to get the date column A to adjust by adding
    Please Login or Register  to view this content.
    into the code I am using, but still need the last column, column F to auto adjust to the text in the cell as it will vary each time.
    Last edited by gmr4evr1; 07-25-2015 at 04:56 PM. Reason: Added update

  13. #13
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: How can I send a sheet in an Automated email with VBA?

    Shazam, boo-ya!, yippie, yee-haw, and all other words of excitement! I got everything to this point figured out. I found the code below
    Please Login or Register  to view this content.
    to auto adjust the cells and it works great for all sheets when placed in the workbook.
    I also discovered that I needed to delete the new sheet that was created as the workbook is being saved on a server and is updated daily so we didn't need all the extra sheets that we were going to be getting. It took me a little tinkering, but, I got it to work with the code below
    Please Login or Register  to view this content.
    Thank you again Gregor Y! The next time I am in CO. or you are in Tx., I owe you a beer or 12. I have been working on this (I hate to admit) on and off since November, Even our IT department couldn't get it figured out. Once you gave me the "clean" code everything fell into place. I am marking this as SOLVED.......FINALLY!!!!!!!!!
    P.S. I know that once this goes "live" at work, I will have 'macros not enabled issues' for some/all end users, but, I will post another thread for that when I get to that hurdle. Sheesh, hopefully it's just a hurdle and not another gigantic bridge!
    Again, THANK YOU Gregor Y!
    Last edited by gmr4evr1; 07-26-2015 at 03:16 PM. Reason: Typo

  14. #14
    Forum Contributor Gregor y's Avatar
    Join Date
    10-24-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2010 32-Bit
    Posts
    280

    Re: How can I send a sheet in an Automated email with VBA?

    Here is the updated CloseButton_Click event to include the two updates you've detailed above.

    Please Login or Register  to view this content.
    a couple notes:
    • I believe application.screenupdating=false turns itself back on at the end of the currently running macro so it should be alright, all be it a bit sloppy, to not include application.screenupdating=true
    • however application.displayalerts=false does not turn itself back on, and you should include application.displayalerts=true. Everything will appear to run fine, but subbtle things like just closeing out of excel without prompting to save your work first will happen.
    • While putting the autofit in the worksheet's change event will technically accomplish your goal, it is then running the autofit everytime the worksheet changes and thus slowing down your code runtime considerably, by putting it in the code above i've made it run once at the end.

    also, thanks to the Melissa virus trying to get a company whome doesn't have a decently implemented PKI to run the macro code is going to be an uphill battle.

  15. #15
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: How can I send a sheet in an Automated email with VBA?

    Thanks for the updated code and the info. I will check with out IT dept. and find out what PKI we have, if any.
    When I get back to work tomorrow, I will look through the code to see if/where I have set things back to True. In the CloseButton_Click event I have code toward the end that deletes the new sheet automatically. I set the DisplayAlerts to false so the default message wouldn't pop up asking them if they were sure they wanted to delete the sheet. Or, I think I will just save it as another workbook, add your updated code to it and see how things go.

    Again, many, many, MANY thanks for helping me with this.

+ 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] Automated Email VBA from Excel - Freezes before send
    By ELeGault in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 12-16-2014, 12:37 AM
  2. Send automated email if value in cell is value
    By DelKolio in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-16-2014, 08:53 AM
  3. Macro to send automated email reminders
    By Amber12 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 02-13-2014, 11:22 AM
  4. command button VBA to send automated email not workbook or worksheet
    By lpratt in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-08-2012, 11:13 PM
  5. Automated email to send worksheet as report
    By wellseytd in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-10-2011, 10:28 AM

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