Why would report format change?
If there is no selection in combobox and user runs code, it will error. Should prevent that.
Combobox DefaultValue property needs to be: "MailMergeTest.docx" - with quote marks, otherwise Access changes to: [MailMergeTest].[docx] and combobox will not populate value.
Revised code:
Code:
Private Sub MailMerge_Click()
On Error GoTo DoMailMerge_Err
Me.Requery
Dim oWord As Object
Dim oWdoc As Object
Dim wdInputName As String
Dim wdOutputName As String
Dim outFileName As String
If Me.GTEE_FORMATE & "" <> "" Then
' Set Template Path
'------------------------------------------------
wdInputName = CurrentProject.Path & "\" & Me.GTEE_FORMATE.Value
' Create unique save filename with minutes and seconds to prevent overwrite
'------------------------------------------------
outFileName = "MailMergeFile_" & Format(Now(), "yyyymmddmms")
' Output File Path w/outFileName
'------------------------------------------------
wdOutputName = CurrentProject.Path & "\" & outFileName
Set oWord = CreateObject("Word.Application")
Set oWdoc = oWord.Documents.Open(wdInputName)
' Start mail merge
'------------------------------------------------
With oWdoc.MailMerge
.MainDocumentType = 0 'wdFormLetters
.OpenDataSource _
Name:=CurrentProject.FullName, ReadOnly:=True, _
AddToRecentFiles:=False, _
LinkToSource:=True, _
Connection:="QUERY MailMergeQueryGtTrayTbl", _
SQLStatement:="SELECT * FROM [MailMergeQueryGtTrayTbl] "
.Destination = 0 'wdSendToNewDocument
With .DataSource
Select Case MailMergeOption.Value
Case Is = 3
.FirstRecord = 1
.LastRecord = .RecordCount ' replace 2o to .RecordCount to get record count
Case Is = 2
.FirstRecord = InputBox("Enter the First Record # to merge", , 1)
.LastRecord = InputBox("Enter the Last Record # to merge", , .RecordCount) ' replace 2o to .RecordCount to get record count
End Select
End With
.Execute Pause:=False
End With
' Save file as PDF
' Uncomment the line below and comment out
' the line below "Save file as Word Document"
'------------------------------------------------
'oWord.ActiveDocument.SaveAs2 wdOutputName & ".pdf", 17
' Save file as Word Document
'------------------------------------------------
oWord.ActiveDocument.SaveAs wdOutputName & ".docx", 16
' Quit Word to Save Memory
'------------------------------------------------
oWord.Quit savechanges:=False
End If
DoMailMerge_Exit:
' Clean up memory
'------------------------------------------------
If Not oWdoc Is Nothing Then Set oWdoc = Nothing
If Not oWord Is Nothing Then Set oWord = Nothing
Exit Sub
DoMailMerge_Err:
Select Case MsgBox(Error$)
End Select
Resume DoMailMerge_Exit
End Sub
I was able to output several Word docs switching between ALL and RANGE without issue. However, if code execution errors, an instance of Word is left running in Windows processes. This can cause issue because the Word file is in use. I suppose this should be dealt with in error handler or move the Quit line into Exit block. If you stop code execution in debugger then must manually end the Word process in Windows Task Manager.