A
Anne P.
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.
(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.