PDA

View Full Version : Merge Tables



nachtschicht
06-30-2009, 09:06 AM
Hi!

I have to merge a couple of table in a word document (an application is returning one table per dataset rather than one additional row... :doh:). As there are several tables in the document which should not be touched by the macro I have added a string in front of the break (which is after every table):

(I cannot post links yet - please find the image at: security-blog.eu/wp-content/uploads/2009/06/table.png or attached)

From my understanding I should now be able to merge the tables by simply deleting the string ###FIND_ME### as well as the wrapping breaks using this function:



Sub merge_tables()

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p###FIND_ME###^p"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

... but it doesn't work that way :(

It works partly if I just replace the string ^p###FIND_ME###.

Any suggestions how I could get this function working?

Thanks!

fumei
06-30-2009, 10:31 AM
I am not following.

"there are several tables in the document which should not be touched by the macro"

"I have added a string in front of the break (which is after every table"

"wdReplaceAll"

On one hand it seems you do NOT want to ReplaceAll, yet you put the string with every table, and use ReplaceAll.

"... but it doesn't work that way

It works partly "

Please be as clear as possible. What does that mean?

You may want to try:
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
.ClearFormatting
.Text = "^p###FIND_ME###^p"
.Replacement.Text = ""
.Forward = True
Do While .Execute = True
With r
.Delete
.Collapse 0
End With
Loop
End With
The problem lies with the ending ^p being right before a table.

nachtschicht
06-30-2009, 02:45 PM
Sorry, I try to be more clear.


I am not following.

"there are several tables in the document which should not be touched by the macro"

"I have added a string in front of the break (which is after every table"

"wdReplaceAll"

The second statement should be "which is after every table which should be merged"


On one hand it seems you do NOT want to ReplaceAll, yet you put the string with every table, and use ReplaceAll.

"... but it doesn't work that way

It works partly "

The first statement was related to the search string with a starting and ending ^p. The second statement was just related to the search sting which starts with ^p


Please be as clear as possible. What does that mean?

You may want to try:
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
.ClearFormatting
.Text = "^p###FIND_ME###^p"
.Replacement.Text = ""
.Forward = True
Do While .Execute = True
With r
.Delete
.Collapse 0
End With
Loop
End With
The problem lies with the ending ^p being right before a table.

Perfect! This seems to work! :clap::clap:

THANKS!

fumei
07-02-2009, 11:44 AM
Glad I could help.

fumei
07-02-2009, 11:45 AM
If you are fine with the thread as it is, could you please mark the thread as Solved? Thanks.