Class: OutlookNotes in Category Microsoft Outlook : Automation from Total Visual SourceBook

Working with Microsoft Outlook Notes through Automation using VBA and VB6.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the COutlookNotes class.
AppOutlook Property Get the handle to the current instance of Outlook.
AppNameSpace Property Get the handle to the current instance of the Outlook NameSpace.
AddNote Method Add a new note.
DeleteNote Method Delete a Note Item.
DeleteNoteID Method Delete the note identified by the passed ID number.
GetNoteList Method Get a list of all Note Items containing the passed string.
GetNoteCount Method Get the number of notes containing the passed string.
GetNoteID Method Get the ID of the first Note Items containing the passed string.
GetNoteIDBody Method Get a note's body (text) based on its ID in the outlook notes collection.
SetNoteIDBody Method Update the note body for a particular ID.
GetNoteIDCategories Method Get a note's categories based on its ID in the outlook notes collection.
SetNoteIDCategories Method Update the note categories for a particular ID.
StartOutlook Method Start an instance of Outlook.
Class_Terminate Terminate Clean up class variables opened for Outlook.
CloseOutlook Method Close an instance of Outlook.
' Example of the COutlookNotes class
'
' To try this example, do the following:
' 1. Create a new form
' 2. Add the following text boxes
'       txtNote
'       txtCategories
' 3. Add the following command buttons:
'       cmdAdd
'       cmdDelete
'       cmdNotesCount
'       cmdListNotes
'       cmdGetNoteBody
'       cmdGetNoteCategories
'       cmdGetNoteID
'       cmdDeleteNoteID
'       cmdSetNoteIDBody
'       cmdSetNoteIDCategories
' 4. Paste all the code from this example to the new form's module
' 5. Run the form.

Private Sub cmdAdd_Click()

  Dim strBody As String
  Dim strCategories As String
  Dim clsOutlookNotes As COutlookNotes

  strBody = Nz(Me.txtNote)

  If strBody <> "" Then
    If MsgBox("Do you want to add the note: " & vbCrLf & "'" & Left$(strBody, 50) & "'?", vbYesNo) = vbYes Then
      strCategories = Nz(Me.txtCategories)

      ' Initialize Notes class and connect to Outlook
      Set clsOutlookNotes = New COutlookNotes
      clsOutlookNotes.StartOutlook

      If clsOutlookNotes.AddNote(strBody, strCategories, True) Then
        MsgBox "Note added successfully"
      Else
        MsgBox "Note was not added"
      End If

      ' Clean up
      Set clsOutlookNotes = Nothing

    End If
  Else
    MsgBox "Specify text for the note"
    Me.txtNote.SetFocus
  End If
End Sub

Private Sub cmdDelete_Click()
  Dim eResult As VbMsgBoxResult
  Dim strBody As String
  Dim clsOutlookNotes As COutlookNotes

  strBody = Nz(Me.txtNote, "")

  If MsgBox("Are you sure you want to delete note(s): " & vbCrLf & "'" & strBody & "'?", vbYesNo) = vbYes Then

    eResult = MsgBox("Delete partial matches?" & vbCrLf & vbCrLf & _
              "   Click 'Yes' to delete all Notes containing the text anywhere in the note." & vbCrLf & _
              "   Click 'No' to delete Notes that match exactly." & vbCrLf & _
              "   Click 'Cancel' to cancel without deleting Notes.", vbYesNoCancel)

    ' Initialize Notes class and connect to Outlook
    Set clsOutlookNotes = New COutlookNotes
    clsOutlookNotes.StartOutlook

    Select Case eResult
      Case vbYes
        ' Delete Notes with Partial Matching
        clsOutlookNotes.DeleteNote strBody, True
      Case vbNo
        ' Delete Notes with Exact Match
        clsOutlookNotes.DeleteNote strBody, False
      Case vbCancel
        ' Don't delete Notes.
    End Select

    ' Clean up
    Set clsOutlookNotes = Nothing
  End If

End Sub

Private Sub cmdDeleteNoteID_Click()

  Dim lngNoteID As Long
  Dim clsOutlookNotes As COutlookNotes

  lngNoteID = EnterNoteID("delete")

  If lngNoteID >= 0 Then
    ' Initialize Notes class and connect to Outlook
    Set clsOutlookNotes = New COutlookNotes
    clsOutlookNotes.StartOutlook

    If clsOutlookNotes.DeleteNoteID(lngNoteID) Then
      MsgBox "Note successfully deleted"
    Else
      MsgBox "Note could not be deleted"
    End If

    ' Clean up
    Set clsOutlookNotes = Nothing
  End If

End Sub

Private Sub cmdGetNoteBody_Click()

  Dim lngNoteID As Long
  Dim strNote As String
  Dim clsOutlookNotes As COutlookNotes

  lngNoteID = EnterNoteID("retrieve its body")

  If lngNoteID >= 0 Then
    ' Initialize Notes class and connect to Outlook
    Set clsOutlookNotes = New COutlookNotes
    clsOutlookNotes.StartOutlook

    strNote = clsOutlookNotes.GetNoteIDBody(lngNoteID)

    MsgBox strNote

    ' Clean up
    Set clsOutlookNotes = Nothing
  End If

End Sub

Private Sub cmdGetNoteCategories_Click()

  Dim lngNoteID As Long
  Dim strCategories As String
  Dim clsOutlookNotes As COutlookNotes

  lngNoteID = EnterNoteID("retrieve its categories")

  If lngNoteID >= 0 Then
    ' Initialize Notes class and connect to Outlook
    Set clsOutlookNotes = New COutlookNotes
    clsOutlookNotes.StartOutlook

    strCategories = clsOutlookNotes.GetNoteIDCategories(lngNoteID)

    MsgBox strCategories

    ' Clean up
    Set clsOutlookNotes = Nothing
  End If

End Sub

Private Sub cmdGetNoteID_Click()

  Dim strNote As String
  Dim lngNoteID As Long
  Dim clsOutlookNotes As COutlookNotes

  strNote = InputBox("Get the ID of a note containing this text (blank retrieves the first Note):")

  ' Initialize Notes class and connect to Outlook
  Set clsOutlookNotes = New COutlookNotes
  clsOutlookNotes.StartOutlook

  lngNoteID = clsOutlookNotes.GetNoteID(strNote, True)
  If lngNoteID > 0 Then
    MsgBox "The text was found in Note: " & lngNoteID
  Else
    MsgBox "Note was not found"
  End If

  ' Clean up
  Set clsOutlookNotes = Nothing
End Sub

Private Sub cmdListNotes_Click()
  Dim strFilter As String
  Dim clsOutlookNotes As COutlookNotes
  Dim col As Collection
  Dim intCounter As Integer

  strFilter = InputBox("List Notes containing this text (leave blank to return all Notes):")

  ' Initialize Notes class and connect to Outlook
  Set clsOutlookNotes = New COutlookNotes
  clsOutlookNotes.StartOutlook

  Set col = clsOutlookNotes.GetNoteList(strFilter)

  ' Clean up
  Set clsOutlookNotes = Nothing

  If col.Count = 0 Then
    MsgBox "No Notes found."
  Else
    For intCounter = 1 To col.Count
      Debug.Print col(intCounter)
      Debug.Print "----------------------------------"
    Next intCounter
    MsgBox "Notes listed in the Immediate Window."
  End If

End Sub

Private Sub cmdNotesCount_Click()

  Dim strFilter As String
  Dim clsOutlookNotes As COutlookNotes
  Dim lngNotes As Long

  strFilter = InputBox("Number of Notes containing this text (leave blank to return all Notes):")

  ' Initialize Notes class and connect to Outlook
  Set clsOutlookNotes = New COutlookNotes
  clsOutlookNotes.StartOutlook

  lngNotes = clsOutlookNotes.GetNoteCount(strFilter)

  ' Clean up
  Set clsOutlookNotes = Nothing

  MsgBox lngNotes & " Notes found"

End Sub

Private Sub cmdSetNoteIDBody_Click()

  Dim strBody As String
  Dim lngNoteID As Long
  Dim clsOutlookNotes As COutlookNotes

  strBody = Nz(Me.txtNote)

  If strBody <> "" Then
    lngNoteID = EnterNoteID("assign the body text")
    If lngNoteID >= 0 Then
      ' Initialize Notes class and connect to Outlook
      Set clsOutlookNotes = New COutlookNotes
      clsOutlookNotes.StartOutlook

      If clsOutlookNotes.SetNoteIDBody(lngNoteID, strBody) Then
        MsgBox "Note successfully updated"
      Else
        MsgBox "Note could not be updated"
      End If

      ' Clean up
      Set clsOutlookNotes = Nothing
    End If
  Else
    MsgBox "Specify text for the note"
    Me.txtNote.SetFocus
  End If

End Sub

Private Sub cmdSetNoteIDCategories_Click()

  Dim strCategories As String
  Dim lngNoteID As Long
  Dim clsOutlookNotes As COutlookNotes

  strCategories = Nz(Me.txtCategories)

  If strCategories <> "" Then
    lngNoteID = EnterNoteID("assign categories")
    If lngNoteID >= 0 Then
      ' Initialize Notes class and connect to Outlook
      Set clsOutlookNotes = New COutlookNotes
      clsOutlookNotes.StartOutlook

      If clsOutlookNotes.SetNoteIDCategories(lngNoteID, strCategories) Then
        MsgBox "Note categories successfully updated"
      Else
        MsgBox "Note categories could not be updated"
      End If

      ' Clean up
      Set clsOutlookNotes = Nothing
    End If
  Else
    MsgBox "Specify the categories for the note"
    Me.txtCategories.SetFocus
  End If

End Sub

Private Function EnterNoteID(strPrompt As String) As Long
  ' Comments: Prompt user for a note ID
  ' Params  : strPrompt       Text to display when asking for the ID number
  ' Returns : Note ID that was entered, 0 for none

  Dim lngNoteID As Long
  Dim clsOutlookNotes As COutlookNotes
  Dim lngNotes As Long
  Dim strReturn As String

  lngNoteID = 0

  ' Initialize Notes class and connect to Outlook
  Set clsOutlookNotes = New COutlookNotes
  clsOutlookNotes.StartOutlook

  lngNotes = clsOutlookNotes.GetNoteCount()

  ' Clean up
  Set clsOutlookNotes = Nothing

  If lngNotes > 0 Then
    strReturn = InputBox("Enter the Note ID to " & strPrompt & " (between 1 and " & lngNotes & "):")
    If strReturn <> "" Then
      lngNoteID = CLng(strReturn)
      If (lngNoteID <= 0) Or (lngNoteID > lngNotes) Then
        lngNoteID = 0
        MsgBox "Enter a valid ID"
      End If
    End If
  Else
    MsgBox "No notes exist"
  End If

  EnterNoteID = lngNoteID

End Function

Private Sub Form_Load()
  Const clngLeft As Long = 2250
  Const clngWidth As Long = 2670

  ' Setup controls
  With Me.txtCategories
    .Top = 100
    .Width = 2000
    .Height = 325
    .Left = 100
    .Value = "Red Category,Yellow Category"
  End With

  With Me.txtNote
    .Top = 500
    .Width = 2000
    .Height = 3000
    .Left = 100
    .Value = "FMS Test Note"
  End With

  With Me.cmdAdd
    .Caption = "Add note"
    .Top = 100
    .Left = clngLeft
    .Width = clngWidth
  End With

  With Me.cmdDelete
    .Caption = "Delete Notes"
    .Top = 500
    .Left = clngLeft
    .Width = clngWidth
  End With

  With Me.cmdNotesCount
    .Caption = "Number of Notes"
    .Top = 900
    .Left = clngLeft
    .Width = clngWidth
  End With

  With Me.cmdListNotes
    .Caption = "List Notes"
    .Top = 1300
    .Left = clngLeft
    .Width = clngWidth
  End With

  With Me.cmdGetNoteBody
    .Caption = "Get Note Body by ID"
    .Top = 1700
    .Left = clngLeft
    .Width = clngWidth
  End With

  With Me.cmdGetNoteCategories
    .Caption = "Get Note Categories by ID"
    .Top = 2100
    .Left = clngLeft
    .Width = clngWidth
  End With

  With Me.cmdGetNoteID
    .Caption = "Get NoteID"
    .Top = 2500
    .Left = clngLeft
    .Width = clngWidth
  End With

  With Me.cmdDeleteNoteID
    .Caption = "Delete Note by ID"
    .Top = 2900
    .Left = clngLeft
    .Width = clngWidth
  End With

  With Me.cmdSetNoteIDBody
    .Caption = "Set Note by ID"
    .Top = 3300
    .Left = clngLeft
    .Width = clngWidth
  End With

  With Me.cmdSetNoteIDCategories
    .Caption = "Set Categories by ID"
    .Top = 3700
    .Left = clngLeft
    .Width = clngWidth
  End With
End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!


View all FMS products for Microsoft Access All Our Microsoft Access Products

Reviews

Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info

Question

 

 

Free Product Catalog from FMS