PDA

View Full Version : error 5991



Strongs
09-27-2007, 02:03 PM
Hi all,

I am copying text from table rows in multiple documents and pasting the result into a table in a main document. I am getting the error 5991 "Cannot access individual rows in this collection because the table has vertically merged cells."

the row that i am copying from will always be row 1 in table 2 for each and every document. Unfortunately though, some documents, evidently have merged rows whilst others do not.

I am using the following code snippet to copy from the document collection and pasting into the main document.

Is there a way to copy from a table with merged vertical rows and avoid this error?

'loop thru open docs except Section 2.doc and copy required cells and paste into Section 2

For i = 1 To Application.Documents.Count - 1

Documents(1).Activate

'disable macros in blah docs that conflict with this macro
WordBasic.DisableAutoMacros 1


ActiveDocument.Tables(2).Rows(1).Cells(1).Select
Selection.Copy

Documents("Section 2.doc").Activate
ActiveDocument.Tables(1).Rows.Last.Cells(1).Select
Selection.Paste

Documents(1).Activate
ActiveDocument.Tables(2).Rows(1).Cells(2).Select
Selection.Copy

Documents("Section 2.doc").Activate
ActiveDocument.Tables(1).Rows.Last.Cells(2).Select
Selection.Paste


ActiveDocument.Tables(1).Rows.Last.Select
Selection.InsertRowsBelow 1

Documents(1).Activate
ActiveDocument.Close savechanges:=wdDoNotSaveChanges

Next i


Any suggestions would be welcomed

Regards,

TonyJollans
09-27-2007, 03:28 PM
Try something like:


Set OneCell = DocumentRef.Tables(2).Cells(1)
'do whatever
Set OneCell = OneCell.Next
'do whatever
' etc.

Strongs
09-27-2007, 11:34 PM
Hi Tony,

Many thanks for responding to this issue. I haven't tried your suggestion yet, but it did give me an idea to approach the issue from another direction:

'loop thru open docs except Section 2.doc and copy required cells and paste into Section 2

For i = 1 To Application.Documents.Count - 1

Documents(1).Activate

'disable macros in blah docs that conflict with this macro
WordBasic.DisableAutoMacros 1


ActiveDocument.Tables(2).Cell(1, 1).Range.Select
Selection.Copy

Documents("Section 2.doc").Activate
ActiveDocument.Tables(1).Rows.Last.Cells(1).Select
Selection.Paste

Documents(1).Activate
ActiveDocument.Tables(2).Cell(1, 2).Range.Select
Selection.Copy

Documents("Section 2.doc").Activate
ActiveDocument.Tables(1).Rows.Last.Cells(2).Select
Selection.Paste


ActiveDocument.Tables(1).Rows.Last.Select
Selection.InsertRowsBelow 1

Documents(1).Activate
ActiveDocument.Close savechanges:=wdDoNotSaveChanges

Next i

this appears to solve the problem, but I need to test it extensively on live documents today.

Thanks once again for stimulating the grey cells.

Regards,

Paul

TonyJollans
09-27-2007, 11:44 PM
It depends on exactly what your tables are like but you should probably be ok. One point I would make is that you should get and use a reference to each of your documents and be consistent in the way you address them. Activating is unneccesary (possibly excessive) overhead.

Strongs
09-28-2007, 03:32 AM
Hi Tony,

thanks for the advice. When you say reference each document, do mean reference them by name i.e. "Document.doc"? The problem for me with this approach is that the number of documents is an unknown factor. Currently the code asks the user to navigate to the folder where the documents are stored, multiselect the docs, which are then opened and then the above code does its work on the open docs. If there is a way to loop through the docs in the selected folder and open one at a time, then i would be grateful for advice on how this is done. The code worked incidentally on both documents with vertically merged cells and those without.

Regards,

Paul

TonyJollans
09-28-2007, 04:46 AM
No I mean something like ...


Set TargetDoc = Documents("Section 2.doc")

For Each SourceDoc In Documents
If SourceDoc Is TargetDoc Then
' Do Nothing
Else
SourceDoc.Tables(2).Cell.etc
...
TargetDoc.Tables(1).Rows.etc
...
End If
Next SourceDoc



Sorry - I accidentally posted half a post and my fat fingers are messing up the edit. Will post fully shortly

TonyJollans
09-28-2007, 05:22 AM
Sorry about the earlier post. This is better.


Dim SourceDoc As Document, TargetDoc As Document
Set TargetDoc = ActiveDocument
For Each SourceDoc In Documents
If SourceDoc Is TargetDoc Then
Else
With SourceDoc.Tables(2)
SourceDoc.Range(.Cell(1, 1).Range.Start, .Cell(1, 2).Range.End).Copy
End With
With TargetDoc.Tables(1).Rows.Last
TargetDoc.Range(.Cells(1).Range.Start, .Cells(2).Range.End).Paste
.Range.Rows.Add
End With
End If
Next


Here you keep a permanent reference to your target doc and then loop through all open documents ignoring the target and doing your copy as per your posted code. Doesn't keep activating, doesn't use the selection, doesn't affect the display at all. It should be much faster.

Strongs
09-30-2007, 12:41 PM
Hi Tony,
Been away for the weekend, hence delay in responding. Thank you so much for your guideance. I will replace the code with your post tomorrow.

Once again, many thanks for your advice.

regards,

Paul