Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-20-2015, 05:08 PM
scootermac315 scootermac315 is offline How to reference more than one table in a Word doc Mac OS X How to reference more than one table in a Word doc Office for Mac 2011
Novice
How to reference more than one table in a Word doc
 
Join Date: Jun 2015
Posts: 3
scootermac315 is on a distinguished road
Question 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!
Reply With Quote
  #2  
Old 06-21-2015, 12:14 AM
gmayor's Avatar
gmayor gmayor is offline How to reference more than one table in a Word doc Windows 7 64bit How to reference more than one table in a Word doc Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
or if users are tabbing through tables, the following macro will evaluate Row 3, column 3 of the current table as you tab out of the cell

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
Reply With Quote
  #3  
Old 06-21-2015, 12:47 AM
macropod's Avatar
macropod macropod is offline How to reference more than one table in a Word doc Windows 7 64bit How to reference more than one table in a Word doc Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
The code for a table bound to a content control is similar.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 06-22-2015, 01:27 PM
scootermac315 scootermac315 is offline How to reference more than one table in a Word doc Mac OS X How to reference more than one table in a Word doc Office for Mac 2011
Novice
How to reference more than one table in a Word doc
 
Join Date: Jun 2015
Posts: 3
scootermac315 is on a distinguished road
Default

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
Reply With Quote
  #5  
Old 06-22-2015, 05:01 PM
macropod's Avatar
macropod macropod is offline How to reference more than one table in a Word doc Windows 7 64bit How to reference more than one table in a Word doc Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You could change:
i = Len(Rng.Text) - 1
to:
i = Len(Replace(Rng.Text, vbCr, "")) - 1
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 06-22-2015, 05:28 PM
scootermac315 scootermac315 is offline How to reference more than one table in a Word doc Mac OS X How to reference more than one table in a Word doc Office for Mac 2011
Novice
How to reference more than one table in a Word doc
 
Join Date: Jun 2015
Posts: 3
scootermac315 is on a distinguished road
Smile

Works great! Thanks so much macropod
Reply With Quote
Reply

Tags
error, range, table



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to reference more than one table in a Word doc Create updating reference in table droseman Word 1 09-05-2014 05:39 PM
How to reference more than one table in a Word doc 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

Other Forums: Access Forums

All times are GMT -7. The time now is 05:47 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