Group: microsoft.public.word.vba.general
From: "Doug Robbins - Word MVP"
Date: Tuesday, March 25, 2008 2:26 AM
Subject: Re: pulling select table rows based on content, pasting to new word doc (or Excel)

I am guessing that stepping through a collection like that, when an error
occurs, Word does not retain information on where it was in the collection.
Try declaring a couple of variables to be used as counters and incremented
as you step through the collection.


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

"Ker_01" wrote in message
news:ORd73PfjIHA.5724@TK2MSFTNGP03.phx.gbl...
> Doug- thank you for the quick reply.
>
> I'm well on the way; now I've run into an error message - "Runtime error
> 5991: Cannot access individual rows in this collection because the table
> has vertically merged cells". The particular table/cell it crashes on is
> /not/ one of my target rows, I'm tempted to just add an "On Error Resume
> Next". However, these documents are in an uncontrolled environment, and
> there is a (slight) possibility that someone could have merged cells
> across two rows while putting in target data, and I don't want to miss it,
> so I plan to use an errorhandler to deal with this particular error.
>
> I've pasted my current code below. In the errorhandler, I want to activate
> the row causing the error so the user can see it onscreen, but my syntax
> for selecting that row isn't working (Runtime error 438: Object doesn't
> support this property or method).
> Thanks for any additional assistance,
> Keith
>
> '-----------------------------------------------------------------------------------------------
> Sub CycleFiles()
>
> On Error GoTo ErrorHandler
>
> Dim atable As Table
> Dim arow As Row
> Dim arange As Range
> Dim TempDoc As Document
> Dim RootDoc As Document
>
> Set RootDoc = Word.ActiveDocument
>
> PathToUse = "C:\KimTest\"
>
> myfile = Dir$(PathToUse & "*.doc")
>
> While myfile <> ""
>
> 'Open document
> Set TempDoc = Documents.Open(PathToUse & myfile)
>
> For Each atable In TempDoc.Tables
> For Each arow In atable.Rows
> If arow.Cells.Count = 4 Then
> Set arange = arow.Cells(2).Range
> arange.End = arange.End - 1
> 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
> Next arow
> 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.
> With TempDoc
> .Activate
> .atable.Rows(arow).Select '*** error 438 here ***
> 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.", , "Error
> reading merged cells"
> End With
> Case Else
> ' do nothing for now?
> End Select
> Resume
>
> End Sub
>
>
>


Safety Articles | Usenet Groups | Usenet News | Bluegrass