Avatar of PatternNut
PatternNut
 asked on

Skip Tables in VBA macro

I have a macro that looks through each paragraph in a document, like so:

Dim para as Paragraph
For each para in ActiveDocument.Paragraphs
a = a + 1
run code
etc

However, if there is a table in the document then it naturally looks through each cell of the table separately.   I don't want it to look at tables, so if there are lots of tables in the document, I'm running lots of unnecessary iterations! I forget my exact solution, but it was something like this...

If ActiveDocument.Paragraphs(a).Range.Tables.Count > 0 Then next paragraph
Else
Run lots of code

That checks each paragraph to see if it's in a table... if it is then it skips to next paragraph and checks there.  But is there any way to make it skip straight to the end of the table?  I do not want to use 'select' because it will cause complications later on.

Thanks,

PatternNut

Microsoft Word

Avatar of undefined
Last Comment
GrahamSkan

8/22/2022 - Mon
GrahamSkan

Not really. Why don't you just do this:

Dim para as Paragraph
For each para in ActiveDocument.Paragraphs
     If ActiveDocument.Paragraphs(a).Range.Tables.Count = 0 Then
         run code
     End If
Next para
PatternNut

ASKER
Hi Graham, that's exactly what I have done (more or less).

But what I was trying to say is that that's the solution I want to avoid.  The reason is that I might have a 120 page document with 50 tables, each of 100 cells.  That's 5000 queries the computer has made when I could skip 4950 of them if I could figure this out.

But my experience says that if you don't know the answer, I doubt anyone does!  I have an idea for a workaround... I'll leave this question open and see if there is a solution and ask a question about the workaround as soon as I can get my head through it properly

Thanks - I really appreciate your assiststance Graham,

PatternNut
ASKER CERTIFIED SOLUTION
GrahamSkan

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
PatternNut

ASKER
Extremely clever.  If I was half as smart as this, I'd have cured world poverty and become a billionaire.  Like you say, not sure what's faster in the end... but it's a great approach.  

Bloody good stuff.  Cheers mate,

PatternNut
Your help has saved me hundreds of hours of internet surfing.
fblack61
GrahamSkan

Well it was a challenge. Thanks