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?
Source
at the beginning you should still be usingSource
in these lines of code, as well - instead ofActiveDocument
. – Cindy MeisterRange
object (or objects) and while stepping through useRange.Select
to see whether what VBA thinks is the right Range is the same one you think it should be using. – Cindy MeisterBookmarks("\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