Table has some merged cells. How to get the value from some cell andwill escape recieving error?

A

avkokin

There is the table which has some merged of cells. When I tried to get
value from first cell I get the error.
Why I don’t get value from first cell of this table? How to avoid it?
The document for example: http://www.box.net/shared/ccq86x4sgs
And where I can to read more about this (VBA and table)?
Thank's.
 
S

StevenM

To: Avkokin,

Because your table has merged cells both vertically and horizontally, you
cannot access the table via rows nor via columns. But you can do it via cells.

Sub CopyTable1ToTable2()
Dim fmTable As Table
Dim toTable As Table
Dim i As Long
Dim s As String

If ActiveDocument.Tables.Count < 2 Then Exit Sub

Set fmTable = ActiveDocument.Tables(1)
Set toTable = ActiveDocument.Tables(2)

With toTable.Range
For i = 1 To .Cells.Count
s = .Cells(i).Range.Text
s = Left(s, Len(s) - 2)
If StrComp(s, "Insert here", vbTextCompare) = 0 Then
.Cells(i).Range.Delete
s = fmTable.Range.Cells(1).Range.Text
s = Left(s, Len(s) - 2)
.Cells(i).Range.Text = s
Exit For
End If
Next i
End With
End Sub

Steven Craig Miller
 
H

Helmut Weber

Hi Anton,
There is the table which has some merged of cells.
When I tried to get value from first cell I get the error.

there is no way to avoid the error,
when trying to access cells in a merged table
by rowindex and colorindex.

However, you can use the cellindex, like that:

Sub Test8009()
Dim T1 As Table
Dim T2 As Table
Dim C1 As Cell
Dim C2 As Cell
Dim S1 As String
Set T1 = ActiveDocument.Tables(1)
Set T2 = ActiveDocument.Tables(2)
Set C1 = T1.Range.Cells(1)
Set C2 = T2.Range.Cells(13)
S1 = C1.Range.Text
S1 = Left(S1, Len(S1) - 2)
C2.Range.Text = S1
End Sub

A bit more difficult is it to find out,
what cellindex a certain cell has.

Sub Test8010()
Dim C As Cell
Dim CIndex As Long
Selection.Cells(1).Range.Bookmarks.Add "ThisCell"
For Each C In Selection.Tables(1).Range.Cells
CIndex = CIndex + 1
If C.Range.Bookmarks.Exists("ThisCell") Then
Exit For
End If
Next
MsgBox CIndex
ActiveDocument.Range.Bookmarks("ThisCell").Delete
End Sub

For a more substantial explanation, see:
http://gregmaxey.mvps.org/Table_Cell_Data.htm

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
A

avkokin

Thank you, Steven! But here enumerating all cells. But if I need to
get cells only from first 3 columns (and different number of rows). Is
it possible, if cannot access the table via rows nor via columns?
 
A

avkokin

Thank you Helmut. But if the table has many cells then enumerating
every cell is not comfort.
 
H

Helmut Weber

Hi Anton,
But if the table has many cells then enumerating
every cell is not comfort.

well, I'm enumerating cells only until the bookmark is found.
I'm sorry, I see no other way.

It is about transferring contents between tables
which have no common structure except enumeration
of cells.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
M

Manfred F

Hi avkokin,

avkokin said:
Thank you Helmut. But if the table has many cells then enumerating
every cell is not comfort.

well, there IS one way to iterate through rows or cells in a table with
merged cells (W2003):
Set the selection into the appropriate Cell in the first Row/Column, then
use Selection.SelectColumn or .SelectRow to expand Your Selection. Then You
can iterate through Selection.Range.Cells. You can evaluate the .Rowindex and
the .Columnindex.
This is one of the rare examples where using the selection object can't be
avoided.
Of course, You habe to save it before and restore it afterwards.

Hope, this helps
Manfred
 

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