Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-26-2019, 07:31 PM
jeffreybrown jeffreybrown is offline Top row of table not selecting in code Windows 10 Top row of table not selecting in code Office 2016
Expert
Top row of table not selecting in code
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default 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.
Attached Files
File Type: docm Testing Tables.docm (49.8 KB, 6 views)
Reply With Quote
  #2  
Old 11-26-2019, 08:29 PM
Guessed's Avatar
Guessed Guessed is offline Top row of table not selecting in code Windows 10 Top row of table not selecting in code Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #3  
Old 11-26-2019, 08:53 PM
jeffreybrown jeffreybrown is offline Top row of table not selecting in code Windows 10 Top row of table not selecting in code Office 2016
Expert
Top row of table not selecting in code
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

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?
Reply With Quote
  #4  
Old 11-26-2019, 11:01 PM
Guessed's Avatar
Guessed Guessed is offline Top row of table not selecting in code Windows 10 Top row of table not selecting in code Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #5  
Old 11-27-2019, 05:03 AM
jeffreybrown jeffreybrown is offline Top row of table not selecting in code Windows 10 Top row of table not selecting in code Office 2016
Expert
Top row of table not selecting in code
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

That seems to have done it, but hopefully I can test it out more today. Thank you!
Reply With Quote
  #6  
Old 11-28-2019, 07:28 AM
jeffreybrown jeffreybrown is offline Top row of table not selecting in code Windows 10 Top row of table not selecting in code Office 2016
Expert
Top row of table not selecting in code
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

It all worked out great Andrew. Thanks again.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Top row of table not selecting in code 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
Top row of table not selecting in code 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
Top row of table not selecting in code 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

Other Forums: Access Forums

All times are GMT -7. The time now is 06:53 AM.


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