Troy
Troy

Reputation: 1

Clearing a specific tabstop in a range of paragraphs

In my TOC fields, a tab stop is being automatically added by another program when the Word document is generated. The tab stop is in the wrong place.

I can use the following to find and select each TOC field:

Dim doc As Document
Dim fld As Field
Dim rng As Range

Set doc = ActiveDocument

For Each fld In doc.Fields
    If fld.Type = wdFieldTOC Then
        fld.Select
    End If
Next

However, if I try to remove that tabstop from the selected TOC, it tells "This TabStops collection has mixed tab settings."

I need a way to iterate through each paragraph in the selected TOC, remove tabstop B and add TabStop B. I thought that was going to be the easy part, but I must be doing something wrong. Please help!! Thanks!!

Upvotes: 0

Views: 103

Answers (1)

FunThomas
FunThomas

Reputation: 29584

You already find the field that defined the TOC. The content of this field is a Range and can be read using the Result property.

The range of the TOC contains a paragraph for every line (entry). Every of those paragraphs has tab definitions.

I assume that a TOC paragraph should contain only one tab stop, the one that positions the page numbers at the right (for Left to Right reading). It is right aligned and has a "." (dot) as leading character.

The following code will remove (clear) all tabs from the TOC paragraphs and add only the one right-aligned for the page numbers. As you need to specify the tab position (in points) and this can vary depending on your page layout, I use the position of the last tab of the first paragraph. Maybe you have to adapt the code to your needs.

Sub CorrectTOC()
    Dim fld As Field
    For Each fld In ActiveDocument.Fields
        If fld.Type = wdFieldTOC Then
            SetTOCTabs fld.Result
        End If
    Next
End Sub

Sub SetTOCTabs(r As Range)
    Dim tabPos As Double
    Dim p As Paragraph
    For Each p In r.Paragraphs
        If tabPos = 0 Then 
            ' Save tab position if not already done.
             tabPos = p.TabStops(p.TabStops.Count).Position
        End If
        ' Remove all Tabs and add only one (right aligned)
        p.TabStops.ClearAll   
        p.TabStops.Add tabPos, wdAlignTabRight, Leader:=1
    Next
End Sub

Update
If it is only about changing the position of the tab stop at the right, it's easy, just change the Position-property.

Sub SetTOCTabs(r As Range)
    Const TabPosRight As Double = 468

    Dim p As Paragraph
    For Each p In r.Paragraphs
        p.TabStops(p.TabStops.Count).Position = TabPosRight
    Next
End Sub

If however, only some of your TOC entries have a left Tab stop that you want to keep or only some of the TOC entries have that invalid right Tab stop, maybe this can be your solution:

Sub SetTOCTabs(r As Range)
    Const TabPosRight As Double = 468

    Dim p As Paragraph
    For Each p In r.Paragraphs
        Dim tabPosLeft As Double
        If p.TabStops.Count > 1 Then
            tabPosLeft = p.TabStops(1).Position
        Else
            tabPosLeft = -1
        End If
        p.TabStops.ClearAll
        ' Add left tab stop
        If tabPosLeft > 0 Then
            p.TabStops.Add tabPosLeft, wdAlignTabLeft
        End If
        ' Add right tab stop
        p.TabStops.Add tabPos, wdAlignTabRight, Leader:=1
    Next
End Sub

Upvotes: 0

Related Questions