I posted a question recently about getting my mail merge document to split and save. After finding some code online, I was able to combine it with my own code to get the document to split and create a name that I wanted. However, now when the code goes to save the document, it puts out a 5152 error, and I have no clue on how to go about it. This is what my code looks like and the error occurs at the ActiveDocument.SaveAs filename:=Fullname, fileformat:=wdFormatDocumentDefault, AddToRecentFiles:=False
Option Explicit
Sub Splitter()
' splitter Macro
' Macro created by Doug Robbins to save each letter created by a mailmergeas a separate file.
Application.ScreenUpdating = False
Dim Program As String
Dim DocName As String
Dim Letters As Integer, Counter As Integer
Dim filename, extension, Fullname, Mask As String
Letters = ActiveDocument.Sections.Count
Selection.HomeKey Unit:=wdStory
Counter = 1
While Counter < Letters
'program = ActiveDocument.MailMerge.DataSource.DataFields("Program_Outcomes_PlanReport_Name").Value
DocName = "Reports" & LTrim$(Str$(Counter)) 'Generic name of document
ActiveDocument.Sections.First.Range.Cut
Documents.Add
Selection.Paste
ActiveDocument.Sections(2).PageSetup.SectionStart = wdSectionContinuous
filename = ActiveDocument.Paragraphs(1).Range.Text
filename = Replace(filename, Chr$(13), "")
filename = Replace(filename, Chr$(10), "")
filename = Replace(filename, "/", "_")
filename = Replace(filename, "&", "_")
extension = ".docx"
DocName = "E:\assessment rubrics" & filename & " - Academic Program Review - " & Format(Now(), Mask)
Fullname = DocName & extension
ActiveDocument.SaveAs filename:=Fullname, fileformat:=wdFormatDocumentDefault, AddToRecentFiles:=False
ActiveWindow.Close
Counter = Counter + 1
Wend
Application.ScreenUpdating = True
End Sub
Run-Time error '5152': This is not a valid file name. Try one or more of the following: *Check the path to make sure it was typed coerrectly. *Select a file from the list of files and folders.
– Cocoberry2526assessment rubrics
a folder name or first words of your file name? If it is a folder name, then you should change it to"E:\assessment rubrics\"
. Also change fileformat tofileformat:=wdFormatXMLDocument
since it has adocx
extension. – Tehscript