0
votes

I would like add a working checkbox (content control?) to an existing task, like a list of sub-tasks, that could just be checked off.

Within Outlook I referenced the Microsoft Word 16.0 Object Library, and I have tried the suggestions at http://www.vboffice.net/en/developers/use-word-macro-in-outlook/ and https://www.slipstick.com/developer/word-macro-apply-formatting-outlook-email/ without success.

I tried

Option Explicit

Public Sub Checkbox()
    Dim objItem As Object
    Dim objInsp As Outlook.Inspector
    
    ' Add reference to Word library
    ' in VBA Editor, Tools, References
    Dim objWord As Word.Application
    Dim objDoc As Word.Document
    Dim objSel As Word.Selection
    On Error Resume Next
   
    ' Reference the current Outlook item
    Set objItem = Application.ActiveInspector.CurrentItem
    If Not objItem Is Nothing Then
        If objItem.Class = olMail Then
            Set objInsp = objItem.GetInspector
            If objInsp.EditorType = olEditorWord Then
                Set objDoc = objInsp.WordEditor
                Set objWord = objDoc.Application
                Set objSel = objWord.Selection


            ' Formatting code goes here
            Selection.Range.ContentControls.Add (wdContentControlCheckBox)

            End If
        End If
    End If
    
    Set objItem = Nothing
    Set objWord = Nothing
    Set objSel = Nothing
    Set objInsp = Nothing
End Sub

I have also tried

Public Sub Check2()
    Dim Ins As Outlook.Inspector
    Dim Document As Word.Document
    Dim Word As Word.Application
    Dim Selection As Word.Selection

    Set Ins = Application.ActiveInspector
    Set Document = Ins.WordEditor
    Set Word = Document.Application
    Set Selection = Word.Selection

    Selection.Range.ContentControls.Add (wdContentControlCheckBox)

End Sub

The first one didn't do anything, as I recall.

The second one showed

Run-time error '445'".
Object doesn't support this action

1

1 Answers

0
votes

Your VBA code works correctly:

Public Sub Check2()
  Dim Ins As Outlook.Inspector
  Dim Document As Word.Document
  Dim Word As Word.Application
  Dim Selection As Word.Selection

  Set Ins = Application.ActiveInspector
  Set Document = Ins.WordEditor
  Set Word = Document.Application
  Set Selection = Word.Selection

  Selection.Range.ContentControls.Add (wdContentControlCheckBox)

End Sub

You just needed to add a reference to the Word object library (Tools -> References):

enter image description here