0
votes

I am trying to split my word document by pages and save the splits as new files named from cells in various tables that are the same on each page. The error I am encountering is:

Run-time error '5941':

The requested member of the collection does not exist.

My code thus far is:

Sub splitter()
'
'
Dim Counter As Long, Source As Document, Target As Document
Set Source = ActiveDocument
Selection.HomeKey Unit:=wdStory
Pages = Source.BuiltInDocumentProperties(wdPropertyPages)
Counter = 0
While Counter < Pages
Counter = Counter + 1
DocName = "" _
    & Left(ActiveDocument.Tables(3).Rows(1).Cells(2).Range.Text, _
        Len(ActiveDocument.Tables(3).Rows(1).Cells(2).Range.Text)) _
    & Left(ActiveDocument.Tables(5).Rows(1).Cells(2).Range.Text, _
        Len(ActiveDocument.Tables(5).Rows(1).Cells(2).Range.Text)) _
    & Left(ActiveDocument.Tables(6).Rows(1).Cells(2).Range.Text, _
        Len(ActiveDocument.Tables(6).Rows(1).Cells(2).Range.Text))
Source.Bookmarks("\Page").Range.Cut
Set Target = Documents.Add
Target.Range.Paste
Target.SaveAs FileName:=DocName
Target.Close
Wend
End Sub

The error specified in the title occurs within these lines of the code:

DocName = "" _
    & Left(ActiveDocument.Tables(3).Rows(1).Cells(2).Range.Text, _
        Len(ActiveDocument.Tables(3).Rows(1).Cells(2).Range.Text)) _
    & Left(ActiveDocument.Tables(5).Rows(1).Cells(2).Range.Text, _
        Len(ActiveDocument.Tables(5).Rows(1).Cells(2).Range.Text)) _
    & Left(ActiveDocument.Tables(6).Rows(1).Cells(2).Range.Text, _
        Len(ActiveDocument.Tables(6).Rows(1).Cells(2).Range.Text))

I am not sure how to resolve this error.

A second question is where can I set the directory that the documents are saved to in this code?

1
Do you have (at least) six tables in the document? And is the document the "active" one that you think it is? Do each of these tables have (at least) two cells? Note that, since you set the active document to Source at the beginning you should still be using Source in these lines of code, as well - instead of ActiveDocument.Cindy Meister
If you cut a page, Table(2) becomes Table(1). ;-) Better to loop backwards - or always refer to Table(1)LocEngineer
I have 8 tables per page; i I am 99.99% sure it is the "active" document. I have made the amendments to my code, thanks for pointing that out! I know receive a '424' Object required error for the same section of code.... DO you know what may cause this? Do I need to define it?Infernez
In your place, I'd probably step through the code (F8) to determine exactly what line is triggering it. I'd also declare and assign to a Range object (or objects) and while stepping through use Range.Select to see whether what VBA thinks is the right Range is the same one you think it should be using.Cindy Meister
Additional note: Bookmarks("\Page") is going to refer to the current SELECTION (page with the selection). Things may be going wrong because that's not what you expect - selecting one of the Table / Row / Cell ranges might also help.Cindy Meister

1 Answers

0
votes

At least part of the problem is that you're getting the end-of-cell characters for each cell. Try:

DocName = "" _
    & Split(Source.Tables(3).Cell(1 ,2).Range.Text, vbCr)(0) _
    & Split(Source.Tables(5).Cell(1, 2).Range.Text, vbCr)(0) _
    & Split(Source.Tables(6).Cell(1, 2).Range.Text, vbCr)(0)

You also aren't supplying the file extension, format, etc. as part of the SaveAs.

You may also be interested in Split Merged Output to Separate Documents in the Mailmerge Tips and Tricks thread at: http://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html which, amongst other things, shows how to supply the required SaveAs parameters.