How do I get a list of all the headings in a word document by using VBA?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You mean like this createOutline function (which actually copy all headings from a source word document into a new word document):
(I believe the
astrHeadings = docSource.GetCrossReferenceItems(wdRefTypeHeading)function is the key in this program, and should allow you to retrieve what you are asking for)UPDATE by @kol on March 6, 2018
Although
astrHeadingsis an array (IsArrayreturnsTrue, andTypeNamereturnsString()) I get atype mismatcherror when I try to access its elements in VBScript (v5.8.16384 on Windows 10 Pro 1709 16299.248). This must be a VBScript-specific problem, because I can access the elements if I run the same code in Word’s VBA editor. I ended up iterating the lines of the TOC, because it works even from VBScript: