i'm walking through a table, cell by cell, using VBA. and, for each cell, i
need to know whether it's
spanned over two or more rows( have it been merged with other cells). How
can i get this info?
In general, I wanna know a whole structure of a table, so my app could
recreate it.
would be glad to any hints concerning this theme.
Thanks in advance.
Lek Ych
All I can think of is:
Selection.SelectRow
If Selection.Rows.Count > 1 Then
Msgbox "This cell spans more than one row"
End If
But I don't think that will really do what you want, at least not in complex
tables. I don't think you can do what you want in VBA :-(
Regards
Dave
"Lek Ych" <boo...@mail.ru> wrote in message
news:uIGJtjL8AHA.1988@tkmsftngp02...
There have been some threads on that which you might locate by searching for
"rowspan" or "colspan" on www.googles.com (specify the microsoft.public.word.* newsgroups).
You can get the number of spanned rows, if you select the cell
(myCell.Select), then use
rowspan=Selection.Information(wdEndOfRangeRowNumber)-Selection.Information(wdStartOfRangeRowNumber).
But be careful: this does *not* work if you select the cell by hand or with
Selection.Expand Unit:=wdCell (probably a bug; I reported it as one to Microsoft.)
There doesn't seem to be a straightforward way to determine the number of
columns that a merged cell spans. A nice work-around was suggested by Jeff Hall:
> I use a work-around method for COLSPANs
>
> 1. calculate total width of table (in points)
> 2. get width of current cell in points
> 3. convert to a percentage of the width of the table (eg 27%)
> 4. use <TD COLSPAN=27 WIDTH=27%>
>
> This effectively breaks a table into 100 vertical columns, each 1% of the
> table width. The browser can easily display the cells accurately even when
> there are staggered or horizontally merged cells. No need for a load of
> complicated logic.
>
> Note that you should not use COLS=100 in the <TABLE> definition because
> (surpise surpise) NS does not like it!!
Greetings, Klaus Linke
That rowspan trick doesn't work when I try it - it alwats seems to return 0
if a single cell is selected.
Regards
Dave
"Klaus Linke" <fotosatz...@t-online.de> wrote in message
news:eA3mhnP8AHA.1752@tkmsftngp03...
I had an error in that -- you'd have to add 1 to the rowspan. The following
macro should be started in an empty document. I wrote it to demonstrate the
bug to the bug hunters at support.microsoft.com:
Sub TableBug()
'
' Selection.Information(wdEndOfRangeRowNumber) gives different results
' depending on how you selected the cell
' - by hand (mouse) or by Selection.Expand Unit:=wdCell
' - by Cell.Select
'
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, _
NumColumns:=2, DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.Cells.Merge
Selection.Collapse
Selection.Expand (wdCell)
SI_SR = Str$(Selection.Information(wdStartOfRangeRowNumber))
SI_ER = Str$(Selection.Information(wdEndOfRangeRowNumber))
Prompt$ = "row" & SI_SR & " / "
Prompt$ = Prompt$ & "row" & SI_ER & "(!)"
MsgBox Prompt$, vbOKOnly + vbInformation, "Selection.Expand -- row start/end:"
Selection.Collapse
ActiveDocument.Tables(1).Cell(1, 1).Select
SI_SR = Str$(Selection.Information(wdStartOfRangeRowNumber))
SI_ER = Str$(Selection.Information(wdEndOfRangeRowNumber))
Prompt$ = "row" & SI_SR & " / "
Prompt$ = Prompt$ & "row" & SI_ER & "(!)"
MsgBox Prompt$, vbOKOnly + vbInformation, "Cell.Select -- row start/end:"
End Sub
I use Word 2000, and the macro says row1 to row1 is selected when selecting
the merged cells with Selection.Expand, and row1 to row2 is selected when
selecting it with Cell.Select. If you select it by hand (with the mouse), you
also get a rowspan of 1 for all (merged) cells.
I don't know about other versions of Word (97/2002).
Greetings, Klaus
You're right, and the bug is also present in Word 97 - and in both versions,
the bug is also evident if you try to use ranges (Set MyRange =
ActiveDocument.Tables(1).Cell(1, 1).Range will give you a rowspan of 1
whether or not the cells are merged).
I wonder what the difference is in practice between selecting with the mouse
and using ActiveDocument.Tables(1).Cell(1, 1).Select!
Regards
Dave
"Klaus Linke" <fotosatz...@t-online.de> wrote in message
news:u3eVFnZ8AHA.1916@tkmsftngp04...
Well, i'll have to use Selection object, though, through whole my program i
used Range object only.
Dave wrote:
> I wonder what the difference is in practice between selecting with the
mouse
> and using ActiveDocument.Tables(1).Cell(1, 1).Select!
I think that'll not surprise after noticing that Selection.Expand
Unit:=wdCell doesn't work properly (in this matter), and it lays under
manual selection.
By the way, while searching the solution I detected that Cell() method and
ColumnIndex property for a cell don't work "synchronously" for table with
notorious merged cells. Imagine: a table 2x2 where cells (1,1) and (2,1)
(first column) been merged, you call Set myCell = ...Cell(2,1), then
myCell.ColumnIndex will return 2. As if Cell() think this cell is (2,1) and
cell itself believe its coordinates are(2,2).
Gone finishing program.
Lek Ych.