Table Help

L

LEU

I was looking at all the table questions last night and found a macro written
by Lene Fredborg that does almost what I am looking for. What I’m looking for
is how to go to the 5 text location in the cell and copy the rest of the text
(which would very in length) in that cell. The macro below will only get the
second text item.

Dim strWord1 As String
Dim oBkRange As Range
Dim strBk1 As String

'Define bookmark names
strBk1 = "bkTitle"

With ActiveDocument.Sections(1).Range.Tables(2)
'Trim removes trailing spaces
strWord1 = Trim(.Cell(1, 4).Range.Words(2).Text)
End With

'Update bookmark text
'Bookmarks must be added again
With ActiveDocument
Set oBkRange = .Bookmarks(strTitle).Range
oBkRange.Text = strWord1
'Add bookmark again
.Bookmarks.Add Name:=strBk1, Range:=oBkRange
End With
Set oBkRange = Nothing
 
L

Lene Fredborg

I am not quite sure what you mean by <how to go to the 5 text location in the
cell and copy the rest of the text>. If you mean that you want to find all
text in the cell except the first 4 characters, you could use the code below
(below, I have named the variable strBkText instead of strWord1).

Note that a table cell ends with a cell marker that has a length of 2.
Therefore, a total of 6 characters are subtracted from the string containing
all text in the cell: the first 4 characters + the cell marker)

With ActiveDocument.Sections(1).Range.Tables(1)
'First get all text in the cell
strBkText = .Cell(1, 4).Range.Text
'Now remove the first 4 characters + the cell marker (length 2)
strBkText = Mid(strBkText, 5, Len(strBkText) - 6)
End With

Note 1: The code you used as the starting point had to get text from a
footer. This is why ".Section(1)" was included. If your table is simply the
first table in the document, you only need:

With ActiveDocument.Tables(1)

Note 2: In your code, you have this line:
..Bookmarks(strTitle).Range

Your bookmark name, however, is in the variable "strBk1" so the line needs
to be changed to:

..Bookmarks(strBk1).Range

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
L

LEU

Lene,

The macro worked great.
Thank you for your help. I have learned alot about how tables work.

LEU
 

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