+ Reply to Thread
Results 1 to 4 of 4

Combine mail VBA, and make it insert a timestamp.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-20-2015
    Location
    Stockholm
    MS-Off Ver
    2013
    Posts
    307

    Combine mail VBA, and make it insert a timestamp.

    I have this VBA for a "send mail button"

    -Is there a way to make this leave a timestamp in "A10" that says "Shipped TIME+DATE" ?
    Sub MailAugust()
        Dim Source As Range
        Dim Dest As Workbook
        Dim wb As Workbook
        Dim TempFilePath As String
        Dim TempFileName As String
        Dim FileExtStr As String
        Dim FileFormatNum As Long
        Dim OutApp As Object
        Dim OutMail As Object
    
        Set Source = Nothing
        On Error Resume Next
        Set Source = Range("AX1:CT43").SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
    
        If Source Is Nothing Then
            MsgBox "The source is not a range or the sheet is protected, please correct and try again.", vbOKOnly
            Exit Sub
        End If
    
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
        Set wb = ActiveWorkbook
        Set Dest = Workbooks.Add(xlWBATWorksheet)
    
        Source.Copy
        With Dest.Sheets(1)
            .Cells(1).PasteSpecial Paste:=8
            .Cells(1).PasteSpecial Paste:=xlPasteValues
            .Cells(1).PasteSpecial Paste:=xlPasteFormats
            .Cells(1).Select
            Application.CutCopyMode = False
        End With
    
        TempFilePath = Environ$("temp") & "\"
        TempFileName = "pay" & " " & Format(Now, "dd-mmm-yy")
    
        If Val(Application.Version) < 12 Then
           
            FileExtStr = ".xls": FileFormatNum = -4143
        Else
           
            FileExtStr = ".xlsx": FileFormatNum = 51
        End If
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
        With Dest
            .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
            On Error Resume Next
            With OutMail
                .To = ""
                .CC = ""
                .BCC = ""
                .Subject = "Pay"
                .Body = ""
                .Attachments.Add Dest.FullName
                        .Send
            End With
            On Error GoTo 0
            .Close savechanges:=False
        End With
    
        Kill TempFilePath & TempFileName & FileExtStr
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    End Sub

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Combine mail VBA, and make it insert a timestamp.

    Add this line. Change the Time and Date format to suit.

        Set OutMail = Nothing
        Set OutApp = Nothing
        
        Range("A10").Value = "Shipped " & Format(Now, "hh:mm d/m/yyyy")
        
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Contributor
    Join Date
    05-20-2015
    Location
    Stockholm
    MS-Off Ver
    2013
    Posts
    307

    Re: Combine mail VBA, and make it insert a timestamp.

    Quote Originally Posted by AlphaFrog View Post
    Add this line. Change the Time and Date format to suit.

        Set OutMail = Nothing
        Set OutApp = Nothing
        
        Range("A10").Value = "Shipped " & Format(Now, "hh:mm d/m/yyyy")
        
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    End Sub
    Awesome, thanks a million!!!

    If it's not to big of a hassle, and sorry for not asking before - is it possible for it to change the cell-color to light-green?

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Combine mail VBA, and make it insert a timestamp.

    Range("A10").Interior.ColorIndex = 35

    TIP: you can start recording a macro and do the steps (e.g. change the cell color) to get the example code.

+ 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] VBA to sort by timestamp, insert decimal, and transpose to new worksheet
    By blueflwr19 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-12-2014, 05:30 PM
  2. Replies: 2
    Last Post: 11-07-2013, 06:19 AM
  3. [SOLVED] Insert timestamp when ticked....
    By Ben1985 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-09-2013, 06:46 AM
  4. Replies: 6
    Last Post: 01-16-2013, 07:44 AM
  5. Replies: 0
    Last Post: 07-22-2012, 01:34 PM
  6. Replies: 1
    Last Post: 07-22-2012, 09:26 AM
  7. [SOLVED] insert date timestamp to end of spreadsheet
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-08-2006, 12:25 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