#1
|
|||
|
|||
Top row of table not selecting in code
I'm converting some tables which have a paragraph directly above the table and would like that paragraph to be the first row of the table.
For some reason, after the code runs and adds the new row to the top of the table, the top row is not being identified as row(1). I would like to have the top row, bold, left aligned, and only the bottom border. The third table in the attached example is what I would like to achieve. |
#2
|
||||
|
||||
You should be using a style to apply to the caption rather than doing a local format for bold. Working code might be along the lines of the following
Code:
Sub UpdateTables2() Dim rng As Range Dim aTbl As Table Dim aRow As Row Dim x As Long Dim MyTables As Long: MyTables = ActiveDocument.Tables.Count Dim MyNote As String: MyNote = "Does this table have a table name" Dim answer As String Dim aRng As Range Application.ScreenUpdating = False For Each aTbl In ActiveDocument.Tables Set aRng = aTbl.Range aRng.MoveStart Unit:=wdParagraph, Count:=-1 aRng.Select ActiveWindow.ScrollIntoView aRng, True Set aRow = aTbl.Rows.Add(BeforeRow:=aTbl.Rows(1)) aRow.Range.Cells.Merge Set aRng = aRng.Paragraphs(1).Range aRng.MoveEnd Unit:=wdCharacter, Count:=-1 aRow.Range.Cells(1).Range.FormattedText = aRng.FormattedText aRng.Paragraphs(1).Range.Delete aRow.Borders(wdBorderTop).LineStyle = wdLineStyleNone aRow.Borders(wdBorderLeft).LineStyle = wdLineStyleNone aRow.Borders(wdBorderRight).LineStyle = wdLineStyleNone aRow.Range.Style = "Caption" Next aTbl Application.ScreenUpdating = True End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Hi Andrew and thanks,
I had initially setup a style, but somehow got away from it. Anyway, the style caption is set for bold and left aligned, but for some reason, the title in row 1 always stays centered. Not sure why. Modifying the style "Caption" shows bold and left aligned. Any reason why this might be happening? |
#4
|
||||
|
||||
You can reset the paragraph format to bring it back to left aligned
aRow.Range.Style = "Caption" aRow.Range.ParagraphFormat.Reset I think your table style (which has defined the paragraphs as centred) is causing the problem you are seeing. I generally try to avoid over-specifying the table styles because they can then override the paragraph styles that I use. Table styles are somewhat problematic because once you have included an 'optional' attribute like font or paragraph format settings, it can be very hard to remove it but easy to reassign.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
That seems to have done it, but hopefully I can test it out more today. Thank you!
|
#6
|
|||
|
|||
It all worked out great Andrew. Thanks again.
|
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Code to find a named (bookmarked?) table, replicate a row or table, and delete a specified table. | kevinbradley57 | Word VBA | 9 | 09-21-2017 04:58 PM |
Problem with table grid when selecting status in ribbon | lodi123 | Word VBA | 1 | 02-24-2017 05:32 AM |
Selecting a digit from a code | Warren99 | Excel | 2 | 05-29-2016 04:08 AM |
Selecting table cell vs. text | dionnaro | Word Tables | 1 | 05-24-2016 04:04 PM |
Selecting/deselecting items in pivot table | mbesspiata | Excel | 0 | 07-22-2014 08:15 AM |