Link to home
Start Free TrialLog in
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

Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of PatternNut
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
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Well it was a challenge. Thanks