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