Results 1 to 12 of 12

Thread: [Word] 2003 VBA - Absolute Row/Col in Tables with Merged Cells

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    7

    [Word] 2003 VBA - Absolute Row/Col in Tables with Merged Cells

    I need to validate the structure of a Word table in a document using VBA. For example, assume there is a 4x4 table with the middle 4 cells merged I need to determine for EVERY cell:
    the staring ABSOLUTE row, column,
    the number of horizontal merged cells,
    the number of vertical merged cells

    With this I can determine if the table is the correct format and in what cell I will find the information I need to extract for the rest of the process.

    The merged cells would report:
    row 2, column 2, vertical 2, horizontal 2

    The next cell on row 2 would report:
    row 2, column 4, vertical 1, horizontal 1

    I have tried the .Information() options, .Rowindex, etc. But to no avail.

    Help!

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: [Word] 2003 VBA - Absolute Row/Col in Tables with Merged Cells

    Welcome to the Forums.

    You may want to look into the tables properties as shown in my faq item - http://vbforums.com/showthread.php?t=402098

    There is a .Table.Cell(x, y) object property that should give you what you need.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    7

    Re: [Word] 2003 VBA - Absolute Row/Col in Tables with Merged Cells

    Guru,

    Given the 4x4 table with the 4 center cells merged, the 2nd row, right-most cell is really tables(1).cell(2,3) as you would expect.

    What you posted worked for unmerged tables.

    I know how to refer to the cells as items and using row column but with merged cells these are relative. I need the info about the referenced cell.

    Any other suggestions?

    Thanks

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: [Word] 2003 VBA - Absolute Row/Col in Tables with Merged Cells

    Well I was just trying to show that you can access all the table properties etc. but,...

    If you use the cells.count property for the merged row and it returns 1 then its merged all the way across.

    Code:
    ActiveDocument.Tables(1).Rows(2).Cells.Count
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    7

    Re: [Word] 2003 VBA - Absolute Row/Col in Tables with Merged Cells

    True, this tells me if the whole row merged across. The problem is that i need to figure out the absolute positions and sizes of merged cells so I can pick the correct cell to get my data from. The stucture of the table is not enforced to the author, just the appearance of the table. So I need to definitively determine at least the absolute column number of a referenced cell. As of now, I see nothing to determine this.

    Is there anything in the object model I have overlooked? I suspected the:
    .Information(wd{Start/End}ofRange{Column/Row})
    would have done it but it does not.

    Thanks, I know you are trying to help - and I appreciate it!

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: [Word] 2003 VBA - Absolute Row/Col in Tables with Merged Cells

    Maybe I am missing the point or what you are really needing beacause if you can tell if the row has a merged cell in it with the above code what else would you need or why wouldnt that work for you. Can you give me a bit more background info on ht eproblem perhaps?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    7

    Re: [Word] 2003 VBA - Absolute Row/Col in Tables with Merged Cells

    <see table in attachement for details that follow>

    I need Data as shown that is directly below the “Need 1” column wherever possible or detect that the table is constructed incorrectly and made to “look” correct. If borders were not on you could not tell even by eye what cell the needed data is in.

    From within Word if you select the “Need 1” column and paste it into Notepad you get exactly the data I need! How does Word do it?

    Using all the programatic techniques I know only row “A” returns data reliably

    1) I can’t use the following because it generate an error:
    ActiveDocument.Tables(1).Columns(3).Select

    2) If all rows had the same number of cells I could calculate the cell ref by:
    i = row*5 -1 i=9, 14, 19, 24
    d$ = ActiveDocument.Tables(1).Cells(i) .Range.Text

    But I can’t calculate what cells I need since all rows don’t have the same number of cells. If I knew that:
    row “B” column 3 was merged cell 2 columns wide (subtract 1) 13=14-1
    row “C” column 2 was merged cell 2 columns wide (subtract 1+1) 17=19-2
    row “C” column 4 was merged cell 2 columns wide (subtract 1+1+0) 22=24-2

    Then: I could get the data I needed.

    Please look at row “C”. Obviously by looking at this the dilemma is clear but there is no way (I have figured yet) to programmatically determine that there are 2 merged cells in the middle of row 2 and that the cell I need is 17 instead of 19.

    3) I tried:
    Rows(n=2,3,4).Range.Cells(4).Range.Text

    Again, like above, if I knew the width of each cell (in columns) then I could do what I needed.

    Absolutely Troubled,
    Geoboater
    Attached Files Attached Files

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: [Word] 2003 VBA - Absolute Row/Col in Tables with Merged Cells

    Well I had a whole reply typed but stupid connection issues with the site caused me to lose it.

    Anyways I'll try to rewrite it as best I can.

    The problem is not just the merged cells but the existance of a Tab as shown in the attachment image's red lines. This is why the cell is left aligned and yet still appears to the right align.

    Now to get around the mixed cell width errors you can more easily read it from the rows direction vs the column direction.

    Code:
    If ActiveDocument.Tables(1).Rows(3).Cells.Count <> 5 Then
        'Row contains a merged cell or more
    End If
    Then to select the cell you do this...
    Code:
    ActiveDocument.Tables(1).Rows(3).Cells(3).Range.Cells.Item(1).Select
    And to read the cell...
    Code:
    Debug.Print ActiveDocument.Tables(1).Rows(3).Cells(3).Range.Text
    Attached Images Attached Images  
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    7

    Re: [Word] 2003 VBA - Absolute Row/Col in Tables with Merged Cells

    I know HOW to select the cell. But how do I know WHAT cell to select since I need:

    cell 3 in row "C" but
    cell 4 in row "A" & "D"

    And the only reason I need cell 3 in "C" is becasue I have a merged cell. If I new this cell was 2 columns wide then I know what to do. Otherwise I do not. You cannot use the cells in row becasue you do not know where in the row the merged cell is located (like in row "D" where its at the end)

    Looking at the table is not possible which is what you are doing. I need to figure it out programatically.

    And yes, the user (who I have no control over) puts the tab in to make it "look" correct. I know its there programatically and can deal with it.

    Please look back at my original post:

    Ineed to validate the structure of a Word table in a document using VBA. For example, assume there is a 4x4 table with the middle 4 cells merged I need to determine for EVERY cell:
    the staring ABSOLUTE row, column,
    the number of horizontal merged cells,
    the number of vertical merged cells

    Can I get this information?

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: [Word] 2003 VBA - Absolute Row/Col in Tables with Merged Cells

    Well I was only pointing it out since it wasnt mentioned about the tab and stuff but anyways...


    For validating the structure I gave you code to determine the cell counts. This will tell you if its got the correct cell count in each row. If not then it contains a merged cell.

    You can not detect if a cell is merged with the word object model other then doing the count comparison. There is no property or function that will tell you if a cell or row or table contains merged cells.

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    7

    Re: [Word] 2003 VBA - Absolute Row/Col in Tables with Merged Cells

    Ok, That's what I thought - no merged-cell properties - but I hoped I was mistaken! Thanks for that info.

    Of course this begs the following questions:
    1) How does Word get it right when you select/copy/paste-to-Notepad on column 3
    2) Do you know of any properties that will fail on a merged cell (without splitting it) that can be trapped with "On error" to detect which cells are merged.
    3) Might the .row(r).Cells(c).Information(wd{Horizontal/Vertical}PositionRelativetoPage) on the 1st character in the cell allow me to derive what is needed. If I have the 1st row without merged cells then I get the number of twips for the start of each column. Hmmm

    Sorry, but troubled....

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: [Word] 2003 VBA - Absolute Row/Col in Tables with Merged Cells

    1. Probably something with taking the selection object but just a guess.
    2. ActiveDocument.Tables(1).Columns(3).Cells.Count 'Assumes column 3 is a column with a merged cell in it.
    3. There is no .Information object/property off of a Column. There is for the .Range of a column though but nothing in there will provide any relevant info.

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width