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!!
You already find the field that defined the TOC. The content of this field is a Range and can be read using the
Resultproperty.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.
Update
If it is only about changing the position of the tab stop at the right, it's easy, just change the
Position-property.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: