Chuck
Chuck

Reputation: 201

Can't set footnote in Word doc using Excel VBA

I have numerous WORD documents that have several Content Controls in them. I am using an Excel file to update the WORD docs. When I make an update, I need to insert a footnote describing the change. I can update the contents of the Content Control just fine, but I am having problems inserting the footnote. Here's my code:

Set cc = oRange.ContentControls(intCounter)
strOriginalDate = cc.Range.Text

If wrdDoc.ProtectionType <> wdNoProtection Then
    wrdDoc.Unprotect strSheetPassword
End If

If wrdDoc.FormsDesign = False Then
    wrdDoc.ToggleFormsDesign
End If
cc.Range.Text = strCOD
'
' Insert the footnote
'
oRange = wrdDoc.Range(cc.Range.End, cc.Range.End)
oRange.Select
Selection.MoveRight Units:=wdCharacter, Count:=1
Selection.TypeText Text:=" "
With Selection
    With .FootnoteOptions
        .Location = wdBottomOfPage
        .NumberingRule = wdRestartContinuous
        .StartingNumber = 1
        .NumberStyle = wdNoteNumberStyleArabic
        .LayoutColumns = 0
    End With
    .Footnotes.Add Range:=cc.Range, Text:="Case Opening Date changed from " & _
    strOriginalDate & " to " & strCOD & " on " & Date, Reference:=""
    End If
End With

wrdDoc.ToggleFormsDesign
wrdDoc.Protect Type:=wdAllowOnlyFormFields, Password:=strSheetPassword
wrdDoc.Save

When I get down to the line Selection.MoveRight Units:=wdCharacter, Count:=1, I get an error that says Object doesn't support this property or method. In essence, I'm trying to move to the end of the control, then on the next step, I'm trying to move beyond/outside the control.

When I comment out that line and the line that follows it, I end up trying to insert the footnote into the content control. That fails on the With .FootnoteOptions line, possibly because the content control I'm using is a date picker.

Upvotes: 0

Views: 709

Answers (1)

Comintern
Comintern

Reputation: 22195

You are correct that you can't add a footnote inside of a Content Control. The solution is exactly what you are trying to do - put it in the document after. The problem is that you are trying to add it using the Selection object.

Since you already have a Range within the context of the Document (oRange), just work with it directly:

'
' Insert the footnote
'

'Move the oRange to an "insertion point" after the control.
oRange.Start = cc.Range.End + 1
'Collapse it.
oRange.End = oRange.Start
'Add your space.
oRange.Text = " "

With oRange.FootnoteOptions
    .Location = wdBottomOfPage
    .NumberingRule = wdRestartContinuous
    .StartingNumber = 1
    .NumberStyle = wdNoteNumberStyleArabic
    .LayoutColumns = 0
End With

oRange.Footnotes.Add Range:=oRange, Text:="Case Opening Date changed from " & _
                     strOriginalDate & " to " & strCOD & " on " & Date

There's really no reason to be mucking around with the Selection - it's just a glorified Range with the added benefit of doing all the annoying things that Word does "for your benefit" (like grabbing the trailing space) while you're highlighting with the mouse.

I'll also note that you can omit the Reference:="" - it gets set to an empty string by default. You also have a floating End If inside your With block.

Upvotes: 1

Related Questions