File Permission Error Runtime 5487 - Splitting Mail Merge Document

W

Wendy

Hi,
I've got this code and I'm getting the above error when I run it:

Sub splitter()
'
' Splitter Macro
'
'

Dim Letters As Integer, Counter As Integer
Letters = ActiveDocument.Sections.Count
Selection.HomeKey Unit:=wdStory
Counter = 1
While Counter < Letters


ActiveDocument.Sections.First.Range.Cut
Documents.Add
Selection.Paste
Selection.HomeKey Unit:=wdStory
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Copy
Docname = Trim(Selection.Text)
ActiveDocument.Sections(2).PageSetup.SectionStart = wdSectionContinuous
ActiveDocument.SaveAs FileName:="D:\" & Docname
ActiveWindow.Close
Counter = Counter + 1
Wend

End Sub

The error occurs when trying to save each individual file - if I execute the
save manually with the clipboard contents at the point of breaking, the file
saves okay. Why would it not work in this macro context? Any clues? I've got
a 200 page document that needs splitting by tomorrow.

Thanks guys

Woo
 
R

Russ

Howdy Woo,
I think it is breaking where you amended Doug's code to get a filename.
See in message below response.
Hi,
I've got this code and I'm getting the above error when I run it:

Sub splitter()
'
' Splitter Macro
'
'

Dim Letters As Integer, Counter As Integer
Letters = ActiveDocument.Sections.Count
Selection.HomeKey Unit:=wdStory
Counter = 1
While Counter < Letters


ActiveDocument.Sections.First.Range.Cut
Documents.Add
Selection.Paste
Selection.HomeKey Unit:=wdStory
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Copy
Selection.Copy was just for testing, right?
Docname = Trim(Selection.Text)
Docname might have a paragraph mark in it?
Msgbox "zz" & Docname & "zz"

You could use:
If InStr(Docname, vbCR) <> 0 then
Docname = Trim(Left(Docname, InStr(Docname, vbCR) - 1))
End If

Remember docname must adhere to file naming syntax (punctuation may confuse
Windows OS).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top