G
Greg
Enter a simple table and add a bit of text to a few cells.
Put a check in: Tool>Options>Edit>Use smart cut and paste
Type a few words after the table and copy it to the clipboard.
Now run:
Sub Test()
Dim tbl As Word.Table
Dim cel As Word.Cell
Dim rng As Word.Range
Set tbl = ActiveDocument.Tables(1)
For Each cel In tbl.Range.Cells
Set rng = cel.Range
rng.MoveEnd wdCharacter, -1
rng.Collapse wdCollapseEnd
rng.Paste
Next
End Sub
This has interesting results.
1. If you selected and copied a complete word or phrase (without the
leading space). The result is the text is pasted to all cells as
expected, but a run time error is generated.
Run Time Error '5825' Object has been deleted. :-(
2. If you copied only part of a word or a word/phase including a
leading
space, all is well.
I have discovered that the cause of this behaviour is
Tools>Options>Edit>Use smart cut and paste. With that option off, the
procedure works as expected with no run time error. That is the cause,
but I don't really understand the why.
I suppose that knowing this you could use an error handler:
Sub Test()
Dim tbl As Word.Table
Dim cel As Word.Cell
Dim rng As Word.Range
Set tbl = ActiveDocument.Tables(1)
On Error GoTo Handler
For Each cel In tbl.Range.Cells
Set rng = cel.Range
rng.MoveEnd wdCharacter, -1
rng.Collapse wdCollapseEnd
rng.Paste
Next
Exit Sub
Handler:
End Sub
Does anyone know if this bug, as I have called it, is documented, why
it happends, or a more appropriate workaround?
Thanks.
Put a check in: Tool>Options>Edit>Use smart cut and paste
Type a few words after the table and copy it to the clipboard.
Now run:
Sub Test()
Dim tbl As Word.Table
Dim cel As Word.Cell
Dim rng As Word.Range
Set tbl = ActiveDocument.Tables(1)
For Each cel In tbl.Range.Cells
Set rng = cel.Range
rng.MoveEnd wdCharacter, -1
rng.Collapse wdCollapseEnd
rng.Paste
Next
End Sub
This has interesting results.
1. If you selected and copied a complete word or phrase (without the
leading space). The result is the text is pasted to all cells as
expected, but a run time error is generated.
Run Time Error '5825' Object has been deleted. :-(
2. If you copied only part of a word or a word/phase including a
leading
space, all is well.
I have discovered that the cause of this behaviour is
Tools>Options>Edit>Use smart cut and paste. With that option off, the
procedure works as expected with no run time error. That is the cause,
but I don't really understand the why.
I suppose that knowing this you could use an error handler:
Sub Test()
Dim tbl As Word.Table
Dim cel As Word.Cell
Dim rng As Word.Range
Set tbl = ActiveDocument.Tables(1)
On Error GoTo Handler
For Each cel In tbl.Range.Cells
Set rng = cel.Range
rng.MoveEnd wdCharacter, -1
rng.Collapse wdCollapseEnd
rng.Paste
Next
Exit Sub
Handler:
End Sub
Does anyone know if this bug, as I have called it, is documented, why
it happends, or a more appropriate workaround?
Thanks.