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;
}
//--------------------------------------------------------------------------- |
Partager