VBOffice

Add date at the cursor position

This macro is useful if you want to add a new note, for instance, to a contact or task.

Last modified: 2019/06/13 | Accessed: 2.338  | #170
◀ Previous sample Next sample ▶

Content

SAM SAM
Determine the "identity" of your emails. Set with SAM the sender and the folder folder for sent items with the help of rules.

Insert date at the top of the body

This sample inserts the date at the top of the body. See the DefaultMsg variable in the AddNote method, here you can set a default message that should be added together with the date.

In order to use this sample you need to add a reference to the 'Microsoft Word x.x Object Library' via Tools/References.


tip  How to add macros to Outlook
Public Sub AddNote()
  Dim DefaultMsg$
  
  DefaultMsg = ""
  
  AddNote_Ex Application.ActiveInspector, DefaultMsg
End Sub

Private Sub AddNote_Ex(Inspector As Outlook.Inspector, Optional Msg As String)
  Dim WdSel As Word.Selection
  Dim p&

  Msg = Format(Date, "mm/dd/yyyy", vbUseSystemDayOfWeek, vbUseSystem) & _
    ": " & Msg
  Msg = vbCrLf & "---" & vbCrLf & Msg
  Set WdSel = GetCurrentWordSelection(Inspector)
  p = Len(Msg) - 2
  WdSel.Start = 0
  WdSel.End = 0
  WdSel.InsertBefore Msg
  WdSel.Start = WdSel.Start + p
  WdSel.End = WdSel.Start
End Sub

Private Function GetCurrentWordSelection(OpenInspector As Outlook.Inspector) As Word.Selection
  Dim Doc As Word.Document
  Dim Wd As Word.Application
  
  Set Doc = OpenInspector.WordEditor
  Set Wd = Doc.Application
  Set GetCurrentWordSelection = Wd.Selection
End Function
OLKeeper OLKeeper
OLKeeper reliably prevents users from closing their Outlook window and thus possibly missing reminders or e-mails.

Insert date at the current cursor position

This sample is similar to the first one, but this time the date is inserted at the current position of the cursor within the body. (Inserting anything in another text field than the body doesn´t work this way.)

In order to use this sample you need to add a reference to the 'Microsoft Word x.x Object Library' via Tools/References.

Public Sub AddNote()
  Dim DefaultMsg$
  
  DefaultMsg = ""
  
  AddNote_Ex Application.ActiveInspector, DefaultMsg
End Sub

Private Sub AddNote_Ex(Inspector As Outlook.Inspector, Optional Msg As String)
  Dim WdSel As Word.Selection
  Dim p&

  Msg = Format(Date, "mm/dd/yyyy", vbUseSystemDayOfWeek, vbUseSystem) & _
    ": " & Msg
  Msg = vbCrLf & "---" & vbCrLf & Msg
  Set WdSel = GetCurrentWordSelection(Inspector)
  p = Len(Msg) - 2
  WdSel.End = WdSel.Start
  WdSel.InsertBefore Msg
  WdSel.Start = WdSel.Start + p
  WdSel.End = WdSel.Start
End Sub

Private Function GetCurrentWordSelection(OpenInspector As Outlook.Inspector) As Word.Selection
  Dim Doc As Word.Document
  Dim Wd As Word.Application
  
  Set Doc = OpenInspector.WordEditor
  Set Wd = Doc.Application
  Set GetCurrentWordSelection = Wd.Selection
End Function
Reporter Reporter
VBOffice Reporter is an easy to use tool for data analysis and reporting in Outlook. A single click, for instance, allows you to see the number of hours planned for meetings the next month.
email  Send a message