Je travaille actuellement sur une feuille de calcul Excel qui crée une opération de fusion et publipostage, puis scinde le publipostage en fichiers de composants, sans nécessiter d'intervention une fois le programme démarré. Je rencontre le problème: après la fusion, le focus wdApplication revient au document Word d'origine et je n'arrive pas à comprendre comment faire pour que le courrier fusionne le document actif ou pour définir une variable être la nouvelle fusion de courrier afin que je puisse le manipuler là.VBA Référence au nouveau document de fusion et publipostage
Voici mon code actuel:
Dim wdApp As New Word.Application, wdDoc As Word.Document
Dim strWorkbookName As String: strWorkbookName = ThisWorkbook.FullName
Dim sections As Integer
Dim fileName As String
With wdApp
.DisplayAlerts = wdAlertsNone
Set wdDoc = .Documents.Open(ThisWorkbook.Path & "\Potential Template.docx", ConfirmConversions:=False, ReadOnly:=True, AddToRecentFiles:=False)
With wdDoc
With .MailMerge
.MainDocumentType = wdFormLetters
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
.OpenDataSource Name:=strWorkbookName, ReadOnly:=True, _
LinkToSource:=False, AddToRecentFiles:=False, _
Format:=wdOpenFormatAuto, _
Connection:="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=strWorkbookName;" & _
"Mode=Read;Extended Properties=""HDR=YES;IMEX=1"";", _
SQLStatement:="SELECT * FROM `'Final Data Set$'`", _
SubType:=wdMergeSubTypeAccess
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute
.MainDocumentType = wdNotAMergeDocument
End With
.Close SaveChanges:=wdDoNotSaveChanges
End With
.Browser.Target = wdBrowseSection
For i = 1 To ((ActiveDocument.sections.Count) - 1)
ActiveDocument.Bookmarks("\Section").Range.Copy
Documents.Add
Selection.Paste
Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
fileName = filePath & Worksheets("Final Data Set").Range(Cells(i + 1, 4)).value
ActiveDocument.SaveAs (fileName)
ActiveDocument.Close
.Browser.Next
Next i
.DisplayAlerts = wdAlertsAll
.Visible = True
.Quit SaveChanges:=wdDoNotSaveChanges
End With