
Signature
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
> Doug- thank you for the quick reply.
>
[quoted text clipped - 85 lines]
>
> End Sub
I added a table and row counter, and that helped me obtain additional
information (related to the original error which prompted my error
handling). It still crashes in the error handler (5991: Can't access
individual rows in this collection because the table has vertically merged
cells), but I was able to use the table and row counters to look for the
offending row. There isn't any obvious merging, but there is a text form
field (regular text, unlimited length, bookmark area, fill-in enabled) in
table 2, row 1, column 1. As far as I can tell, nothing is protected.
Interestingly, that table shows onscreen as a 2x2 table so I wouldn't have
expected my code to even stop there, since I'm screening for rows with 4
columns.
My counters are not very elegant (I wasn't sure how to derive the row and
table number directly from atable and arow), but here is the current version
of the code.
Other than splitting cells, is there any type of "unmerge" command in Word?
I close the source documents without saving, so if the problem is merged
cells, perhaps there is a way to unmerge each table before processing, then
scan the table?
Thank you for any assistance or advice,
Keith
Sub CycleFiles()
Dim atable As Table
Dim arow As Row
Dim arange As Range
Dim TempDoc As Document
Dim RootDoc As Document
Dim NumTable as Integer
Dim NumRow as Integer
Set RootDoc = Word.ActiveDocument
PathToUse = "C:\KimTest\"
myfile = Dir$(PathToUse & "*.doc")
While myfile <> ""
'Open document
Set TempDoc = Documents.Open(PathToUse & myfile)
On Error GoTo ErrorHandler
NumTable = 1
For Each atable In TempDoc.Tables
NumRow = 1
For Each arow In atable.Rows
If arow.Cells.Count = 4 Then
Set arange = arow.Cells(2).Range
arange.End = arange.End - 1
'MsgBox Len(arange)
If UCase(arange.Text) = "V" Then
arow.Range.Copy 'the row will now be on the clipboard
RootDoc.Activate
RootDoc.Tables(1).Rows.Add
RDTRC = RootDoc.Tables(1).Rows.Count
RootDoc.Tables(1).Rows(RDTRC).Cells(2).Select
Selection.Paste
RootDoc.Tables(1).Rows(RDTRC).Cells(1).Range.Text =
myfile.Name
TempDoc.Activate
End If
End If
NumRow = NumRow + 1
Next arow
NumTable = NumTable + 1
Next atable
'Close the modified document after saving changes
TempDoc.Close (False)
'Next file in folder
myfile = Dir$()
Wend
ErrorHandler:
Select Case Err.Number
Case 5991 ' "Vertical Merged Cells" error.
TempDoc.Activate
TempDoc.Tables(NumTable).Rows(NumRow).Select
'Selection.SetRange Start:=Selection.Rows(arow).Range.Start,
End:=Selection.End
MsgBox "The macro has found a vertically merged cell." & _
"Please visually check the cell to ensure that " & _
"this is not a row with critical data. If it is, " & _
"open this source file manually, select the row, " & _
"unmerge it, then re-run this macro." & _
Chr(13) & Chr(13) & _
"This macro will continue to process the remaining
files", , "Error reading merged cells"
On Error Resume Next
Case Else
' do nothing for now?
End Select
Resume
End Sub