PDA

View Full Version : Solved: Problem merging table cells



muttleee
06-23-2005, 04:30 AM
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!

fumei
06-23-2005, 10:30 AM
Odd this. As you state if you have this:Set myRange = ActiveDocument.Range(TableObj.Cell(2, 1).Range.Start, _
TableObj.Cell(4, 1).Range.End)
myRange.Cells.Merge you get an object does not exist error, BUT if you put this:Set myRange = ActiveDocument.Range(TableObj.Cell(2, 1).Range.Start, _
TableObj.Cell(4, 1).Range.End)
myRange.Select
Selection.Cells.Merge
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.

MOS MASTER
06-23-2005, 01:00 PM
Hi and Welcome to VBAX! :hi:

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

Like:
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


Enjoy! :whistle:

muttleee
06-23-2005, 01:38 PM
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! :bow:

MOS MASTER
06-23-2005, 01:46 PM
Hi, :yes
You're Welcome! :beerchug:

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! :whistle:

RogerSXXI
10-15-2019, 06:45 AM
Hi, :yes
You're Welcome! :beerchug:

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! :whistle:

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