Search code examples
vbams-wordnon-breaking-characters

Can Microsoft Word VBA automatically insert a non-breaking space into a caption, e.g. Table or Figure caption?


I inherited Word VBA code that inserts an auto-numbered caption for a table or figure, e.g. Table 1. I want there to be a non-breaking space between the label, e.g. Table, and the number, but I cannot figure out how to do that.

I tried adding “Chr(160)” after DOCVARIABLE TablePrefix] but it inserted a non-breaking space in the middle of my Field Codes instead of inserting it immediately before the table number.

When I toggle field codes on the output of the macro I see this:

{ DOCVARIABLE TablePrefix"o" \* MERGEFORMAT }{ SEQ Table \* MERGEFORMAT }

(Please note, I used "o" to show where the non-breaking space is)

Sub InsertCaptionParagraph(sCapType As String, rPara As Range)

    Dim sFieldText As String
    Dim sCapField As String
    Dim rInsertPos As Range
    Dim fField As Field
    Dim rFirstChar As Range

    'Insert the required Table Caption
    If sCapType = "Table" Then
        sCapField = "DOCVARIABLE TablePrefix" & Chr(160)
        'Create the SEQ number
        sFieldText = "SEQ Table"
        rPara.Style = "TableTitle"
    ElseIf sCapType = "Figure" Then
        sCapField = "DOCVARIABLE FigurePrefix" & Chr(160)
        'Create the SEQ number
        sFieldText = "SEQ Figure"
        rPara.Style = "FigureTitle"
    Else
        sCapField = "STYLEREF  TableTitle" & Chr(160)
        'Create the SEQ number
        sFieldText = ""
        rPara.Style = "TableTitleCont"
    End If

    Set rInsertPos = rPara.Paragraphs(1).Range
    'Need to delete all unwanted field codes from the para before inserting the new ones
    If rInsertPos.Fields.Count > 0 Then
        For Each fField In rInsertPos.Fields
            fField.Delete
        Next
        Set rInsertPos = rPara.Paragraphs(1).Range
        If Mid(rInsertPos.Text, 1, 1) = vbTab Then
            Set rFirstChar = rInsertPos
            rFirstChar.Collapse wdCollapseStart
            rFirstChar.End = rFirstChar.Start + 1
            rFirstChar.Delete
        End If
        'Replace rInsertPos.Text, vbTab, ""
    End If

    'Insert the paragraph with the correct formatting

    rInsertPos.Collapse wdCollapseStart
    If sCapType <> "TableTitleCont" Then
        rInsertPos.InsertAfter Text:=vbTab
        rInsertPos.Collapse wdCollapseStart
    Else
        If Len(rInsertPos.Paragraphs(1).Range) = 1 Then
            rInsertPos.InsertAfter Text:=" continued"
            rInsertPos.Collapse wdCollapseStart
        End If
    End If
    rPara.Fields.Add Range:=rInsertPos, Type:=wdFieldEmpty, Text:= _
        sFieldText, PreserveFormatting:=True
    rInsertPos.Collapse wdCollapseStart
    rPara.Fields.Add Range:=rInsertPos, Type:=wdFieldEmpty, Text:= _
        sCapField, PreserveFormatting:=True

End Sub

Solution

  • The non-breaking space should be inserted at the end of the routine where you add the fields.

    rPara.Fields.Add Range:=rInsertPos, Type:=wdFieldEmpty, Text:= _
        sFieldText, PreserveFormatting:=True
    rInsertPos.Collapse wdCollapseStart
    rInsertPos.Text = Chr(160)
    rInsertPos.Collapse wdCollapseStart
    rPara.Fields.Add Range:=rInsertPos, Type:=wdFieldEmpty, Text:= _
        sCapField, PreserveFormatting:=True