Log in

View Full Version : [SOLVED:] Solved: Word Macro Split Table?



fredlo2010
12-25-2011, 02:17 PM
Hello, I am trying to write this macro for work that will take a report from my Company's operating program and give a little more Commercial appealing look. This is something I have to do several times a day and it is kind of a tedious task.

I have modified several parts of the document and I am very happy with the results. I am trying to get a piece of code to search for a text string and then split the table right above it. I managed to put together a code but I need to add the exception of if row selected is the first row then search next. I get the code looping for ever in the first text string it finds.

This is the code:


Sub Macro1()
'
' Macro1 Macro
'
'
Dim sText As String
Dim bmRange As Range

sText = "Total amount"
Selection.Find.ClearFormatting
With Selection.Find
.Text = sText
.Wrap = wdFindContinue
Do While Selection.Find.Execute
If Selection.Information(wdWithInTable) Then
Selection.Rows.Select
Selection.SplitTable
End If
Loop
End With

End Sub



Thanks in advance

macropod
12-25-2011, 03:39 PM
Cross-posted at: http://www.techsupportforum.com/forums/f57/word-macro-split-table-620318.html

For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

macropod
12-25-2011, 10:29 PM
Hi fredlo,

Assuming you want the table split after the 'found' row, try:

Sub Demo()
Application.ScreenUpdating = False
Dim Tbl As Table, RngFnd As Range, StrFindTxt As String
StrFindTxt = InputBox("What is the string to Find?")
If Trim(StrFindTxt) = "" Then Exit Sub
For Each Tbl In ActiveDocument.Tables
Set RngFnd = Tbl.Range
With RngFnd.Find
.ClearFormatting
.Text = StrFindTxt
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Do While .Execute
With RngFnd.Duplicate
If .Cells(1).RowIndex < .Tables(1).Rows.Count Then
.Tables(1).Split .Cells(1).RowIndex + 1
End If
.Collapse (wdCollapseEnd)
End With
Loop
End With
Next
Set RngFnd = Nothing
Application.ScreenUpdating = True
End Sub
If you want the split to occur before the found row, change:

If .Cells(1).RowIndex < .Tables(1).Rows.Count Then
.Tables(1).Split .Cells(1).RowIndex + 1to:

If .Cells(1).RowIndex > 1 Then
.Tables(1).Split .Cells(1).RowIndex
And, if you only want the macro to run against the selected table(s), change 'ActiveDocument' to 'Selection'.