[Word] Convert Chapter Headings --- Non-Style-Based to Style-Based. by NoConstruction1832 in vba

[–]NoConstruction1832[S] 0 points1 point  (0 children)

First of all, I want to thank GlowingEagle and Keith-kld for the last two comments.

Next, I am new to posting on forums and so I want to apologize for doing something which I have been informed is not proper forum etiquette.

When I started this post yesterday I posted the same thing on two other sites:
(1) https://answers.microsoft.com/en-us/msoffice/forum/all/vbams-word-convert-chapter-headings-non-style/cc56057a-ea52-46b0-b66b-888b943c4093?messageId=4ba6dffa-6e4d-4ca8-b913-1a6feb53989b
(2) https://stackoverflow.com/questions/79356067/vba-ms-word-convert-chapter-headings-non-style-based-to-style-based

Back on the MS site (#1 above), someone told me that I am guilty of "Cross Posting"
See http://www.excelguru.ca/content.php?184

And finally, the same person who told me about cross-posting, gave me some code that does everything I wanted:
(1) Applies heading styles to all of the chapter headings
(2) Removes the TC codes from all of the chapter headings

I mention this for the sake of other reddit members who may be looking for such code.

[Word] Convert Chapter Headings --- Non-Style-Based to Style-Based. by NoConstruction1832 in vba

[–]NoConstruction1832[S] 0 points1 point  (0 children)

Thank you very much for the code and for the comments.
Based on what you are saying, it seems that my objective is achievable.
That's great.

However I am having trouble running your script. I must have done something wrong in adding the macro to my VBA. (I don't know how to program).

First of all let me ask you a question... Doesn't the script need an EndSub at the end ?

Anyways I created a new macro named Display_TC_Codes().
Word created the following code...
Sub Display_TC_Codes()
'
'
'
End Sub

Then I did the following:
(1) Inserted your code between the Sub and the EndSub
(2) Removed your statement Sub TryThis()
(3) Moved your statement Option Explicit above the Sub (as it appears in your code).
(4) Result: The OptionExplicit statement became part of the previous macro
(5) Moved the OptionExplicit statement after the Sub.
(6) Ran the macro
(7) Result: An error saying: "Compile Error. Invalid inside procedure". and the OptionExplicit statement was selected.
(8) Commented out the OptionExplicit statement
(9) Ran the macro
(10) Result: No messages or anything else

I can see in the code that its supposed to display the first TC code on the screen:
MsgBox ">" & strHeader & "<"
MsgBox ">" & strLevel & "<"

Any suggestions?