Microsoft Office Forums Merge the first row in a table

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-01-2018, 05:40 AM
jeffreybrown jeffreybrown is offline Merge the first row in a table Windows Vista Merge the first row in a table Office 2007
Expert
Merge the first row in a table
 
Join Date: Apr 2016
Posts: 549
jeffreybrown will become famous soon enoughjeffreybrown will become famous soon enough
Default 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
If possible, I would also like to see if the text above the table which starts with Table can be added into the new table row and all but the bottom border to remain.

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.
Attached Files
File Type: docx Before and After - Table.docx (15.2 KB, 2 views)

Last edited by jeffreybrown; 09-01-2018 at 07:47 AM.
Reply With Quote
  #2  
Old 09-01-2018, 11:53 PM
macropod's Avatar
macropod macropod is offline Merge the first row in a table Windows 7 64bit Merge the first row in a table Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,536
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
You might find it interesting to step through the code...
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #3  
Old 09-02-2018, 06:44 AM
jeffreybrown jeffreybrown is offline Merge the first row in a table Windows Vista Merge the first row in a table Office 2007
Expert
Merge the first row in a table
 
Join Date: Apr 2016
Posts: 549
jeffreybrown will become famous soon enoughjeffreybrown will become famous soon enough
Default

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
Reply With Quote
  #4  
Old 09-02-2018, 02:34 PM
macropod's Avatar
macropod macropod is offline Merge the first row in a table Windows 7 64bit Merge the first row in a table Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,536
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
  #5  
Old 09-02-2018, 02:52 PM
jeffreybrown jeffreybrown is offline Merge the first row in a table Windows Vista Merge the first row in a table Office 2007
Expert
Merge the first row in a table
 
Join Date: Apr 2016
Posts: 549
jeffreybrown will become famous soon enoughjeffreybrown will become famous soon enough
Default

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
Reply With Quote
  #6  
Old 09-02-2018, 03:33 PM
jeffreybrown jeffreybrown is offline Merge the first row in a table Windows Vista Merge the first row in a table Office 2007
Expert
Merge the first row in a table
 
Join Date: Apr 2016
Posts: 549
jeffreybrown will become famous soon enoughjeffreybrown will become famous soon enough
Default

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?
Reply With Quote
  #7  
Old 09-02-2018, 06:22 PM
macropod's Avatar
macropod macropod is offline Merge the first row in a table Windows 7 64bit Merge the first row in a table Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,536
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
  #8  
Old 09-02-2018, 07:04 PM
jeffreybrown jeffreybrown is offline Merge the first row in a table Windows Vista Merge the first row in a table Office 2007
Expert
Merge the first row in a table
 
Join Date: Apr 2016
Posts: 549
jeffreybrown will become famous soon enoughjeffreybrown will become famous soon enough
Default

Thanks Paul. That's works wonderfully.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Merge the first row in a table 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
Merge the first row in a table 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 the first row in a table Merge Two Table in VBA donbexcel Word VBA 2 10-21-2011 11:46 AM


All times are GMT -7. The time now is 01:11 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2019 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft