Results 1 to 8 of 8
  1. #1
    3 Star Lounger
    Join Date
    Aug 2002
    Location
    Denmark
    Posts
    347
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Merged cells (Win XP/ Wd2003)

    Hi,

    Is there a property or similar to see programmatically, if cells in a Word table are merged?
    (equivalent to XL's property: Selection.MergeCells = True)

    Tia
    Bests,
    RD


    PS: Wish there were a knob on the TV to turn up the intelligence. There's a knob called "brightness," but that doesn't work

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Merged cells (Win XP/ Wd2003)

    Not so easily - see How to get the Rowspan and Colspan of a table cell using VBA on the Word MVP site.

  3. #3
    3 Star Lounger
    Join Date
    Aug 2002
    Location
    Denmark
    Posts
    347
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Merged cells (Win XP/ Wd2003)

    URGH!!

    Unfortunately the assumption of that article seems to be that one is using HTML tables, which I'm not..../ :..o[
    OK - thanks anyway, looks like my table battle must continue...

    btw. any idea why MS thinks that whenever one has inserted a new table within a table they should turn on .allowautofit...
    Rather anoying and a constant thread for errors, I'm constantly turning it off and it just keeps re-appearing...GRRRR

    Thanks for your answers, so far...
    Bests,
    RD


    PS: Wish there were a knob on the TV to turn up the intelligence. There's a knob called "brightness," but that doesn't work

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Merged cells (Win XP/ Wd2003)

    I don't know if you need to be using HTML tables to use that code. Starting in Word 2000, Microsoft has tried to make tables in Word work like HTML tables, so it's not illogical to pretend that they are HTML tables even while still residing in a Word document. Applying the full-blown Internet Explorer Document Object Model to a Word table probably would be overkill. <img src=/S/grin.gif border=0 alt=grin width=15 height=15> But here I think they are suggesting using regular Word objects and methods.

    Upon further review, the method involving columns doesn't sound very practical. Hmmm...

  5. #5
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Merged cells (Win XP/ Wd2003)

    I once had an urgent need to understand the structure of word tables from within VBA. The really nasty solution I found was a piece of code that started...
    <code>
    If Not Selection.Information(wdWithInTable) Then Exit Sub
    Selection.Tables(1).Range.Copy
    Set doc = Application.Documents.Add
    doc.Content.Paste
    strHTML = doc.HTMLProject.HTMLProjectItems(1).Text
    </code>

    This gave me a text string (strHTML) which held the complete HTML description of the table. I then had an HTML parsing routine that found all the <TR and <TD tags within my string and located the rowspan and colspan data.

    Messy and ugly but it kind of worked.

    StuartR

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Merged cells (Win XP/ Wd2003)

    This is a very interesting technique. I can use it to build a 2-dimensional array showing for each row and column coordinate what "cell" should be there. This data really needs to be inverted, I suppose, so that given a cell number (index into the table's cells collection) one could know its starting and ending row and column. I'm not so handy with arrays to know of an efficient way to do that, but I'm sure someone else is.
    <pre>Sub BuildRowColInfoArray()
    ' Creates 2-dim array for first table in doc showing which cell number occupies
    ' the row-column coordinates
    ' Retrieve document as an HTML string, then split it at the closing </table> tag
    Dim strTables() As String
    strTables = Split(ActiveDocument.HTMLProject.HTMLProjectItems( 1).Text, _
    "</table>", , vbTextCompare)
    If UBound(strTables) = 0 Then
    MsgBox "No tables found in document!"
    Exit Sub
    End If
    ' Set up 2-dim array for every row-column coordinate in the first table
    Dim arrRowsCols() As Integer
    ReDim arrRowsCols(1 To ActiveDocument.Tables(1).Rows.Count, _
    1 To ActiveDocument.Tables(1).Columns.Count)
    ' Dim a whole mess 'o variables
    Dim strCells() As String, intCount As Integer, intRow As Integer, intCol As Integer
    Dim intPosR As Integer, intPosC As Integer, strTempR As String, strTempC As String
    Dim intCountR As Integer, intCountC As Integer
    ' Create array of HTML cells (contains lots of junk, but we get what we need)
    strCells = Split(strTables(0), "</td>", , vbTextCompare)
    intRow = 1
    intCol = 0
    For intCount = 0 To UBound(strCells) - 1
    ' Check whether this is the start of a new row (end of previous row tag)
    If InStr(1, strCells(intCount), "</tr>", vbTextCompare) > 0 Then
    intRow = intRow + 1
    intCol = 1
    Else
    intCol = intCol + 1
    End If
    ' If column is pre-occupied as a result of a rowspan, hop over
    While arrRowsCols(intRow, intCol) <> 0
    intCol = intCol + 1
    Wend
    ' Assign cell counter (adjusting for zero-based array)
    arrRowsCols(intRow, intCol) = intCount + 1
    ' Assign same number into adjoining cells if cellSpan or rowSpan
    intPosR = InStr(1, strCells(intCount), "rowspan=", vbTextCompare)
    If intPosR > 0 Then
    strTempR = Mid(strCells(intCount), intPosR + 8, _
    InStr(intPosR + 8, strCells(intCount), " ") - (intPosR + 8))
    Else
    strTempR = "1"
    End If
    intPosC = InStr(1, strCells(intCount), "colspan=", vbTextCompare)
    If intPosC > 0 Then
    strTempC = Mid(strCells(intCount), intPosC + 8, _
    InStr(intPosC + 8, strCells(intCount), " ") - (intPosC + 8))
    Else
    strTempC = "1"
    End If
    If (strTempR > 1) Or (strTempC > 1) Then
    For intCountR = 1 To CInt(strTempR)
    For intCountC = 1 To CInt(strTempC)
    arrRowsCols(intRow + intCountR - 1, _
    intCol + intCountC - 1) = intCount + 1
    Next
    Next
    End If
    ' Done with this cell, check the next one!
    Next
    Stop 'View in the VBE's locals window
    End Sub
    </pre>

    A test document that replaces the Stop with a loop that dumps the array into the document is attached for experimentation.

  7. #7
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Merged cells (Win XP/ Wd2003)

    This revised procedure creates an array by cell number for the first table in the active document. It also illustrates how to ascertain whether the cell is merged using data in that array. Because it is slow to compile, one would want to make this array static if the application permits

  8. #8
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Merged cells (Win XP/ Wd2003)

    Jefferson,

    I have finally found time to have a look at this. That is a very neat piece of code, and has joined my library of useful stuff.

    Thank you.

    StuartR

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •