+ Reply to Thread
Results 1 to 2 of 2

Need vba help to run on active sheet, offset function, insert spacing, signature & email

Hybrid View

ec4excel Need vba help to run on... 08-25-2016, 10:22 PM
dflak Re: Need vba help to run on... 08-26-2016, 12:38 PM
  1. #1
    Registered User
    Join Date
    08-09-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2007/2010/365
    Posts
    82

    Need vba help to run on active sheet, offset function, insert spacing, signature & email

    Dear vba experts,

    I managed to make a vba worked in a single sheet environment to send email automatically but it cannot work on an active worksheet when there are multiple worksheets. (prompt OutLookMailItem.Send error in this line)

    I need to use offset command as the number of rows is not fixed for every month but don't know how to change this. The data will be filtered by date but the test vba sends email to all records, even if it's not the wanted data (meaning excluded from the wanted information using the filtering of the dates)

    On the other hand, I want to arrange the body text with line spacing and it can only work on the first line with this code vbCrLf.

    How to send emails with Outlook that has more than 1 email and set a default email with Signature (which already programmed in Outlook but could not work in the vba).

    I need the data to looks like this in the email body:


    Dear ABC Company, Andy,
    The following is due for renewal:
    YN2038J , expiring on 31 Aug 2016.
    Please renew before due date.


    Hoped that someone can helps.


    Sub SendEmail(what_address As String, subject_line As String, mail_body As String)
    
    Dim OutLookApp As Object
    Set OutLookApp = CreateObject("OutLook.application")
    
        Dim OutLookMailItem As Object
        Set OutLookMailItem = OutLookApp.CreateItem(0)
        
        OutLookMailItem.To = what_address
        OutLookMailItem.Subject = subject_line
        OutLookMailItem.Body = mail_body
        OutLookMailItem.Send
        
    End Sub
    
    Sub SendMassEmail()
    
    row_number = 1
    
    Do
    DoEvents
        row_number = row_number + 1
        Dim mail_body_message As String
        Dim customer_name As String
        Dim valid_to As String
        Dim license_to As String
        
        mail_body_message = ActiveSheet.Range("U2")
        customer_name = ActiveSheet.Range("E" & row_number) & ", " & ActiveSheet.Range("F" & row_number) & ", " & vbCrLf & vbCrLf
        license_to = ActiveSheet.Range("D" & row_number)
        valid_to = ActiveSheet.Range("C" & row_number)
        mail_body_message = Replace(mail_body_message, "replace_name_here", customer_name)
        mail_body_message = Replace(mail_body_message, "license_to_replace", license_to)
        mail_body_message = Replace(mail_body_message, "valid_to_replace", valid_to)
            MsgBox mail_body_message
            
         Call SendEmail(ActiveSheet.Range("J" & row_number), "This is a Renewal Reminder for " & license_to, mail_body_message)
    Loop Until row_number = 20
        
    End Sub
    Attached Files Attached Files
    Last edited by ec4excel; 08-25-2016 at 11:56 PM.

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,958

    Re: Need vba help to run on active sheet, offset function, insert spacing, signature & ema

    The code looks like it will work just fine and will run on whatever the active worksheet is.

    Do you wish to specify a sheet and have it run against that sheet no matter where you are in the workbook?

    if so then do this with the code
    dim sh as worksheet
    ....
    set sh = Sheets(SheetName) ' You will have to figure out how to get SheetName. Use a cell in the workbook or inputbox or calculate it based on date.
    ...
    ' Wherever you see activeworksheet, replace it with sh.
    You might also want to throw in some code to find the real last row number instead of hard coding it to 20.
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

+ 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. How to insert signature in email using .HTMLbody
    By sugaprasad in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-01-2015, 06:28 AM
  2. Insert body of email above signature in Lotus Notes
    By ladislav Jakl in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-16-2015, 04:52 PM
  3. Insert Lotus Notes Signature In Body Of Email Excel VBA
    By Pierce Quality in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-06-2013, 02:57 PM
  4. Insert scanned signature into protected sheet
    By usaexcel789 in forum Excel General
    Replies: 1
    Last Post: 05-01-2013, 11:44 PM
  5. send selected range in email with default outlook email signature included
    By mdsickler in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-04-2013, 10:50 PM
  6. Replies: 4
    Last Post: 02-27-2012, 03:13 AM
  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

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