Avatar of Andreas Hermle
Andreas Hermle
Flag for Germany asked on

Insert a page break before each of occurrence of a certain term in Column 4 in the currently selected table

Dear Experts:

I got a huge table (spanning over a lot of pages and having no header row) in the Active Document. In Column 4 of this table, the term 'Product' or 'Set' appears off and on.

And now I would like to run a macro ...
... to split this huge table by inserting a page break before each occurrence of the term 'Product'. That is, in the end, the huge table will be split up into, say, 30 tables, each separated by a page break. All these tables will have either the term 'Product' or 'Set' in the 4th column of the first row.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
Microsoft WordVBA

Avatar of undefined
Last Comment
Andreas Hermle

8/22/2022 - Mon
FarWest

check this code
Public Function FindAndSplictTable()
Dim searchStr(2) As String
Dim order As Integer
searchStr(0) = "Product"
searchStr(1) = "Set"
Application.ScreenUpdating = False

For order = 0 To 1
Dim myRange As Range
Set myRange = ActiveDocument.Content

With myRange.Find
.MatchWildcards = False
.Forward = False
.Text = searchStr(order)
.MatchWholeWord = True
Do While .Execute() = True
If myRange.Information(wdWithInTable) Then
 With myRange
If .Information(wdStartOfRangeRowNumber) <> 1 And .Information(wdStartOfRangeColumnNumber) Then ' check if it is first row and column 4
 .Tables(1).Split .Information(wdStartOfRangeRowNumber)
End If
 End With
End If
myRange.Collapse wdCollapseStart
Loop
 End With
 Next
Application.ScreenUpdating = True
MsgBox ("done")

End Function

Open in new window

Andreas Hermle

ASKER
Hi FarWest,

thank you very much for your swift and professional help. Works great, the table gets split up into subtables whenever the code hits the terms 'Product' or 'Set'.

But I am afraid to tell you that there is one thing I still would like to get incorporated in this nice code, that is, the ensuing subtables are to be separated by manual page breaks.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
ASKER CERTIFIED SOLUTION
Rgonzo1971

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Andreas Hermle

ASKER
Hi Rgonzo,

I was looking for manual page breaks, but the property 'page break before' is also fine.

Thank you very much for your professional and swift support.

Regards, Andreas
Your help has saved me hundreds of hours of internet surfing.
fblack61
Andreas Hermle

ASKER
Hi Rgonzo,

just in case I may need it in the future, how would your code look like if I wanted to have manual page breaks instead of the property 'PageBreakBefore'

Thank you very much in advance. Regards, Andreas
SOLUTION
Rgonzo1971

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Andreas Hermle

ASKER
Hi Rgonzo, as always, nice and professional coding from your side. Thank you very much for your professional help.

Regards, Andreas