#1
|
|||
|
|||
How to reference more than one table in a Word doc
Hi Everyone,
First time here and a VBA noob. I have one, multi-row, 3-column table in the doc. I've figured out this code to target a specific table cell and determine if its contents are within a certain character limit--if not, the writer gets an error message. This is just one of several If statements (all the same code, just checking different cells) If Len(Selection.Tables(1).Cell(Row:=3, Column:=3).Range) > 40 Then MsgBox "40 Char Description: Exceeded Character Limit of 40 " & "(" & Len(Selection.Tables(1).Cell(Row:=3, Column:=3).Range) - 2 & ")" End If Everything was fine until I was asked to add 3 more identical tables to the doc. Then I got: "The requested member of the collection does not exist" Any thoughts? Thanks everyone! |
#2
|
||||
|
||||
It is not exactly clear what you are doing here. The macro will address the table the cursor is in. If you want to process a variety of tables, you need to loop through the tables, e.g.
Code:
Sub ProcessTables() Dim oTable As Table Dim oRng As Range For Each oTable In ActiveDocument.Tables Set oRng = oTable.Cell(Row:=3, Column:=3).Range oRng.End = oRng.End - 1 'Omit the cell end character If Len(oRng) > 40 Then oRng.Select MsgBox "40 Char Description: Exceeded Character Limit of 40 " & "(" & Len(oRng) & ")" oRng.Text = Left(oRng.Text, 40) 'trim to 40 characters End If Next oTable End Sub Code:
Option Explicit Sub NextCell() Dim oTable As Table Dim oCell As Cell Dim oRng As Range Dim iCol As Long Dim iRow As Long Set oTable = Selection.Tables(1) iCol = oTable.Columns.Count iRow = oTable.Rows.Count Set oCell = Selection.Cells(1) Set oRng = oCell.Range oRng.Collapse 1 If iCol >= 3 And iRow >= 3 Then If oCell.Range.InRange(oTable.Cell(3, 3).Range) Then oRng.End = oCell.Range.End - 1 If Len(oRng) > 40 Then MsgBox "40 Char Description: Exceeded Character Limit of 40 " & "(" & Len(oRng) & ")" oRng.Text = Left(oRng.Text, 40) 'trim to 40 characters Else If Not oRng.InRange(oTable.Cell(iRow, iCol).Range) Then Selection.Cells(1).Next.Select Selection.Collapse 1 Else oTable.Rows.Add Selection.Cells(1).Next.Select Selection.Collapse 1 End If End If End If Else If Not oRng.InRange(oTable.Cell(iRow, iCol).Range) Then Selection.Cells(1).Next.Select Selection.Collapse 1 Else oTable.Rows.Add Selection.Cells(1).Next.Select Selection.Collapse 1 End If End If End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
||||
|
||||
Personally, I'd be inclined to bookmark the table or bind it to a suitably-titled rich text content control, so it no longer matters where the table occurs in the document. Then you could use code like:
Code:
Dim Rng As Range, i As Long With ActiveDocument.Bookmarks("MyTable").Range.Tables(1) Set Rng = .Cell(Row:=3, Column:=3).Range i = Len(Rng.Text) - 1 If i > 40 Then MsgBox "40 Char Description: Exceeded Character Limit of 40 " & "(" & i & ")" End If End With
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#4
|
|||
|
|||
Thanks GM and macropod! Both great ideas. I've decided to go with the bookmarks approach and it's working great.
The only issue I'm still having is that I need a character count, including spaces, but it's counting hard returns which is throwing off my count. Is there a way to ignore HRs? Also, I apologize for not being clearer before Thanks for your help! Scott |
#5
|
||||
|
||||
You could change:
i = Len(Rng.Text) - 1 to: i = Len(Replace(Rng.Text, vbCr, "")) - 1
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
Works great! Thanks so much macropod
|
Tags |
error, range, table |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Create updating reference in table | droseman | Word | 1 | 09-05-2014 05:39 PM |
extra space when I cross-reference a table or figure | Toto | Word | 1 | 06-07-2014 03:51 PM |
Using reference table | BritBiker2 | Excel | 3 | 02-23-2013 05:27 AM |
Why does Word separate table and reference endnotes by allowing text to go in between | newby2013 | Word Tables | 2 | 12-31-2012 03:45 PM |
How do I reference a merged cell in a multi column & row table in MS Word ('03')? | jihanemo | Word Tables | 0 | 03-18-2009 08:33 AM |