none
Outlook 2007 Macro Date or Date/Time RRS feed

  • Question

  • All I wanted was a macro to put in the current Date or Date Time like the F5 key has done for years in Notepad. I made a macro in Word and tried it in outlook, worked in word failed in outlook. Tried a example with copy/paste and got the same error. needed an object. There is no record in outlook but all I want is when I execute the macro it will fill in the date or both date and time. I have emails at ??? would not allow me to put them in???

    Tuesday, October 21, 2014 8:16 PM

Answers

  • While writing emails, do you want to add date stamp at cursor location?

    If yes then, below code will work - you have to run macro from developer tab->Macros-> and select Insert_Date

    Public Sub Insert_Date()
    SendKeys Date
    DoEvents
    End Sub

    • Proposed as answer by Amol Gargote Monday, October 27, 2014 6:07 AM
    • Marked as answer by RJBP Wednesday, October 29, 2014 4:22 PM
    Wednesday, October 22, 2014 11:53 AM
  • Thanks again for the help, I tried on my own, using the following and it did work. perhaps not the best way to do it but I got what I wanted.

    Public Sub Insert_Date()
    ' 102614 - Added line for time and it worked, but am trying to find out how to add a space and dash. updated question
    SendKeys Date
    SendKeys " "
    SendKeys Time
    SendKeys " - "
    DoEvents
    End Sub

    • Marked as answer by RJBP Wednesday, October 29, 2014 4:22 PM
    Monday, October 27, 2014 3:00 AM

All replies

  • While writing emails, do you want to add date stamp at cursor location?

    If yes then, below code will work - you have to run macro from developer tab->Macros-> and select Insert_Date

    Public Sub Insert_Date()
    SendKeys Date
    DoEvents
    End Sub

    • Proposed as answer by Amol Gargote Monday, October 27, 2014 6:07 AM
    • Marked as answer by RJBP Wednesday, October 29, 2014 4:22 PM
    Wednesday, October 22, 2014 11:53 AM
  • Oh thank you so much, would I just add a line Sendkeys Time if I wanted that? and if not asking too much in Word where I could record a macro. I also added date, time (space) - (space) I will try and copy paste and see it I am allowed to put it in?

    Sub DateTime()
    '
    ' DateTime Macro
    '
    '
        Selection.InsertDateTime DateTimeFormat:="M/d/yyyy h:mm am/pm", _
            InsertAsField:=False, DateLanguage:=wdEnglishUS, CalendarType:= _
            wdCalendarWestern, InsertAsFullWidth:=False
        Selection.TypeText Text:=" - "
    End Sub

    alright it worked, I tried to send you a thank you Amol Gargote  but the email came back. I did want so much to thank you.


    • Edited by RJBP Monday, October 27, 2014 2:48 AM
    Monday, October 27, 2014 2:48 AM
  • Thanks again for the help, I tried on my own, using the following and it did work. perhaps not the best way to do it but I got what I wanted.

    Public Sub Insert_Date()
    ' 102614 - Added line for time and it worked, but am trying to find out how to add a space and dash. updated question
    SendKeys Date
    SendKeys " "
    SendKeys Time
    SendKeys " - "
    DoEvents
    End Sub

    • Marked as answer by RJBP Wednesday, October 29, 2014 4:22 PM
    Monday, October 27, 2014 3:00 AM
  • SendKeys is perhaps not the best way to do this. If we are talking about the message body then you could use the following and insert the date format that you originally intended at the cursor.

    Sub InsertDate()
    Dim oRng As Object
        On Error GoTo ErrHandler
        If TypeName(ActiveWindow) = "Inspector" Then
            If ActiveInspector.IsWordMail And _
               ActiveInspector.EditorType = olEditorWord Then
                Set oRng = ActiveInspector.WordEditor.Application.Selection
                oRng.collapse 0
                oRng.InsertDateTime _
                        DateTimeFormat:="M/d/yyyy h:mm am/pm", _
                        InsertAsField:=False, _
                        DateLanguage:=1033, _
                        CalendarType:=6, _
                        InsertAsFullWidth:=False
                oRng.collapse 0
                oRng.Text = " - "
                oRng.collapse 0
                oRng.Select
            End If
        End If
        Set oRng = Nothing
        Exit Sub
    ErrHandler:
        Beep
    End Sub


    Graham Mayor - Word MVP
    www.gmayor.com

    Monday, October 27, 2014 6:30 AM