Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-02-2022, 08:11 PM
donnanBD donnanBD is offline Split Merged Output to Separate Documents - runtime error 4198 Windows 10 Split Merged Output to Separate Documents - runtime error 4198 Office 2016
Novice
Split Merged Output to Separate Documents - runtime error 4198
 
Join Date: Jan 2022
Posts: 2
donnanBD is on a distinguished road
Default Split Merged Output to Separate Documents - runtime error 4198

Hi all,

Using Macropods excellent macro (see source below) as a direct copy and paste, other than commenting out the split to word command. As I only want PDF files.

Getting the runtime error 4198 command failed message.

A couple of weeks ago this macro was working for me, so I know it does work on my system.

I have done a fair bit of googling to get to a solution to no luck. Any suggestions would be greatly appreciated.


Other:
- Working on local drive, desktop folder location
- Working from the locally saved MailMerged output file
- Mail merge in word using Excel from DDE option
- First paragraph of merged file for each record is Firstname Lastname, in Header Style
- section break for each record = 1
- Failing at line: .SaveAs FileName:=StrTxt & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False

Thank you!

Sub SplitMergedDocument()
' Sourced from: https://www.msofficeforums.com/mail-...ps-tricks.html
Application.ScreenUpdating = False
Dim i As Long, j As Long, k As Long, StrTxt As String
Dim Rng As Range, Doc As Document, HdFt As HeaderFooter
Const StrNoChr As String = """*./\:?|"
j = InputBox("How many Section breaks are there per record?", "Split By Sections", 1)
With ActiveDocument
' Process each Section
For i = 1 To .Sections.Count - 1 Step j
With .Sections(i)
'*****
' Get the 1st paragraph's text
StrTxt = Split(.Range.Paragraphs(1).Range.Text, vbCr)(0)
For k = 1 To Len(StrNoChr)
StrTxt = Replace(StrTxt, Mid(StrNoChr, k, 1), "_")
Next
' Construct the destination file path & name
StrTxt = ActiveDocument.Path & "" & StrTxt
'*****
' Get the whole Section
Set Rng = .Range
With Rng
If j > 1 Then .MoveEnd wdSection, j - 1
'Contract the range to exclude the Section break
.MoveEnd wdCharacter, -1
' Copy the range
.Copy
End With
End With
' Create the output document
Set Doc = Documents.Add(Template:=ActiveDocument.AttachedTem plate.FullName, Visible:=False)
With Doc
' Paste contents into the output document, preserving the formatting
.Range.PasteAndFormat (wdFormatOriginalFormatting)
' Delete trailing paragraph breaks & page breaks at the end
While .Characters.Last.Previous = vbCr Or .Characters.Last.Previous = Chr(12)
.Characters.Last.Previous = vbNullString
Wend
' Replicate the headers & footers
For Each HdFt In Rng.Sections(j).Headers
.Sections(j).Headers(HdFt.Index).Range.FormattedTe xt = HdFt.Range.FormattedText
Next
For Each HdFt In Rng.Sections(j).Footers
.Sections(j).Footers(HdFt.Index).Range.FormattedTe xt = HdFt.Range.FormattedText
Next
' Save & close the output document
'.SaveAs FileName:=StrTxt & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
' and/or:
.SaveAs FileName:=StrTxt & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
.Close SaveChanges:=False
End With
Next


End With
Set Rng = Nothing: Set Doc = Nothing
Application.ScreenUpdating = True
End Sub
Attached Images
File Type: jpg runtimeerror4198.JPG (107.5 KB, 17 views)
Reply With Quote
  #2  
Old 01-02-2022, 09:55 PM
gmayor's Avatar
gmayor gmayor is offline Split Merged Output to Separate Documents - runtime error 4198 Windows 10 Split Merged Output to Separate Documents - runtime error 4198 Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The message suggests a file path error.
Change
Code:
StrTxt = ActiveDocument.path & "" & StrTxt
to
Code:
StrTxt = ActiveDocument.path & "\" & StrTxt
However you should ensure that the document you are splitting has actually been saved or it won't have a path.
See also https://www.gmayor.com/MergeAndSplit.htm or
E-Mail Merge Add-in
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 01-03-2022, 01:40 AM
donnanBD donnanBD is offline Split Merged Output to Separate Documents - runtime error 4198 Windows 10 Split Merged Output to Separate Documents - runtime error 4198 Office 2016
Novice
Split Merged Output to Separate Documents - runtime error 4198
 
Join Date: Jan 2022
Posts: 2
donnanBD is on a distinguished road
Default Split Merged Output to Separate Documents - 4198 error

Much thanks for your reply .

Edited to below, as per your suggestion, with same error unfortunately.

Definitely working from a Saved Mail Merged output file.

Sub SplitMergedDocument()
' Sourced from: https://www.msofficeforums.com/mail-...ps-tricks.html
Application.ScreenUpdating = False
Dim i As Long, j As Long, k As Long, StrTxt As String
Dim Rng As Range, Doc As Document, HdFt As HeaderFooter
Const StrNoChr As String = """*./\:?|"
j = InputBox("How many Section breaks are there per record?", "Split By Sections", 1)
With ActiveDocument
' Process each Section
For i = 1 To .Sections.Count - 1 Step j
With .Sections(i)
'*****
' Get the 1st paragraph's text
StrTxt = Split(.Range.Paragraphs(1).Range.Text, vbCr)(0)
For k = 1 To Len(StrNoChr)
StrTxt = Replace(StrTxt, Mid(StrNoChr, k, 1), "_")
Next
' Construct the destination file path & name
StrTxt = ActiveDocument.Path & "" & StrTxt
'*****
' Get the whole Section
Set Rng = .Range
With Rng
If j > 1 Then .MoveEnd wdSection, j - 1
'Contract the range to exclude the Section break
.MoveEnd wdCharacter, -1
' Copy the range
.Copy
End With
End With
' Create the output document
Set Doc = Documents.Add(Template:=ActiveDocument.AttachedTem plate.FullName, Visible:=False)
With Doc
' Paste contents into the output document, preserving the formatting
.Range.PasteAndFormat (wdFormatOriginalFormatting)
' Delete trailing paragraph breaks & page breaks at the end
While .Characters.Last.Previous = vbCr Or .Characters.Last.Previous = Chr(12)
.Characters.Last.Previous = vbNullString
Wend
' Replicate the headers & footers
For Each HdFt In Rng.Sections(j).Headers
.Sections(j).Headers(HdFt.Index).Range.FormattedTe xt = HdFt.Range.FormattedText
Next
For Each HdFt In Rng.Sections(j).Footers
.Sections(j).Footers(HdFt.Index).Range.FormattedTe xt = HdFt.Range.FormattedText
Next
' Save & close the output document
'.SaveAs FileName:=StrTxt & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
' and/or:
.SaveAs FileName:=StrTxt & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
.Close SaveChanges:=False
End With
Next
End With
Set Rng = Nothing: Set Doc = Nothing
Application.ScreenUpdating = True
End Sub
Reply With Quote
  #4  
Old 01-03-2022, 04:55 AM
gmayor's Avatar
gmayor gmayor is offline Split Merged Output to Separate Documents - runtime error 4198 Windows 10 Split Merged Output to Separate Documents - runtime error 4198 Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

You have not changed the issue that is causing the error i.e.

Code:
StrTxt = ActiveDocument.Path & "" & StrTxt
If the document has been saved, the activedocument path would be e.g. "C:\Path".
If you add strTxt to this, the resulting path is "C:\pathstrTxt", whereas it should be "C:\path\strTxt"
Code:
StrTxt = ActiveDocument.Path & "\" & StrTxt
Ensure the document is saved before running the macro.
Code:
Sub SplitMergedDocument()
' Sourced from: https://www.msofficeforums.com/mail-...ps-tricks.html
    Application.ScreenUpdating = False
    Dim i As Long, j As Long, k As Long, StrTxt As String
    Dim Rng As Range, Doc As Document, HdFt As HeaderFooter
    Const StrNoChr As String = """*./\:?|"
    j = InputBox("How many Section breaks are there per record?", "Split By Sections", 1)
    With ActiveDocument
        .Save
        If .path = "" Then
            MsgBox "Save the document!", vbCritical
            Exit Sub
        End If
        ' Process each Section
        For i = 1 To .Sections.Count - 1 Step j
            With .Sections(i)
                '*****
                ' Get the 1st paragraph's text
                StrTxt = Split(.Range.Paragraphs(1).Range.Text, vbCr)(0)
                For k = 1 To Len(StrNoChr)
                    StrTxt = Replace(StrTxt, Mid(StrNoChr, k, 1), "_")
                Next
                ' Construct the destination file path & name
                StrTxt = ActiveDocument.path & "\" & StrTxt
                '*****
                ' Get the whole Section
                Set Rng = .Range
                With Rng
                    If j > 1 Then .MoveEnd wdSection, j - 1
                    'Contract the range to exclude the Section break
                    .MoveEnd wdCharacter, -1
                    ' Copy the range
                    .Copy
                End With
            End With
            ' Create the output document
            Set Doc = Documents.Add(Template:=ActiveDocument.AttachedTemplate.FullName, Visible:=False)
            With Doc
                ' Paste contents into the output document, preserving the formatting
                .Range.PasteAndFormat (wdFormatOriginalFormatting)
                ' Delete trailing paragraph breaks & page breaks at the end
                While .Characters.Last.Previous = vbCr Or .Characters.Last.Previous = Chr(12)
                    .Characters.Last.Previous = vbNullString
                Wend
                ' Replicate the headers & footers
                For Each HdFt In Rng.Sections(j).Headers
                    .Sections(j).Headers(HdFt.Index).Range.FormattedText = HdFt.Range.FormattedText
                Next
                For Each HdFt In Rng.Sections(j).Footers
                    .Sections(j).Footers(HdFt.Index).Range.FormattedText = HdFt.Range.FormattedText
                Next
                ' Save & close the output document
                '.SaveAs FileName:=StrTxt & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
                ' and/or:
                .SaveAs FileName:=StrTxt & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
                .Close SaveChanges:=False
            End With
        Next
    End With
    Set Rng = Nothing: Set Doc = Nothing
    Application.ScreenUpdating = True
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply

Tags
error 4198, macropod, mail merge macro



Similar Threads
Thread Thread Starter Forum Replies Last Post
Split Merged Output to Separate Documents - runtime error 4198 Word 2019 macro not working error 4198 command failed drrr Word 1 08-02-2021 12:22 AM
Split Merged Output to Separate Documents - runtime error 4198 Mail Merge - split merged documents and rename each split document based on text in header FuriousD Word VBA 1 05-12-2019 04:06 AM
Word 2010 Run-Time error 4198 with Insert Picture with Caption and Fram Macro jstills116 Word VBA 0 06-24-2016 07:46 AM
Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error zipit189 Word VBA 7 03-18-2015 01:13 PM
Runtime error 5487 - Word cannot complete the save to to file permission error franferns Word 0 11-25-2009 05:35 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:09 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft