Consulting

Results 1 to 6 of 6

Thread: Solved: Problem merging table cells

  1. #1

    Solved: Problem merging table cells

    Hi,

    I have a piece of code that extracts data from a Lotus Notes document and formats it in a table in a Word document. Column 1 is like a category heading so I would like to merge cells (2, 1), (3, 1), (4, 1) etc while leaving the other columns as they are but when I try this command:

    Set myRange = worddocument.Range(TableObj.Cell(2, 1).Range.Start, TableObj.Cell(4, 1).Range.End)
    myRange.Cells.Merge

    I get a Lotus Notes message saying:

    Microsoft Word: The requested member of the collection does not exist.

    However, if I try:

    Set myRange = worddocument.Range(TableObj.Cell(2, 2).Range.Start, TableObj.Cell(4, 2).Range.End)
    myRange.Cells.Merge

    it works fine. Basically it works in any column except column 1 of the table.

    Am I missing something really obvious here? I am not very experienced with VBA etc I'm afraid. Any help gratefully appreciated!

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Odd this. As you state if you have this:[vba]Set myRange = ActiveDocument.Range(TableObj.Cell(2, 1).Range.Start, _
    TableObj.Cell(4, 1).Range.End)
    myRange.Cells.Merge[/vba] you get an object does not exist error, BUT if you put this:[vba]Set myRange = ActiveDocument.Range(TableObj.Cell(2, 1).Range.Start, _
    TableObj.Cell(4, 1).Range.End)
    myRange.Select
    Selection.Cells.Merge[/vba]
    it does work.

    Why would it work as a selection, but not as a range??? You can merge other ranges - as noted in the post, if the cells are (2,2) and (4,2)...it works, but (2,1) and (4,1) does not...as a range, but DOES as a selection.

    Pretty darn curious.

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi and Welcome to VBAX!

    You're using the right method but you should use the argument: "MergeTo" (Which is designed for this task!)

    Like:[VBA]
    Sub MergeIt()
    Dim TableObj As Word.Table
    Dim worddocument As Word.Document
    Set worddocument = ActiveDocument
    Set TableObj = worddocument.Tables(1)

    TableObj.Cell(2, 1).Merge MergeTo:=TableObj.Cell(4, 1)
    Set TableObj = Nothing
    Set worddocument = Nothing
    End Sub
    [/VBA]

    Enjoy!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  4. #4
    Hi!

    Thanks for your replies. I've got it working now. I'm still curious why it works without the MergeTo argument for all sorts of ranges but NOT for the cells in column one. Curiouser and curiouser, as someone once said...

    Thanks again!

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi,
    You're Welcome!

    Merging Cell's or nesting tables are special jobs in Word for which Word has its own tools. (Method Merge/Argument MergeTo)

    Merging cells can cause problems if you not follow the rules. I can't explain why it Won't work as well on the first row. (Could be something to do with the fact that Word might see it as a headerrow if using the Range object.. wild guess)

    For me it just about using the right tools for the right job. Merge is the right Tool and MergeTo does the job.

    Happy coding!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  6. #6

    Merging rows in the last column.

    Quote Originally Posted by MOS MASTER View Post
    Hi,
    You're Welcome!

    Merging Cell's or nesting tables are special jobs in Word for which Word has its own tools. (Method Merge/Argument MergeTo)

    Merging cells can cause problems if you not follow the rules. I can't explain why it Won't work as well on the first row. (Could be something to do with the fact that Word might see it as a headerrow if using the Range object.. wild guess)

    For me it just about using the right tools for the right job. Merge is the right Tool and MergeTo does the job.

    Happy coding!
    Hi, thank you, your answer helped me a lot, but it does not work for the last column.

Posting Permissions

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