Hi guys,
I get an error as 'Requested object not available' on line
.Destination = wdSendToNewDocument
I'm not sure what I should define this as?
Cheers in advance
I get an error as 'Requested object not available' on line
.Destination = wdSendToNewDocument
I'm not sure what I should define this as?
Cheers in advance
VBA Code:
Sub Mail_Merge()
Application.ScreenUpdating = False
Dim StrFolder As String, StrName As String, MainDoc As Document, i As Long, j As Long
Const StrNoChr As String = """*./\:?|"
Dim MyDate
Dim Month
MyDate = Format(Date, "yyyymmdd")
Month = Format(Date, "mmmm")
Set MainDoc = ActiveDocument
With MainDoc
StrFolder = "C:\Users\Letters Sent\" & Month & "\"
With .MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
On Error Resume Next
For i = 1 To .DataSource.RecordCount
With .DataSource
.FirstRecord = i
.LastRecord = i
.ActiveRecord = i
If Trim(.DataFields("FirstName")) = "" Then Exit For
'StrFolder = .DataFields("Folder") & "\"
StrName = MyDate & " - " & .DataFields("File_Name")
End With
.Execute Pause:=False
If Err.Number = 5631 Then
Err.Clear
GoTo NextRecord
End If
For j = 1 To Len(StrNoChr)
StrName = Replace(StrName, Mid(StrNoChr, j, 1), "_")
Next
StrName = Trim(StrName)
With ActiveDocument
'.SaveAs FileName:=StrFolder & StrName & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
' and/or:
.SaveAs FileName:=StrFolder & StrName & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
.Close SaveChanges:=False
End With
NextRecord:
Next i
End With
End With
Application.ScreenUpdating = True
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
End Sub