I have a 2 column table which has two bookmarks named CapBegin and CapEnd
(both in the first column). The number of rows in the table will vary
(usually between 1 and 4 rows). I have tried using the following code to
capture the text in between those two bookmarks and assign it to a variable.
All variables are declared in my module. However, what is happening instead
is that all of the text from both columns is being added to the variable.
Set bmRange2 =
ActiveDocument.Range(ActiveDocument.Bookmarks("CapBegin").End, _
ActiveDocument.Bookmarks("CapEnd").Start)
bmRange2.moveend unit:=wdCharacter, Count:=-1
strCaseInfo = bmRange2.Text
Is this the proper way to do what I am trying to do? If not, I then found
the following two snippets of code for working with tables, but I can't
figure out how to modify them to do what I need.
Example 1:
Dim myRange As Range, myTable As Table
Dim myRow As Long, myCol As Long
Set myTable = ActiveDocument.Tables(1)
myRow = 1: myCol = 1
Set myRange = myTable.Cell(myRow, myCol).Range
myRange.MoveEnd unit:=wdCharacter, Count:=-1
MsgBox myRange.Text
Example 2:
Sub RetrieveCellValueFromTable()
Dim oRow As Row
Dim oCell As Cell
Dim sCellText As String
' Turn on error checking.
On Error GoTo ErrorHandler
' Loop through each row in the table.
For Each oRow In ActiveDocument.Tables(1).Rows
' Loop through each cell in the current row.
For Each oCell In oRow.Cells
' Set sCellText equal to text of the cell.
' Note: This section can be modified to suit
' your programming purposes.
sCellText = oCell.Range
' Remove table cell markers from the text.
sCellText = Left$(sCellText, Len(sCellText) - 2)
MsgBox sCellText
Next oCell
Next oRow
Exit Sub
ErrorHandler:
If Err <> 0 Then
Dim Msg As String
Msg = "Error # " & Str(Err.Number) & Chr(13) & Err.Description _
& Chr(13) & "Make sure there is a table in the current document."
MsgBox Msg, , "Error"
End If
End Sub
Anyone have any ideas on what to do? I have searched Google and the message
boards but can't find anything (perhaps I used the wrong search terms).
Thanks,
Anne P.
Jezebel - 23 Jul 2005 03:05 GMT
The problem here is that a Range is by definition a continuous stretch of
text within the StoryRange, with a start and end point. a column, however
(unless the table has only one column) is not continuous.
There are two basic approaches, depending on quite what you need to end up
with.
1) Use the Selection object. (This is the oinly instance I know of where you
can do something with the Selection object, that you can't do with
Ranges) --
Mytable.Columns(1).Select
Debug.Print Selection.Range.Text
This is fairly ugly for a number of reasons: it can fail if the table is
irregular (contains merges or splits); the resulting text has a lot of
non-printing characters in it; and using the Selection in VBA ends in tears
sooner or later.
2) Iterate the cells (like your second example) and extract the text as you
go --
Dim pCell as Word.Cell
Dim pText as String
Set pCell = MyTable.Cells(1,1)
Do
if pCell.ColumnIndex = 1 then
pText = pText & Left$(pCell.Range.Text, len(pCell.Range.Text)-1)
end if
set pCell = pCell.Next
Loop until pCell is nothing
This will work however the table is structured. You can be more efficient if
you know the table is regular --
For i = 1 to MyTable.Rows.Count
with MyTable.Cells(i, 1)
...
> I have a 2 column table which has two bookmarks named CapBegin and CapEnd
> (both in the first column). The number of rows in the table will vary
[quoted text clipped - 95 lines]
>
> Anne P.
Anne P. - 23 Jul 2005 05:14 GMT
Jezebel,
Thanks so much. I had to make several minor modifications: I have Option
Explicit on, so I had to add the row to Dimension myTable and then set a
reference to table 1 in the document. Also added " & vbCr" to the end of
the strCaseInfo line so that I had a line break between each line.
Dim pCell As Word.Cell
Dim myTable As Table
Set myTable = ActiveDocument.Tables(1)
Set pCell = myTable.Cell(1, 1)
Do
If pCell.ColumnIndex = 1 Then
strCaseInfo = strCaseInfo & Left$(pCell.Range.Text,
Len(pCell.Range.Text) - 1) & vbCr
End If
Set pCell = pCell.Next
Loop Until pCell Is Nothing
Other than that, this worked like a charm to do what I needed.
Thanks once again for your help.
Anne P.
> The problem here is that a Range is by definition a continuous stretch of
> text within the StoryRange, with a start and end point. a column, however
[quoted text clipped - 134 lines]
>>
>> Anne P.