Working with Tables

S

Seth Masek

I have two (probably simple) questions:

1. Why does word append cell values in tables with the
strange formatting marks? If I retrieve a cell value by:

Debug.Print ThisDocument.Tables(1).Cell(1, 1).Range.Text

I get the cell value (e.g., "MyValue") with two extra
characters that seem to be a carriage return and a tab
line character. I am correcting this by using the
following function...but I am thinking maybe there is an
easier way (if not, will this function always work?):

Debug.Print Left(ThisDocument.Tables(x).Cell(1,
1).Range.Text, _
Len(ThisDocument.Tables(x).Cell(1, 1).Range.Text) -
2)

2. How do you retrieve tables that are in the
header/footer area? These don't show up in the Tables
collection under ThisDocument --- it only counts the
tables in the body region (maybe this has something to do
with sections).

I am new to the Word object model. Any help is
appreciated!

Thanks,

Seth
 
D

Doug Robbins - Word MVP

Yes, when working with the .Range of a cell, you do need to strip off the
end of cell marker. If you declare a variable as a Range, then set it to
the .Range of the cell, you can use

RangeVariable.End = RangeVariable.End - 1

to get just the text from the cell.

There are, depending upon the version of Word, up to either 11 or 15
different story ranges in a Word document. When using code, it is in
somecases necessary to iterate through all of the story ranges and act on
the objects in each to achieve the desired result.

For more information on this, see the article "Using a macro to replace text
where ever it appears in a document

including Headers, Footers, Textboxes, etc." at:

http://word.mvps.org/FAQs/Customization/ReplaceAnywhere.htm

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
D

Dave Lett

Hi Seth,

1. There are "two extra characters" because Word uses two characters to make
the end of cell mark. FYI and FWIW, I typically use the range object with I
want to retrieve values from a cell:

Dim oRng as Range
Set oRng = ThisDocument.Tables(1).Cell(1, 1).Range
'''that's negative one, to remove the _single_ end of cell mark
'''comprised of two characters. yes, somewhat confusing, but
'''that's how to do it
oRng.MoveEnd UNIT:=wdCharacter, Count:=-1
Debug.Print oRng.Text

2. The tables in the header and footer are available by accessing that story
range. (For an understanding of storyranges, see the article "Using a macro
to replace text where ever it appears in a document including Headers,
Footers, Textboxes, etc." at
http://www.word.mvps.org/faqs/macrosvba/FindReplaceAllWithVBA.htm).

In short, you will need to access the appropriate section's header/footer
range. The referenced article does an excellent job of showing how to do
that. If you don't need to cycle through every story, then you can reference
the storyrange explicitly, as in the following:

With ActiveDocument.Sections(1)
If .Headers(wdHeaderFooterEvenPages).Exists Then
Debug.Print .Headers(wdHeaderFooterEvenPages).Range.Tables.Count
Else
Debug.Print "Header does not exist."
End If
End With

HTH,
Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top