C
Carrie L.
I'm using the code below to create separate word documents from a mail merge.
I have 30 different letters and will generate about 5000 documents. I've
done about half, but on a couple of the mail merges I am getting the error
above and can't figure out why it is only happening on some of the letters.
I am setting them up myself and basically using the same merge fields - the
first being the document name. HELP!
Selection.EndKey Unit:=wdStory
Letters = Selection.Information(wdActiveEndSectionNumber)
Selection.HomeKey Unit:=wdStory
Counter = 1
While Counter < Letters
Application.ScreenUpdating = False
With Selection
.HomeKey Unit:=wdStory
.EndKey Unit:=wdLine, Extend:=wdExtend
.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
End With
sName = Selection
Docname = "C:\Test\" & sName & ".doc"
ActiveDocument.Sections.First.Range.Cut
Documents.Add
'Documents are based on the Normal template
'To use an alternative template follow the link.
With Selection
.Paste
.HomeKey Unit:=wdStory
.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
.Delete
End With
'here go to end, backspace, backspace, delete
Selection.EndKey Unit:=wdStory
Selection.TypeBackspace
Selection.TypeBackspace
Selection.Delete Unit:=wdCharacter, Count:=1
ActiveDocument.SaveAs FileName:=Docname, _
FileFormat:=wdFormatDocument
ActiveWindow.Close
Counter = Counter + 1
Application.ScreenUpdating = True
Wend
End Sub
I have 30 different letters and will generate about 5000 documents. I've
done about half, but on a couple of the mail merges I am getting the error
above and can't figure out why it is only happening on some of the letters.
I am setting them up myself and basically using the same merge fields - the
first being the document name. HELP!
Selection.EndKey Unit:=wdStory
Letters = Selection.Information(wdActiveEndSectionNumber)
Selection.HomeKey Unit:=wdStory
Counter = 1
While Counter < Letters
Application.ScreenUpdating = False
With Selection
.HomeKey Unit:=wdStory
.EndKey Unit:=wdLine, Extend:=wdExtend
.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
End With
sName = Selection
Docname = "C:\Test\" & sName & ".doc"
ActiveDocument.Sections.First.Range.Cut
Documents.Add
'Documents are based on the Normal template
'To use an alternative template follow the link.
With Selection
.Paste
.HomeKey Unit:=wdStory
.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
.Delete
End With
'here go to end, backspace, backspace, delete
Selection.EndKey Unit:=wdStory
Selection.TypeBackspace
Selection.TypeBackspace
Selection.Delete Unit:=wdCharacter, Count:=1
ActiveDocument.SaveAs FileName:=Docname, _
FileFormat:=wdFormatDocument
ActiveWindow.Close
Counter = Counter + 1
Application.ScreenUpdating = True
Wend
End Sub