IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C++Builder Discussion :

Mailling Enveloppes OleWord/Excel


Sujet :

C++Builder

  1. #1
    Rédacteur
    Avatar de blondelle
    Homme Profil pro
    Inscrit en
    Mars 2006
    Messages
    2 738
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 2 738
    Par défaut Mailling Enveloppes OleWord/Excel
    Bonjour
    Voici comment effectuer un Mailling d'enveloppes en utilisant Word, la source de donnees et dans un classeur Excel
    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
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
     
    //---------------------------------------------------------------------------
    #include <vcl.h>
    #pragma hdrstop
    #include "Unit1.h"
    #include <utilcls.h>
    //#include<Comobj.hpp>
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    Variant vMSWord, vWDocuments, vWDocument;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
    Button2->Visible = false;
    }
    //---------------------------------------------------------------------------
    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:\\Debut de lettre.doc";
    //vWDocuments = vMSWord.OlePropertyGet("Documents");
    //vWDocument = vWDocuments.OleFunction("Open", vFileName);
    vWDocuments = vMSWord.OlePropertyGet("Documents");
    vWDocument = vWDocuments.OleFunction("Add");
    Button2->Visible = true;
    if (Form1->Visible) // mettre l'appli toujours au premier plan
      {
    Form1->Visible = true;
    Form1->BringToFront();
      }
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Button3Click(TObject *Sender)
    {
        vWDocuments.OleFunction("Close", NULL, NULL);
        vMSWord.OleFunction("Quit");
        // on libere les Variants
        vMSWord = Unassigned;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Button4Click(TObject *Sender)
    {
    Application->Terminate();        
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Button2Click(TObject *Sender)
    {
    Variant Address, ReturnAddress, AutoText;
    Address = "";
    ReturnAddress = "";
    AutoText = "";
    // on va effectuer un mailling Enveloppe
    vWDocument.OlePropertyGet("MailMerge").OlePropertySet("MainDocumentType", 2); // wdEnvelopes = 2
    // type d'enveloppe
    vWDocument.OlePropertyGet("Envelope").OlePropertySet("DefaultSize", "DL");
    // initialisation a "" expediteur, destinataire
    vWDocument.OlePropertyGet("Envelope").OleProcedure("Insert", false, WideString(Address), WideString(AutoText), false, WideString(ReturnAddress));
    // parametrage et affichage expediteur
    vMSWord.OlePropertyGet("Selection").OlePropertyGet("Font").OlePropertySet("Size", 8);
    vMSWord.OlePropertyGet("Selection").OlePropertyGet("Font").OlePropertySet("Name", "Arial");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", "blondelle jean-pierre");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeParagraph");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", "chemin xxxx");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeParagraph");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", "quartier xxxx");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeParagraph");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", "30000 xyxyxyxy");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeParagraph");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", "France");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeParagraph");
    // on se positionne sur la fenetre destinataire
    vWDocument.OlePropertyGet("Envelope").OlePropertyGet("Address").OleProcedure("Select");
    vMSWord.OlePropertyGet("Selection").OleProcedure("MoveDown", 5, 2); // wdLine = 5, Count = 2
    // affichage des parametres de fusion
    vWDocument.OlePropertyGet("MailMerge").OlePropertyGet("Fields").OleProcedure("Add", vMSWord.OlePropertyGet("Selection").OlePropertyGet("Range"), "Prenom");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", " ");
    vWDocument.OlePropertyGet("MailMerge").OlePropertyGet("Fields").OleProcedure("Add", vMSWord.OlePropertyGet("Selection").OlePropertyGet("Range"), "Nom");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeParagraph");
    vWDocument.OlePropertyGet("MailMerge").OlePropertyGet("Fields").OleProcedure("Add", vMSWord.OlePropertyGet("Selection").OlePropertyGet("Range"), "ADRESSE");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeParagraph");
    vWDocument.OlePropertyGet("MailMerge").OlePropertyGet("Fields").OleProcedure("Add", vMSWord.OlePropertyGet("Selection").OlePropertyGet("Range"), "CODE_POSTAL");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", " ");
    vWDocument.OlePropertyGet("MailMerge").OlePropertyGet("Fields").OleProcedure("Add", vMSWord.OlePropertyGet("Selection").OlePropertyGet("Range"), "Ville");
    Variant PasswordDocument, PasswordTemplate, WritePasswordDocument, WritePasswordTemplate, SQLStatement, SQLStatement1;
    PasswordDocument = "";
    PasswordTemplate = "";
    WritePasswordDocument = "";
    WritePasswordTemplate = "";
    SQLStatement = "";
    SQLStatement1 = "";
    String Chemin, Connection;
    Chemin = "C:\\tableau.xls";
    Connection = "Feuille de calcul entière";
    // ouverture de la source
    // une boite de dialogue s'affiche
    vWDocument.OlePropertyGet("MailMerge").OleProcedure("OpenDataSource", Chemin.c_str(), 1, true, true, false, false, PasswordDocument, PasswordTemplate, false, WritePasswordDocument, WritePasswordTemplate, Connection.c_str(), SQLStatement, SQLStatement1, false);
    // parametre de l'adresse Gras, Times New Roman, Size 16
    vWDocument.OlePropertyGet("Envelope").OlePropertyGet("AddressStyle").OlePropertyGet("Font").OlePropertySet("Bold", true);
    vWDocument.OlePropertyGet("Envelope").OlePropertyGet("AddressStyle").OlePropertyGet("Font").OlePropertySet("Name", "Times New Roman");
    vWDocument.OlePropertyGet("Envelope").OlePropertyGet("AddressStyle").OlePropertyGet("Font").OlePropertySet("Size", 16);
    // caracteres en majuscules
    vWDocument.OlePropertyGet("Envelope").OlePropertyGet("AddressStyle").OlePropertyGet("Font").OlePropertySet("AllCaps", 9999998);
    // on fusionne
    vWDocument.OlePropertyGet("MailMerge").OleProcedure("Execute", false);
    }
    //---------------------------------------------------------------------------
    il reste cependant une boite de dialogue imposee par Word qui s'affiche a l'execution de cette ligne
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    vWDocument.OlePropertyGet("MailMerge").OleProcedure("OpenDataSource", Chemin.c_str(), 1, true, true, false, false, PasswordDocument, PasswordTemplate, false, WritePasswordDocument, WritePasswordTemplate, Connection.c_str(), SQLStatement, SQLStatement1, false);
    Si quelqu'un a une idee pour recuperer le Handle de cette boite il serait peut etre possible de simuler un click sur le boutton "ok", ca n'empecherais pas l'affichage de la boite mais il n'y aurait aucune intervention de l'utilisateur

  2. #2
    Membre éprouvé
    Avatar de Sunchaser
    Homme Profil pro
    OPNI (Objet Programmant Non Identifié)
    Inscrit en
    Décembre 2004
    Messages
    2 059
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : OPNI (Objet Programmant Non Identifié)
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Décembre 2004
    Messages : 2 059
    Par défaut
    Salut,

    Je ne connais pas du tout OleWord, donc je vais peut être dire une bêtise, mais n'est ce pas le premier boolean qu'il faudrait passer a false ?
    Je dis ca a cause de : ca

    @+

  3. #3
    Rédacteur
    Avatar de blondelle
    Homme Profil pro
    Inscrit en
    Mars 2006
    Messages
    2 738
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 2 738
    Par défaut
    Merci Sunchaser
    C'est exactement l'aide en ligne de Word, j'ai teste tous les parametres sans succes, j'ai poste dans le Forum Word il n'y a apparement pas de solution, je ne vois pas autre chose que de trouver une astuce pour recuperer le Handle de la boite de dialogue et simuler un click sur le Button

  4. #4
    Membre éprouvé
    Avatar de Sunchaser
    Homme Profil pro
    OPNI (Objet Programmant Non Identifié)
    Inscrit en
    Décembre 2004
    Messages
    2 059
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : OPNI (Objet Programmant Non Identifié)
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Décembre 2004
    Messages : 2 059
    Par défaut
    Arrfff,
    Je savais que j'étais un peu léger avec ma supposition, qui devait forcement déjà être testée.

  5. #5
    Rédacteur
    Avatar de blondelle
    Homme Profil pro
    Inscrit en
    Mars 2006
    Messages
    2 738
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 2 738
    Par défaut
    Non ne crois pas cela les choses les plus evidentes sont le plus difficile a trouver et on oublie toujours quelque chose

  6. #6
    Rédacteur
    Avatar de blondelle
    Homme Profil pro
    Inscrit en
    Mars 2006
    Messages
    2 738
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 2 738
    Par défaut
    Apres de nombreux essais je suis parvenu par l'intermediaire d'un Thread a recuperer le Handle de la boite de dialogue, mais je ne parviens pas a simuler l'appuie du bouton afin de valider la selection, j'ai regarde avec Spy++ mais pour les bouttons je n'ai rien vu
    dans le .h j'ai ajoute
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    class TMonThread : public TThread
    {
    private:
    protected:
            void __fastcall Execute();
    public:
            __fastcall TMonThread(bool CreateSuspended);
    };
    dans le .cpp j'ai ajoute
    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
     
    char sz[256];
    TMonThread *MonThread;
    .........
    MonThread = new TMonThread(false);
    // code d'ouverture de la boite de dialogue
    vWDocument.OlePropertyGet("MailMerge").OleProcedure("OpenDataSource", Chemin.c_str(), 1, true, true, false, false, PasswordDocument, PasswordTemplate, false, WritePasswordDocument, WritePasswordTemplate, Connection.c_str(), SQLStatement, SQLStatement1, false);
    .........
    //---------------------------------------------------------------------------
    __fastcall TMonThread::TMonThread(bool CreateSuspended)
            : TThread(CreateSuspended)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TMonThread::Execute()
    {
            //---- Placer le code du thread ici ----
    HWND hwnd;
    do
    {
    hwnd = FindWindow("bosa_sdm_Microsoft Office Word 11.0", NULL);
    int nRet = GetWindowText(hwnd, sz, 256);
    }
    while(strcmp(sz, "Sélectionner le tableau"));
     // on simule la touche entree pour afficher la boite de dialogue
    PostMessage(hwnd, WM_KEYDOWN, VK_RETURN, 0); // touche enfoncee
    PostMessage(hwnd, WM_KEYUP, VK_RETURN, 0); // touche relachee
    Sleep(50);
    delete MonThread;
    }
    //---------------------------------------------------------------------------
    Si quelqu'un a une idee, la je seche

  7. #7
    Rédacteur
    Avatar de blondelle
    Homme Profil pro
    Inscrit en
    Mars 2006
    Messages
    2 738
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 2 738
    Par défaut
    Je suis parvenu a automatiser completement le Mailling
    le .h
    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
     
    //---------------------------------------------------------------------------
    #ifndef Unit1H
    #define Unit1H
    //---------------------------------------------------------------------------
    #include <Classes.hpp>
    #include <Controls.hpp>
    #include <StdCtrls.hpp>
    #include <Forms.hpp>
    #include <ExtCtrls.hpp>
    //---------------------------------------------------------------------------
    class TMonThread : public TThread
    {
    private:
    protected:
            void __fastcall Execute();
    public:
            __fastcall TMonThread(bool CreateSuspended);
    };
    class TForm1 : public TForm
    {
    __published: // IDE-managed Components
            TButton *Button1;
            TButton *Button2;
            TButton *Button3;
            TButton *Button4;
            void __fastcall Button1Click(TObject *Sender);
            void __fastcall Button3Click(TObject *Sender);
            void __fastcall Button4Click(TObject *Sender);
            void __fastcall Button2Click(TObject *Sender);
    private: // User declarations
    public:  // User declarations
            __fastcall TForm1(TComponent* Owner);
    };
    //---------------------------------------------------------------------------
    extern PACKAGE TForm1 *Form1;
    //---------------------------------------------------------------------------
    #endif
    le .cpp
    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
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
     
    //---------------------------------------------------------------------------
    #include <vcl.h>
    #pragma hdrstop
    #include "Unit1.h"
    #include <utilcls.h>
    //#include<Comobj.hpp>
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    Variant vMSWord, vWDocuments, vWDocument;
    char sz[256];
    TMonThread *MonThread;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
    Button2->Visible = false;
    }
    //---------------------------------------------------------------------------
    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:\\Debut de lettre.doc";
    //vWDocuments = vMSWord.OlePropertyGet("Documents");
    //vWDocument = vWDocuments.OleFunction("Open", vFileName);
    vWDocuments = vMSWord.OlePropertyGet("Documents");
    vWDocument = vWDocuments.OleFunction("Add");
    Button2->Visible = true;
    if (Form1->Visible) // mettre l'appli toujours au premier plan
      {
    Form1->Visible = true;
    Form1->BringToFront();
      }
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Button3Click(TObject *Sender)
    {
        vWDocuments.OleFunction("Close", NULL, NULL);
        vMSWord.OleFunction("Quit");
        // on libere les Variants
        vMSWord = Unassigned;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Button4Click(TObject *Sender)
    {
    Application->Terminate();
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Button2Click(TObject *Sender)
    {
    Variant Address, ReturnAddress, AutoText;
    Address = "";
    ReturnAddress = "";
    AutoText = "";
    // on va effectuer un mailling Enveloppe
    vWDocument.OlePropertyGet("MailMerge").OlePropertySet("MainDocumentType", 2); // wdEnvelopes = 2
    // type d'enveloppe
    vWDocument.OlePropertyGet("Envelope").OlePropertySet("DefaultSize", "DL");
    // initialisation a "" expediteur, destinataire
    vWDocument.OlePropertyGet("Envelope").OleProcedure("Insert", false, WideString(Address), WideString(AutoText), false, WideString(ReturnAddress));
    // parametrage et affichage expediteur
    vMSWord.OlePropertyGet("Selection").OlePropertyGet("Font").OlePropertySet("Size", 8);
    vMSWord.OlePropertyGet("Selection").OlePropertyGet("Font").OlePropertySet("Name", "Arial");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", "blondelle jean-pierre");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeParagraph");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", "chemin xxxx");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeParagraph");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", "quartier xxxx");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeParagraph");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", "30000 xyxyxyxy");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeParagraph");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", "France");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeParagraph");
    // on se positionne sur la fenetre destinataire
    vWDocument.OlePropertyGet("Envelope").OlePropertyGet("Address").OleProcedure("Select");
    vMSWord.OlePropertyGet("Selection").OleProcedure("MoveDown", 5, 2); // wdLine = 5, Count = 2
    // affichage des parametres de fusion
    vWDocument.OlePropertyGet("MailMerge").OlePropertyGet("Fields").OleProcedure("Add", vMSWord.OlePropertyGet("Selection").OlePropertyGet("Range"), "Prenom");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", " ");
    vWDocument.OlePropertyGet("MailMerge").OlePropertyGet("Fields").OleProcedure("Add", vMSWord.OlePropertyGet("Selection").OlePropertyGet("Range"), "Nom");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeParagraph");
    vWDocument.OlePropertyGet("MailMerge").OlePropertyGet("Fields").OleProcedure("Add", vMSWord.OlePropertyGet("Selection").OlePropertyGet("Range"), "ADRESSE");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeParagraph");
    vWDocument.OlePropertyGet("MailMerge").OlePropertyGet("Fields").OleProcedure("Add", vMSWord.OlePropertyGet("Selection").OlePropertyGet("Range"), "CODE_POSTAL");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", " ");
    vWDocument.OlePropertyGet("MailMerge").OlePropertyGet("Fields").OleProcedure("Add", vMSWord.OlePropertyGet("Selection").OlePropertyGet("Range"), "Ville");
    Variant PasswordDocument, PasswordTemplate, WritePasswordDocument, WritePasswordTemplate, SQLStatement, SQLStatement1;
    PasswordDocument = "";
    PasswordTemplate = "";
    WritePasswordDocument = "";
    WritePasswordTemplate = "";
    SQLStatement = "";
    SQLStatement1 = "";
    String Chemin, Connection;
    Chemin = "C:\\tableau.xls";
    Connection = "Feuille de calcul entière";
    // ouverture de la source
    // une boite de dialogue s'affiche
    MonThread = new TMonThread(false);
    vWDocument.OlePropertyGet("MailMerge").OleProcedure("OpenDataSource", Chemin.c_str(), 1, true, true, false, false, PasswordDocument, PasswordTemplate, false, WritePasswordDocument, WritePasswordTemplate, Connection.c_str(), SQLStatement, SQLStatement1, false);
    // parametre de l'adresse Gras, Times New Roman, Size 16
    vWDocument.OlePropertyGet("Envelope").OlePropertyGet("AddressStyle").OlePropertyGet("Font").OlePropertySet("Bold", true);
    vWDocument.OlePropertyGet("Envelope").OlePropertyGet("AddressStyle").OlePropertyGet("Font").OlePropertySet("Name", "Times New Roman");
    vWDocument.OlePropertyGet("Envelope").OlePropertyGet("AddressStyle").OlePropertyGet("Font").OlePropertySet("Size", 16);
    // caracteres en majuscules
    vWDocument.OlePropertyGet("Envelope").OlePropertyGet("AddressStyle").OlePropertyGet("Font").OlePropertySet("AllCaps", 9999998);
    // on fusionne
    vWDocument.OlePropertyGet("MailMerge").OleProcedure("Execute", false);
    }
    //---------------------------------------------------------------------------
    __fastcall TMonThread::TMonThread(bool CreateSuspended)
            : TThread(CreateSuspended)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TMonThread::Execute()
    {
            //---- Placer le code du thread ici ----
    TRect r;
    HWND hwnd;
    do
    {
    hwnd = FindWindow("bosa_sdm_Microsoft Office Word 11.0", NULL);
    int nRet = GetWindowText(hwnd, sz, 256);
    }
    while(strcmp(sz, "Sélectionner le tableau"));
    Sleep(100);
    SetForegroundWindow(hwnd);
    GetWindowRect(hwnd, &r);
    ClipCursor(&r);
    SetCursorPos(r.Right - 130, r.Bottom - 25);
    // on simule l'appuie sur le boutton gauche de la souris pour afficher la boite de dialogue
    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); // boutton gauche enfonce
    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); // boutton gauche relache
    ClipCursor(NULL);
    delete MonThread;
    }
    //---------------------------------------------------------------------------
    ce code fonctionne sur XP Pack2 Office 2003

  8. #8
    Membre éprouvé
    Avatar de Sunchaser
    Homme Profil pro
    OPNI (Objet Programmant Non Identifié)
    Inscrit en
    Décembre 2004
    Messages
    2 059
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : OPNI (Objet Programmant Non Identifié)
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Décembre 2004
    Messages : 2 059
    Par défaut


    Bravo, et ca m'a l'air bien pratique ce Spy++ ...

  9. #9
    Rédacteur
    Avatar de blondelle
    Homme Profil pro
    Inscrit en
    Mars 2006
    Messages
    2 738
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 2 738
    Par défaut
    Oui il y a aussi l'equivalent en BCB, ca te permet en pointant un petit sigle sur une fenetre ou un element d'obtenir le Handle de celui ci, et de verifier si le code renvoie bien ce Handle

  10. #10
    Rédacteur
    Avatar de blondelle
    Homme Profil pro
    Inscrit en
    Mars 2006
    Messages
    2 738
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 2 738
    Par défaut
    Une derniere petite modif qui tient compte de la version de Word, je ne peut pas tester mais je pense que cela fonctionnera sur Word 2000, 2002, 2003
    le .h n'est pas modifie
    le.cpp
    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
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
     
    //---------------------------------------------------------------------------
    #include <vcl.h>
    #pragma hdrstop
    #include "Unit1.h"
    #include <utilcls.h>
    //#include<Comobj.hpp>
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    Variant vMSWord, vWDocuments, vWDocument;
    char sz[256];
    String version_word;
    TMonThread *MonThread;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
    Button2->Visible = false;
    }
    //---------------------------------------------------------------------------
    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:\\Debut de lettre.doc";
    //vWDocuments = vMSWord.OlePropertyGet("Documents");
    //vWDocument = vWDocuments.OleFunction("Open", vFileName);
    vWDocuments = vMSWord.OlePropertyGet("Documents");
    vWDocument = vWDocuments.OleFunction("Add");
    // connaitre le numero de version Word que l'on utilise
    version_word = vMSWord.OlePropertyGet("Version");
    Button2->Visible = true;
    if (Form1->Visible) // mettre l'appli toujours au premier plan
      {
    Form1->Visible = true;
    Form1->BringToFront();
      }
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Button3Click(TObject *Sender)
    {
        vWDocuments.OleFunction("Close", NULL, NULL);
        vMSWord.OleFunction("Quit");
        // on libere les Variants
        vMSWord = Unassigned;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Button4Click(TObject *Sender)
    {
    Application->Terminate();
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Button2Click(TObject *Sender)
    {
    Variant Address, ReturnAddress, AutoText;
    Address = "";
    ReturnAddress = "";
    AutoText = "";
    // on va effectuer un mailling Enveloppe
    vWDocument.OlePropertyGet("MailMerge").OlePropertySet("MainDocumentType", 2); // wdEnvelopes = 2
    // type d'enveloppe
    vWDocument.OlePropertyGet("Envelope").OlePropertySet("DefaultSize", "DL");
    // initialisation a "" expediteur, destinataire
    vWDocument.OlePropertyGet("Envelope").OleProcedure("Insert", false, WideString(Address), WideString(AutoText), false, WideString(ReturnAddress));
    // parametrage et affichage expediteur
    vMSWord.OlePropertyGet("Selection").OlePropertyGet("Font").OlePropertySet("Size", 8);
    vMSWord.OlePropertyGet("Selection").OlePropertyGet("Font").OlePropertySet("Name", "Arial");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", "blondelle jean-pierre");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeParagraph");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", "chemin xxxx");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeParagraph");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", "quartier xxxx");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeParagraph");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", "30000 xyxyxyxy");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeParagraph");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", "France");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeParagraph");
    // on se positionne sur la fenetre destinataire
    vWDocument.OlePropertyGet("Envelope").OlePropertyGet("Address").OleProcedure("Select");
    vMSWord.OlePropertyGet("Selection").OleProcedure("MoveDown", 5, 2); // wdLine = 5, Count = 2
    // affichage des parametres de fusion
    vWDocument.OlePropertyGet("MailMerge").OlePropertyGet("Fields").OleProcedure("Add", vMSWord.OlePropertyGet("Selection").OlePropertyGet("Range"), "Prenom");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", " ");
    vWDocument.OlePropertyGet("MailMerge").OlePropertyGet("Fields").OleProcedure("Add", vMSWord.OlePropertyGet("Selection").OlePropertyGet("Range"), "Nom");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeParagraph");
    vWDocument.OlePropertyGet("MailMerge").OlePropertyGet("Fields").OleProcedure("Add", vMSWord.OlePropertyGet("Selection").OlePropertyGet("Range"), "ADRESSE");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeParagraph");
    vWDocument.OlePropertyGet("MailMerge").OlePropertyGet("Fields").OleProcedure("Add", vMSWord.OlePropertyGet("Selection").OlePropertyGet("Range"), "CODE_POSTAL");
    vMSWord.OlePropertyGet("Selection").OleProcedure("TypeText", " ");
    vWDocument.OlePropertyGet("MailMerge").OlePropertyGet("Fields").OleProcedure("Add", vMSWord.OlePropertyGet("Selection").OlePropertyGet("Range"), "Ville");
    Variant PasswordDocument, PasswordTemplate, WritePasswordDocument, WritePasswordTemplate, SQLStatement, SQLStatement1;
    PasswordDocument = "";
    PasswordTemplate = "";
    WritePasswordDocument = "";
    WritePasswordTemplate = "";
    SQLStatement = "";
    SQLStatement1 = "";
    String Chemin, Connection;
    Chemin = "C:\\tableau.xls";
    Connection = "Feuille de calcul entière";
    // ouverture de la source
    // une boite de dialogue s'affiche
    MonThread = new TMonThread(false);
    vWDocument.OlePropertyGet("MailMerge").OleProcedure("OpenDataSource", Chemin.c_str(), 1, true, true, false, false, PasswordDocument, PasswordTemplate, false, WritePasswordDocument, WritePasswordTemplate, Connection.c_str(), SQLStatement, SQLStatement1, false);
    // parametre de l'adresse Gras, Times New Roman, Size 16
    vWDocument.OlePropertyGet("Envelope").OlePropertyGet("AddressStyle").OlePropertyGet("Font").OlePropertySet("Bold", true);
    vWDocument.OlePropertyGet("Envelope").OlePropertyGet("AddressStyle").OlePropertyGet("Font").OlePropertySet("Name", "Times New Roman");
    vWDocument.OlePropertyGet("Envelope").OlePropertyGet("AddressStyle").OlePropertyGet("Font").OlePropertySet("Size", 16);
    // caracteres en majuscules
    vWDocument.OlePropertyGet("Envelope").OlePropertyGet("AddressStyle").OlePropertyGet("Font").OlePropertySet("AllCaps", 9999998);
    // on fusionne
    vWDocument.OlePropertyGet("MailMerge").OleProcedure("Execute", false);
    }
    //---------------------------------------------------------------------------
    __fastcall TMonThread::TMonThread(bool CreateSuspended)
            : TThread(CreateSuspended)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TMonThread::Execute()
    {
            //---- Placer le code du thread ici ----
    TRect r;
    HWND hwnd;
    String Nom = "bosa_sdm_Microsoft Office Word " + version_word;
    do
    {
    hwnd = FindWindow(Nom.c_str(), NULL);
    GetWindowText(hwnd, sz, 256);
    }
    while(strcmp(sz, "Sélectionner le tableau"));
    Sleep(100);
    SetForegroundWindow(hwnd);
    GetWindowRect(hwnd, &r);
    ClipCursor(&r);
    SetCursorPos(r.Right - 130, r.Bottom - 25);
    // on simule l'appuie sur le boutton gauche de la souris pour afficher
    // la boite de dialogue
    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); // boutton gauche enfonce
    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); // boutton gauche relache
    ClipCursor(NULL);
    delete MonThread;
    }
    //---------------------------------------------------------------------------

Discussions similaires

  1. [XL-2007] adressage enveloppe à partir excel
    Par hrv53 dans le forum Excel
    Réponses: 4
    Dernier message: 19/12/2011, 13h28
  2. Réponses: 9
    Dernier message: 23/12/2008, 23h38
  3. Fusion Publipostage OleWord Excel
    Par blondelle dans le forum C++Builder
    Réponses: 0
    Dernier message: 09/12/2008, 18h32

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo