MS Office Forum / Word / Programming / June 2005
Detect spanning in cell
|
|
Thread rating:  |
Vince - 16 Jun 2005 06:49 GMT I have a table like the one shown below. It's easy to see that 1992 spans from Column 2 to column 4. When I copy it in (say) notepad, I get something like 1992 2000
In other words, I lose two tabs that indicate a span. it should be 1992 2000
My question is, is it possiblet to write a VBA macro that would substitute a tab (chr 9) for any spanning? If so, how would I do this? Converting table to text does not help either. I wrote a program in perl that does all other processing. All I need to be able to do is to indicate spans by tabs from the word table. So, I should have a
\t 1992 \t \t 2000 \t \t.
I tried exploring the cells and other .tables properties but can't seem to find the right syntax.
Thanks,
Vince
1992 2000
Number Proportion of temporary
employment Proportion of total employment Number Proportion of temporary employment Proportion of total
employment
Vince - 16 Jun 2005 09:17 GMT I found this. Thanks Jezebel. The RowIndex and ColumnIndex properties provide the information you need, although as you observe, it can get *very* complex, particularly if there are cells merged vertically as well as horizontally, and if there are split cells as well.
As a general approach --
1. Iterate the table to find the cell with the highest ColumnIndex value -- this determines the number of columns needed for the HTML table. (It might be higher than the number of columns Word thinks is in the table, if there are split cells.)
2. To check for horizontal merging, compare cell with cell.next -- if the same rowindex then the number of horizontal cells merged into the first is Cell2.ColumnIndex - Cell1.ColumnIndex; if not in the same row, then merge = Column Count - ColumnIndex + 1
3. To Check for vertical merging, find the next cell with the same ColumnIndex value, then compare the rowindex values.
> Hello Everybody > I have set out to create a simple macro that converts Word tables to HTML [quoted text clipped - 9 lines] > victorious. > alfonso gonzales --------------------------------------------------------------------------------
a.. Next message: Tom Ogilvy: "Re: VBA, Bookmarks, Word and Excel... help please!" b.. Previous message: Doug Robbins: "Re: VBA, Bookmarks, Word and Excel... help please!" c.. In reply to: alfonso gonzales: "determine a merged cell span column value" d.. Messages sorted by: [ date ] [ thread ] We are proud to have Web Hosting and Rack Housing from 9 Net Avenue Deutschland. -------------------------------------------------------------------------------- This document was last modified: 02/28/05 20:56 CET (04)
> I have a table like the one shown below. It's easy to see that 1992 spans > from Column 2 to column 4. When I copy it in (say) notepad, I get [quoted text clipped - 32 lines] > > employment Jezebel - 16 Jun 2005 09:22 GMT You can detect merged cells by iterating the cells of the table and checking the ColIndex property in each case, comparing it with the ColIndex of the previous and next cells. (if the table might have vertically merged cells, you need to do likewise with the RowIndex property also).
You can also check the table's .Uniform property: if TRUE, there are no merged or split cells in the table.
> I have a table like the one shown below. It's easy to see that 1992 spans > from Column 2 to column 4. When I copy it in (say) notepad, I get [quoted text clipped - 32 lines] > > employment Vince - 16 Jun 2005 10:14 GMT Thanks for your reply.
I am totally stuck here (coffee or loo break didn't help either). Here's my code:
For i = 1 To ActiveDocument.Tables.Count
ActiveDocument.Tables(i).Select
If Not ActiveDocument.Tables(i).Uniform Then For j = 1 To ActiveDocument.Tables(i).Rows.Count
For Each acell In ActiveDocument.Tables(i).Rows(j).Cells acell.Select
MsgBox acell.Range.text & " " & acell.ColumnIndex & " " & " " & acell.Next.ColumnIndex
Next Next
End If
Next
The column index value seems to be the real value (as it appears in the table) and the merged cells are ignored. What I mean is, supposing the row has 7 cells: CELLS: 1,1 1,2 - 1,3 1,4 - 1,5 1,5 - 1,6 COLUMNS: 1 2-3 4-5 5-6
I don't get "1 2 ; 2 4 ; 4 6; 6 1 " as my message box returns, I instead get: "1 2; 2 3 ; 3 4 ; 4 1" meaning word pretends that the cells that were merged aren't there. How can I detect the span then? Please help me figure this out!
Thanks a lot!
Vince
> You can detect merged cells by iterating the cells of the table and > checking the ColIndex property in each case, comparing it with the [quoted text clipped - 41 lines] >> >> employment Jezebel - 17 Jun 2005 10:02 GMT You're quite right. Interesting. This has changed since Word 2000. It seems that Word is now following the HTML table structure more closely. (Which is no bad thing, all in all.)
As you say, if you start with a row containing seven cells and merge three pairs, you have now have a row with four cells and nothing, it seems, to indicate which cells were merged. Which is actually logical. Consider:
1. Create a table with several rows and seven columns. 2. In row one, merge cells 2-3, 4-5, 6-7 3. In row 2, merge cells 1-2, 3-4, 5-6 4. In row 3, merge cells 1-2-3-4 5. Now align the columns.
As a matter of logic, are those rows different? They all have four cells in them, of equal width.
> Thanks for your reply. > [quoted text clipped - 81 lines] >>> >>> employment Vince - 17 Jun 2005 10:43 GMT I see what you mean and would normally appreciate the way it works, but not when I am trying to tag it. Anyway, I am on the way to building a solution to this. (It's working with the tables I need to convert but I can imagine many where it may fail). I am actually getting the smallest cell width (from the row with the max number of columns) and dividing each cell's width with it. Then I remove the tabs from the cells (if any) that I have split. After splitting, I ensure that the new cells don't contain data and only contain a tab. My Perl program then processes the whole thing! Kind of strange but it's working so far!
For i = 1 To ActiveDocument.Tables.Count Call DoCaptions("Processing table: " & i & " of " & ActiveDocument.Tables.Count) ActiveDocument.Tables(i).Select
If Not ActiveDocument.Tables(i).Uniform Then
Call DoCaptions("Calculating Maximum columns")
MaximumCols = ReturnMaxTableColumns(i, RowNumber) MinColumnWidth = ReturnMinColumnWidth(i, RowNumber)
For j = 1 To ActiveDocument.Tables(i).Rows.Count Call DoCaptions("Reading row : " & j & " of " & ActiveDocument.Tables(i).Rows.Count & " in table " & i)
For Each aCell In ActiveDocument.Tables(i).Rows(j).Cells aCell.Select
Counter = Int(aCell.Width / MinColumnWidth) If Counter > 1 Then 'MsgBox "spanning: " & Counter & " rows" Call DoCaptions("Killing span") aCell.Split numcolumns:=Counter
'MsgBox aCell.Range.text Suse = aCell.Range.text Counter2 = Counter
While Counter2 > 1 Set bCell = aCell.Next Counter2 = Counter2 - 1 bCell.Select Suse = Suse & bCell.Range.text 'MsgBox bCell.Range.text bCell.Range.text = vbTab Wend Suse = Replace(Suse, vbTab, "") Suse = Replace(Suse, Chr(7), "") Suse = Replace(Suse, Chr(10), "") Suse = Replace(Suse, Chr(13), "")
aCell.Range.text = Suse
End If
Next Next
End If
Next
> You're quite right. Interesting. This has changed since Word 2000. It > seems that Word is now following the HTML table structure more closely. [quoted text clipped - 98 lines] >>>> >>>> employment Klaus Linke - 17 Jun 2005 19:19 GMT Hi Vince,
For some other ideas, see http://www.word.mvps.org/faqs/macrosvba/GetRowColSpan.htm
But I would save as filtered HTML (or maybe XML in Word2003) and look at that.
BTW, the HTML format won't be fooled by Jezebel's table... it'll show no merged cells. So probably Word figures out the rowspan and colspan cleverly when saving... It would just take some pretty sophisticated analysis of the table structure to do as well as the HTML export filters.
Usually you want to get the rowspan and colspan when you write some kind of export filter of your own. And you can save yourself a lot of work if you start from filtered HTML or WordML (XML) -- Although I myself don't do as I preach and use old macros inserting tags into the Word document, as I've used and perfected them since Word 6 ;-)
But one of these days, I'll throw them in the bin and do it properly... I swear.
Regards, Klaus
> I see what you mean and would normally appreciate the way it works, but not > when I am trying to tag it. Anyway, I am on the way to building a solution [quoted text clipped - 161 lines] >>>>> >>>>> employment Vince - 18 Jun 2005 01:53 GMT Ha ha! I have a couple of macros too that deserved to be "binned". But, you know, in a battle between business and idealism, business wins.
Thanks for the tip. I'll deviate from my current column-width analysis if it proves to be useless in the long run. I hate to think about having to write something again from scratch.
Thanks, again.
Vince
Hi Vince,
For some other ideas, see http://www.word.mvps.org/faqs/macrosvba/GetRowColSpan.htm
But I would save as filtered HTML (or maybe XML in Word2003) and look at that.
BTW, the HTML format won't be fooled by Jezebel's table... it'll show no merged cells. So probably Word figures out the rowspan and colspan cleverly when saving... It would just take some pretty sophisticated analysis of the table structure to do as well as the HTML export filters.
Usually you want to get the rowspan and colspan when you write some kind of export filter of your own. And you can save yourself a lot of work if you start from filtered HTML or WordML (XML) -- Although I myself don't do as I preach and use old macros inserting tags into the Word document, as I've used and perfected them since Word 6 ;-)
But one of these days, I'll throw them in the bin and do it properly... I swear.
Regards, Klaus
"Vince" <sdsad@fsd.com> wrote:
> I see what you mean and would normally appreciate the way it works, but > not [quoted text clipped - 169 lines] >>>>> >>>>> employment Jezebel - 18 Jun 2005 02:03 GMT > BTW, the HTML format won't be fooled by Jezebel's table... it'll show no > merged cells. Indeed it won't. That was precisely my point. As far as HTML is concerned, those rows are the same: four-column rows in a seven-column table. How they got that way is irrelevant.
|
|
|