![]() |
#1
|
|||
|
|||
![]()
Hi,
I am trying to format tables. Have to check all kinds of issues. However, when I try to check the row number in a cell that has vertically merged cells, I get an exception. (I need the row number to know if the cell can be a heading or not etc). Is there any way to access table rows even if there are vertically merged cells? Thanks, Rocky |
#2
|
||||
|
||||
![]() Quote:
PS: When posting code, please use the code tags, indicated by the # button on the posting menu. Without them, your code loses much of whatever structure it had.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
||||
|
||||
![]()
Using Rows or Columns is problematic in non uniform tables. For that you will need to check each cell instead
Code:
Sub ExploreATable() Dim aCell As Cell, aRow As Row With Selection.Tables(1) If .Uniform Then For Each aRow In .Rows Debug.Print "Row: " & aRow.Index, "Cells: " & aRow.Cells.Count Next aRow Else For Each aCell In .Range.Cells Debug.Print aCell.RowIndex, aCell.ColumnIndex Next End If End With End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#4
|
|||
|
|||
![]()
I am searching for a heading in a table and I need to modify it if it's not in the first 2 rows.
I can't (and don't want to) loop through the entire table. I just need to find out if the cell I found is in rows 1-2 or not. Code:
While Selection.Find.Found If Selection.Information(wdWithInTable) Then Set oTable = Selection.Tables(1) iRow = Selection.Rows(1).Index If iRow > 2 Then Do something.... End If End If Selection.Collapse Direction:=wdCollapseEnd Selection.Find.Execute Wend I tried to access Selection.Range.Cells(1).RowIndex - but I get the same exception. Thanks for your help, Rocky |
#5
|
||||
|
||||
![]()
If the heading uses a Heading Style (or some other Style whose scope is limited to headings), simply use Find/Replace to look for the Style as part of the Find parameters, then test whether it's in a table and, if so, get the cell's RowIndex - if it's 1 it's on the first row, and if it's 2 it's on the second row, thus:
Code:
Sub Demo() Application.ScreenUpdating = False With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Text = InputBox("What is the Text to Find") .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = True .Style = "Heading 1" .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Execute End With Do While .Find.Found If .Information(wdWithInTable) Then If .Cells(1).RowIndex < 3 Then 'Do something with the heading MsgBox .Text End If ElseIf .End = ActiveDocument.Range.End Then Exit Sub End If .Collapse wdCollapseEnd .Find.Execute Loop End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
![]()
Thanks.
But I get the same exception on : Code:
If .Cells(1).RowIndex < 3 Then I also looked at what Guessed posted, but it requires me to go over every cell and that is very time consuming in long documents. |
#7
|
||||
|
||||
![]()
Paul's code works on vertically and horizontally merged cells in my testing. How is your table configured?
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#8
|
|||
|
|||
![]()
Not sure what you mean by "how is it configured".
It have one line that is merged horizontally. (some tables have more - but horizontally merged cells are no problem). In all tables, the merged cells are in the first column to the left. There are usually 2 or 3 cells that are merged into a single cell. When other properties can affect this? |
#9
|
||||
|
||||
![]()
The code I posted works fine for any table format - i.e. no merged cells, vertically merged cells, horizontally merged cells, varying cell widths in a column, etc. in any combination. If you're getting errors, that's most likely because you've not implemented it as provided. But, since you haven't posted your code, we can't see what you're doing...
Of course, it's also possible the table you're trying to modify has been corrupted somehow. Corrupt tables can often be 'repaired' by: • converting the tables to text and back again; • cutting & pasting them to another document that you save the document in RTF format, which you then close then re-open before copying them back to the source document; or • saving the document in RTF format, closing the document then re-opening it and re-saving in the doc(x) format
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#10
|
|||
|
|||
![]()
This is the code I used (I marked the line in which I get an exception):
Code:
Sub FixWhiteFontInTable() With ActiveDocument.Range With .Find .ClearFormatting .Style = ActiveDocument.Styles("Table Heading") .Text = "" .Forward = True .Execute Format:=True, Forward:=True End With Do While .Find.Found If .Information(wdWithInTable) Then If .Cells(1).RowIndex > 2 Then Selection.Font.ColorIndex = wdBlack End If End If .Collapse wdCollapseEnd .Find.Execute Loop End With End Sub |
#11
|
||||
|
||||
![]()
Did you check whether the table might be corrupted? I don't get an error using that code. Mind you, it also won't do what you're probably expecting it to... (Hint: the code doesn't select anything - and doesn't need to). That said, one wonders why you're overriding a Style definition with hard formatting instead of either modifying the existing Style or applying another one with the required formatting.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#12
|
|||
|
|||
![]()
OK- if I create a new table and merge cells, it does work.
However, the Word file I have is generated by another software. What properties could a software give a table that would cause an exception in that row? Are there any hidden table properties? Anything I could reset? (BTW - it does select and change the color, but you are right and I should change the style and not the color). |
#13
|
||||
|
||||
![]()
Is there a reason why you can't post a document that contains an example table we can test if you remove any sensitive content?
If we can see what you are dealing with it will be a lot faster to arrive at a solution.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
![]() |
Tags |
tables;formula;automating |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
paik1002 | Excel | 6 | 09-20-2016 03:00 AM |
Combining 2 tables into 1 and use Table2's column widths (hoping for workaround dealing merged cells | CodingGuruInTraining | Word VBA | 24 | 10-07-2015 07:48 PM |
![]() |
wendyloooo | Word Tables | 1 | 05-26-2015 01:19 PM |
![]() |
tinfanide | Word | 3 | 11-24-2013 06:37 AM |
How to merge matching cells vertically? | Odiseh | Excel | 1 | 01-02-2010 02:41 PM |