0
votes

I am using VBA to parse a large set of Word documents, and I want to make sure that their structure follows certain rules (eg you cannot have more than one paragraph between tables, table nesting can only go down one level (ie you can have a table in a table, but no further nesting), and so on (this is not a complete list!)).

So I thought I would step through the "elements" in each document, looking for these patterns, but I have been unable to do so. All looping solutions seem to focus on one type of object, and I don't see any easy way to do that here. When I look at a document, I see (eg) a header, then three paragraphs, then a table, then a paragraph, then two tables, and so on; I would like my VBA to be able to step through the document in the same way. Is it possible?

I am very experienced in Excel VBA but just starting out in Word VBA, so still trying to understand the concepts and the object model -- please forgive my ignorance!

[EDIT] In the end I found that the best way to accomplish what I wanted was to use VBA to Save As HTML; that saves the structure as it is in the document. I can then parse the HTML, looking for the structural elements that I want. Sounds a little roundabout, I agree, but it's fairly quick and does the trick.

1

1 Answers

0
votes

Word's object model doesn't give you a way to do as you describe. Closest you could get would be to "walk" the selection one character at a time and test the type of thing it is - is it in a table? Does its range contain InlineShapes? etc. But this would be a lot more work to code than simply looping the various kinds of collection. And it would also be slower.

Better would be to make a list of the rules, then code for each one...