Reputation: 1
My question relates to VBA and MS Word (Office 2021)
I have some large legacy documents containing multi-level, manually-numbered, chapter headings. When these documents were created back in the 1990s, I was using the TC (Table of Contents Entry) field to define the text and page numbers for entries in the TOC (Table of Contents). I don't think that Microsoft had yet introduced Styles at that time.
Re the TC field --- see https://support.microsoft.com/en-us/office/field-codes-tc-table-of-contents-entry-field-01e5dd8a-4730-4bc2-8594-23d7329e25c3?ns=WINWORD&version=21
Here's an example of a TC-based chapter heading as seen in RevealCodes mode. https://i.sstatic.net/9z8MheKN.png
As you can see, the heading appears in the body of the document as well as in the TC field (the stuff enclosed within parenthesis). The TC field becomes a TOC entry.
Anyways I would like to convert these documents such that the headings become Style-based and auto-numbered. However, converting all these documents manually would be terribly time-consuming. Therefore I would like to hire someone to do this programmatically with VBA.
However before doing so I need to educate myself on the subject, in order to determine whether its indeed feasible.
I assume that there is a VBA-accessible table (somewhere in the Word doc) containing all the instances of TC codes. That being the case, the VBA program will do the following for each element of the table: (1) Examine the contents of the TC field and determine whether it is a Level1, Level2, or Level3 heading. (2) Apply the appropriate Heading Style (level 1, 2, or 3) to the heading text in the body of the doc. (3) Remove the TC field as it will no longer be needed.
QUESTIONS: (1) Does this sound feasible? (2) Do you have any code that demonstrates how to access the table of TC code instances.
Any suggestions would be greatly appreciated.
I did not try anything as I do not know how to program. I simply want to know whether VBA can solve my problem.
Upvotes: -1
Views: 43
Reputation: 13515
You could do the basic conversion with a macro like:
Sub Demo()
Application.ScreenUpdating = False
Dim f As Long, StrLvl As String
With ActiveDocument
For f = .Fields.Count To 1 Step -1
With .Fields(f)
If .Type = wdFieldTOCEntry Then
StrLvl = "1"
If InStr(.Code.Text, "\l") > 0 Then StrLvl = Split(Split(.Code.Text, "\l")(1), " ")(1)
.Code.Paragraphs.First.Style = "Heading " & StrLvl
.Delete
End If
End With
Next
End With
Application.ScreenUpdating = True
End Sub
Do note, though, that TC fields can tell Word to display a different text from what it in the actual heading. Heading Styles don't allow that. The above macro assumes the existing heading text is to be displayed in your rebuilt Table of Contents.
You will also need to take care of any multi-level list numbering etc., along with deleting any unwanted manual numbering. Although that, too could be done in code, I have no way of knowing what numbering schemes your documents use - and different code would be required for each numbering scheme variant.
Upvotes: 2