Link to home
Create AccountLog in
Avatar of Andreas Hermle
Andreas HermleFlag 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
Avatar of FarWest
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

Avatar of 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
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
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
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
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Hi Rgonzo, as always, nice and professional coding from your side. Thank you very much for your professional help.

Regards, Andreas