#1
|
|||
|
|||
Merge the first row in a table
I'm converting PDFs to Word and most tables in the document have merged rows, but not sure how many columns. They all differ.
I'm adding a row to the top of the table, but then it needs to be merged. I've found the macro to add the row, but can't find how to merge it to the entire row. I did find a macro to merge, by only the first two columns. How can I find the end of the table for the first row to merge the first row no matter how many columns? Code:
Sub Add_Rows_Table() Application.ScreenUpdating = False Dim Tbl As Table, Rng As Range For Each Tbl In ActiveDocument.Tables Tbl.Cell(1, 1).Range.Select Selection.InsertRowsAbove 1 Next Tbl Application.ScreenUpdating = True End Sub Code:
Sub Test_This() Dim mrgrng As Range With Selection.Tables(1) Set mrgrng = .Cell(1, 1).Range mrgrng.End = .Cell(1, 2).Range.End mrgrng.Cells.Merge End With End Sub Note: It seems the overall problem is the vertically merged cells. I can't seem to find how to work around the vertically merged cell to format the newly added first row of the table. Last edited by jeffreybrown; 09-01-2018 at 07:47 AM. |
#2
|
||||
|
||||
Because your tables have merged cells, some circumlocution is required:
Code:
Sub Add_Rows_Table() Application.ScreenUpdating = False Dim Tbl As Table, Rng As Range For Each Tbl In ActiveDocument.Tables With Tbl .Rows.Add Set Rng = .Range .Range.Cells(.Range.Cells.Count).Range.InsertBreak Type:=wdColumnBreak Rng.Tables(2).Range.Cells.Merge .Range.Characters.First.Previous.InsertBefore vbCr .Range.Characters.First.Previous.Previous.FormattedText = Rng.Tables(2).Range.FormattedText Rng.Tables(2).Delete .Range.Characters.First.Previous.Delete .Range.Characters.Last.Next.Delete End With Next Tbl Set Rng = Nothing Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Hi Paul and thanks,
Indeed, stepping thru the code was very interesting. So, as I understand the code, you are counting the number of cells and making the table split before the last row then using that format to format the title of the table. In my document, these tables are in-between two specific words like shown below. Can the macro start with the word Attachment 3 and end with either Attachment 4 or if no Attachment 4, then simply the end of the document? 9 times out of 10 there will be an Attachment 4, but just in case, the end of the document would be the final. Attachment 3 Table 1 Table 2 Table 3 etc. Attachment 4 |
#4
|
||||
|
||||
The code adds a row to the table, splits that off, merges its cells, then replicates the row at the top of the table, before deleting it and filling the new row at the top of the table with the 'Attachment' text.
It would have been helpful if you had said you only wanted to process two specific tables. You could simply add an If test to the 'With Tbl' block to check what the preceding paragraph contains. For example: Code:
With Tbl If .Range.Characters.First.Previous.Paragraphs.Last.Range.Text Like "Attachment [3-4]" & vbCr Then ... End If End With
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Here is what I tried after including the If test, but I get a "Compile Error: Method or data member not found"
It debugs to - .Text I'm I right on thinking the [3-4] should be changed to [3] if I'm starting with Attachment 3 as the beginning point? Code:
Sub Add_Rows_Table() Application.ScreenUpdating = False Dim Tbl As Table, Rng As Range For Each Tbl In ActiveDocument.Tables With Tbl If .Range.Characters.First.Previous.Paragraphs.Last.Text Like "Attachment [3]" & vbCr Then .Rows.Add Set Rng = .Range .Range.Cells(.Range.Cells.Count).Range.InsertBreak Type:=wdColumnBreak Rng.Tables(2).Range.Cells.Merge .Range.Characters.First.Previous.InsertBefore vbCr .Range.Characters.First.Previous.Previous.FormattedText = Rng.Tables(2).Range.FormattedText Rng.Tables(2).Delete .Range.Characters.First.Previous.Delete .Range.Characters.Last.Next.Delete End If End With Next Tbl Set Rng = Nothing Application.ScreenUpdating = True End Sub |
#6
|
|||
|
|||
Sorry for my omission, but the words Attachment are not part of any table. They are simply text in the document. With the If setup you provided, that would be directed to a table. Correct?
Note: On second though, since the text at the beginning of each table starts with Table 3, can the If test be off of the text at the start of the table? |
#7
|
||||
|
||||
In that case, you might use:
Code:
With Tbl If .Range.Characters.First.Previous.Paragraphs.Last.Range.Text Like "Table [3-4].*" & vbCr Then ... End If End With
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
Thanks Paul. That's works wonderfully.
|
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How do I mail merge records into table columns with more than one merge field? | tech123 | Mail Merge | 1 | 04-26-2017 07:13 PM |
Table Merge | murazlan | Mail Merge | 3 | 11-21-2016 08:05 PM |
Merge only the text in a table and keep the 2 original colums of a table | james1979uk | Word Tables | 5 | 12-15-2013 05:48 PM |
Cannot Merge Into A Table | MacDee | Mail Merge | 1 | 01-29-2013 07:21 PM |
Merge Two Table in VBA | donbexcel | Word VBA | 2 | 10-21-2011 11:46 AM |