More Fun With Word Ranges

L

LaVerne

I am getting an error of unknown origin and appreciate any
input...

Dim oTagRange As Word.Range
oTagRange = oRange.Tables.Item(iLastTable).Cell
(iRowCounter, 1).Range

On this line of code, I get "requested member of the
collection does not exist"

Any ideas? I am going mad!

notes:
- iLastTable is the tables.count value b/c i want the very
last table in the doc at this point (iLastTable is a valid
number)
- iRowCounter is one at this point and "1" is a valid
number of rows
- I am using VB.NET to automate Word XP
 
C

Charles Maxson

LaVerne,

Think you have VB.NET on the mind... in VBA, ya still need the Set keyword
to instance an object:

Set oTagRange = oRange.Tables.Item(iLastTable).Cell(iRowCounter, 1).Range


Charles
www.officezealot.com
 
J

Jezebel

Does your table have any merged cells in it? For example, if your table
started out as 4 rows x 3 columns, and you merge the cells in column 1 rows
3 and 4, then the cell(iRowCounter,1) doesn't exist.

If this is your problem, experiment with the RowIndex and ColIndex
properties to understand what's going on.
 
L

LaVerne

Hi Charles,
Unfortunately I am in VB.NET, using a Word document
object, so there is no Set keyword. Thanks for the idea,
though.
 
K

Klaus Linke

Probably showing my total ignorance about .NET...

But wasn't there something about arrays in .NET being zero-based?
The last item would be Item(Items.Count-1)

Klaus
 
L

LaVerne

Here is what worked for me. I am posting this in the hopes
of helping some future newsgroup searcher some of the
headache this little item gave me... Thank you to all who
posted.

Private Sub ReplaceNextAvailableDateTags(ByRef
dvNextDateInfo As DataView, _
ByVal oRange As Word.Range)

'the datarowview contains the next exam info:
Dim drv As DataRowView

'how many future dates are in this datarowview:
Dim iDateCount As Integer
iDateCount = dvNextDateInfo.Count

'Used as a counter of the number of rows in the table
'NOTE: Because row 1 of the table is the "header" info,
start with row 2:
Dim iRowCounter As Integer = 2

'Find each tag and replace with the next date info:
Do While oRange.Find.Execute(FindText:="[#NEXTDATEINFO#]")
= True

For Each drv In dvNextDateInfo

If (iRowCounter - 1) <= iDateCount Then

'column one (define a range here so the original tag
can be "written over"):
Dim oTagRange As Word.Range
oTagRange = oRange.Tables.Item(1).Cell(iRowCounter,
1).Range
oTagRange.Text = drv.Row.Item("NextExamDate").ToString

'column two (in the oRange, find the 2nd column for
the next deadline date)
'NOTE: iRowCounter is the row, and "2" is the column
number:
oRange.Tables.Item(1).Cell(iRowCounter, 2).Range.Text
= drv.Row.Item("NextExamDeadline").ToString

'column three (in the oRange, find the 3rd column for
the gen location)
'NOTE: iRowCounter is the row, and "3" is the column
number:
oRange.Tables.Item(1).Cell(iRowCounter, 3).Range.Text
= drv.Row.Item("GeneralLocation").ToString

Else
'If we're out of next date info, then get out of this
loop so the
'extra tags can be removed:
Exit Do
End If

iRowCounter += 1

Next
Loop

'Get rid of exam tags:
Do While oRange.Find.Execute(FindText:="[#NEXTDATEINFO#]")
= True

'define a range for this row so it can be deleted
'NOTE: if you use oRange.Rows.Delete, then the oRange is
re-defined and
'any remaining [#NEXTDATEINFO#] tags will not be found:
Dim oRangeToDelete As Word.Range
oRangeToDelete = oRange.Tables.Item(1).Cell(iRowCounter,
1).Range
oRangeToDelete.Rows.Delete()
iRowCounter += 1

Loop

End Sub
 
J

Jay Freedman

LaVerne said:
I am getting an error of unknown origin and appreciate any
input...

Dim oTagRange As Word.Range
oTagRange = oRange.Tables.Item(iLastTable).Cell
(iRowCounter, 1).Range

On this line of code, I get "requested member of the
collection does not exist"

Any ideas? I am going mad!

notes:
- iLastTable is the tables.count value b/c i want the very
last table in the doc at this point (iLastTable is a valid
number)
- iRowCounter is one at this point and "1" is a valid
number of rows
- I am using VB.NET to automate Word XP

I'm running the code below, mostly taken from your earlier post, on a
document that contains two tables (each 2x2). It runs without errors,
both single-stepping and running flat-out. Maybe you can figure out
something by comparing it to your code.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oRange As Word.Range

oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Open("C:\temp\laverne.doc")
oRange = oDoc.Range

'Define a range
Dim oTableRange As Word.Range

'Define an int
Dim iTableCount As Integer, iRowCounter As Integer

'Set int = tables.count (I need the last table in the range)
iTableCount = oRange.Tables.Count

'Look at all rows of this table
For iRowCounter = 1 To
oRange.Tables.Item(iTableCount).Rows.Count
oTableRange =
oRange.Tables.Item(iTableCount).Cell(iRowCounter, 1).Range

'do stuff to oTableRange here...
oTableRange.Text = iRowCounter
Next iRowCounter
End Sub
 

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