Results 1 to 15 of 15

Thread: Help sending a worksheet content in email body [RESOLVED]

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    32

    Resolved Help sending a worksheet content in email body [RESOLVED]

    Hi All,

    Basically I have a workbook which keeps track of how often a list of documents are reviewed. If a document's "Last reviewed date" falls beyond 180 days, conditional formatting turns the cell red to warn the owner.

    What I now want to be able to do is to automatically email the owner of those documents to tell them to take action. I've written the below but this will email the entire workbook to the user rather than just the contents (or a range thereof) of a worksheet.

    Can this be achieved? If so how?

    Many Thanks in advance

    Cliff


    VB Code:
    1. Private Sub MailDocument()
    2. 'This routene mails the customer
    3. Dim stDocName, User As String
    4. Dim MyMessage As Object
    5. User = "cliff@clifforddashwrightdotcodotuk"
    6. ColUpdateoverdue = 9
    7. For a = 2 To Sheets.Count
    8.         Sheets(a).Activate
    9.         For x = 3 To ActiveSheet.UsedRange.Rows.Count
    10.             If Sheets(a).Cells(x, ColUpdateoverdue) = "Yes" Then
    11.             With ThisWorkbook
    12.                '.HasRoutingSlip = False
    13.                 With .RoutingSlip
    14.                     .Delivery = xlAllAtOnce
    15.                     .Recipients = User
    16.                     .Subject = "These OSM Documents are due for review"
    17.                     .Message = Sheets(a).Cells(x, 8) & " Document " & Sheets(a).Cells(x, 1) & " is due for review"
    18.                     .ReturnWhenDone = False
    19.                    
    20.                 End With
    21.             .Route
    22.             End With
    23.          
    24.            'just for testing only    
    25.             MsgBox Sheets(a).Cells(x, 8) & " Document " & Sheets(a).Cells(x, 1) & " is due for review"
    26.             Else
    27.             End If
    28.         Next x
    29. Next a
    30.  
    31.  
    32.    
    33. End Sub
    Last edited by cliffw; Jun 19th, 2006 at 11:47 AM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Help sending a worksheet content in email body

    Yes, you can copy the data that you want to email into a new workbook and then email that using the .SendMail method instead.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    32

    Re: Help sending a worksheet content in email body

    True - but I really wanted the content of the email to be say for example range A1:J10, appearing directly as if I'd opened an email and pasted this into the body iteself so that there are no attachments to the email ...

    That possible?

    Cheers

    Cliff

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Help sending a worksheet content in email body

    Yes, do you have Outlook? If not we can still do it.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    32

    Re: Help sending a worksheet content in email body

    Yes I'm using outlook

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Help sending a worksheet content in email body

    Then you can automate Outlook from within Excel by adding a reference to Outlook and writting code to create a new email.

    Or if you want to add basic text to the email body you can use the ShellExecute API and pass teh &body= argument with the text to add.

    So you didnt like the create new workbook with the data and send that object instead?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    32

    Re: Help sending a worksheet content in email body

    Quote Originally Posted by RobDog888
    So you didnt like the create new workbook with the data and send that object instead?
    No not really ... it would serve a purpose but ...

    The spreadsheet is a central tracking spreadsheet for a department .. the purpose of the email is to warn individual owners of documents that they should review their document which is 'logged' on this spreadsheet.

    If I were to send this entire spreadsheet as an attachment, users might be tempted to update the attached version rather than the centrally stored version and hence cause me issues ...

    By purely having the body of the email showing a range of cells, this would allow me to get the message across that document X needs reviewing, whilst avoiding the temptation for users to update anything other than the centrally stored tracking spreadsheet .. make sense?

    Quote Originally Posted by RobDog888
    Then you can automate Outlook from within Excel by adding a reference to Outlook and writting code to create a new email.

    Can you give me a hint at the code?

    Thanks

    Cliff

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Help sending a worksheet content in email body

    Sure, but I have a massive amount of Outlook code on the forums but...
    VB Code:
    1. Option Explicit
    2. 'Add a reference to ms outlook xx.0 object library
    3. Private Sub Command1_click()
    4.  
    5.     Dim oApp As outlook.application
    6.     dim oEmail as outlook.mailitem
    7.  
    8.     set oapp = new outlook.application
    9.     set oemail =- oapp.createitem(olOutlookEMailitem)
    10.     oemail.body = "Your excel range or cell(s) data"
    11.     oemail.to = "[email protected]"
    12.     oemail.send
    13.     set oemail = nothing
    14.     set oapp = nothing
    15. end sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    32

    Re: Help sending a worksheet content in email body

    Thanks for this .. my knowledge of non-excell VBA is pretty shoddy ...

    Using this code I get a "Compile Error; User-defined type not defined" ...

    What's that about then?

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Help sending a worksheet content in email body

    'Add a reference to ms outlook xx.0 object library
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    32

    Re: Help sending a worksheet content in email body

    Sheesh ... thanks for the speedy response ... have added the reference that I managed to miss ... doh!

    But ... now I get an error saying that "olOutlookEMailitem" isn't defined ... Am I being blonde still ?

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Help sending a worksheet content in email body

    Oops, thats what I get for freehand typing it into the reply box. It should be olMailItem.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    32

    Re: Help sending a worksheet content in email body

    Hmmm ... I did try this but thought it wasn't right because I get a 'Type mismatch' error (olMailItem seems to have a default value of '0' if this helps)

    Any ideas ?

    Much appreciate your help here

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Help sending a worksheet content in email body

    Another type-o. this line should be like this.
    VB Code:
    1. set oemail = oapp.createitem(olMailitem)
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  15. #15

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    32

    Re: Help sending a worksheet content in email body [RESOLVED]

    Brilliant!

    I had wondered what the - was for ... should've known to try it ...

    Works like a charm now ... Thanks again for your help

    Cliff

Posting Permissions

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



Click Here to Expand Forum to Full Width