Voici comment realiser une fusion publipostage en utilisant Word pour le document de sortie et Excel comme base de donnees.
1 - il faut creer sous Word un document avec les champs de fusion exactement de la meme facon qu'en mode manuel avec la base Excel que l'on va utiliser
2 - on execute le programme qui suit et la fusion s'execute on peut mettre la base a jour et relancer la fusion
le code
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
 
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include <utilcls.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
Variant vMSWord, vWDocuments, vWDocument, vFileName;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
try
        {
//    vMSWord = Variant::GetActiveObject("Word.Application");
    vMSWord = Variant::CreateObject("Word.Application");
        }
catch(...)
        {
    vMSWord = Variant::CreateObject("Word.Application");
        }
vMSWord.OlePropertySet("Visible", true);
vFileName = "c:\\publi_test.doc";
vWDocuments = vMSWord.OlePropertyGet("Documents");
vWDocument = vWDocuments.OleFunction("Open", vFileName);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    vWDocuments.OleFunction("Close", NULL, NULL);
    vMSWord.OleFunction("Quit");
    // on libere les Variants
    vMSWord = Unassigned;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
Application->Terminate();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
Variant PasswordDocument, PasswordTemplate, WritePasswordDocument, WritePasswordTemplate, SQLStatement, SQLStatement1;
PasswordDocument = "";
PasswordTemplate = "";
WritePasswordDocument = "";
WritePasswordTemplate = "";
SQLStatement = "";
SQLStatement1 = "";
String chemin, texte;
chemin = "C:\\tableau.xls";
texte = "Feuille de calcul entière";
vWDocument.OlePropertyGet("MailMerge").OlePropertySet("MainDocumentType", 0);
vWDocument.OlePropertyGet("MailMerge").OleProcedure("OpenDataSource", chemin.c_str(), 1, true, true, false, false, PasswordDocument, PasswordTemplate, false, WritePasswordDocument, WritePasswordTemplate, texte.c_str(), SQLStatement, SQLStatement1, false);
vWDocument.OlePropertyGet("MailMerge").OlePropertySet("Destination", 0);
vWDocument.OlePropertyGet("MailMerge").OlePropertySet("SuppressBlankLines", 0);
vWDocument.OlePropertyGet("MailMerge").OlePropertyGet("DataSource").OlePropertySet("FirstRecord", 1);
vWDocument.OlePropertyGet("MailMerge").OlePropertyGet("DataSource").OlePropertySet("LastRecord", 16);
vWDocument.OlePropertyGet("MailMerge").OleProcedure("Execute", false);
}
J'ai failli me peter un neurone sur ce coup la