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 :

Comment créer un document Word avec C++ Builder ?


Sujet :

C++Builder

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    9
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2006
    Messages : 9
    Points : 6
    Points
    6
    Par défaut Comment créer un document Word avec C++ Builder ?
    Bonjour à tous,

    Je n'arrive pas à écrire du texte dans un document Word...
    J'ai pourtant suivi "à la lettre" (en adaptant un peu bien sur)
    Ce qui est sur cette page :
    http://lfe.developpez.com/Word/

    Malheureusement, çà fonctionne pas...

    Quelqu'un peut m'aider ?

    D'avance Merci

    au fait, quand j'ajoute du texte via mon script, voilà ce qui s'affiche :
    Images attachées Images attachées  

  2. #2
    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
    Points : 3 766
    Points
    3 766
    Par défaut
    peut etre en executant ce petit bout de programme avec une forme et un bouton.
    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
     
    //---------------------------------------------------------------------------
    #include <vcl.h>
    #pragma hdrstop
    #pragma link "Word_2K_SRVR"
    #include "Unit1.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
    }
    //----------------------------------------------
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    //----------------------------------------------
    Variant vMSWord , vWDocument , vWDocuments;
    Variant vFileName;
    //----------------------------------------------
    vMSWord = Variant::CreateObject("Word.Application");
    vMSWord.OlePropertySet("Visible",true);
    vFileName = "C:\\Documents and Settings\\blondelle\\Mes documents\\plan_gps\\PLAN_GPS\\télégraphe.csv";
    vWDocuments = vMSWord.OlePropertyGet("Documents");
    vWDocument = vWDocuments.OleFunction("Open", vFileName);
    }
    //-----------------------------------------------

  3. #3
    Futur Membre du Club
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    9
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2006
    Messages : 9
    Points : 6
    Points
    6
    Par défaut
    Oui mais là tu fais seulement l'ouverture de Word. (et çà, çà fonctionne aussi chez moi) Mais pour écrire du texte via mon prog, comment faire ?

  4. #4
    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
    Points : 3 766
    Points
    3 766
    Par défaut
    un qui je pense te sera utile.

  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
    Points : 3 766
    Points
    3 766
    Par défaut
    Excuse moi je n'avais pas vu que tu connaissait le liens, j'ai un exemple mais il est en allemand, et la je ne peu pas le traduire, je pense qu'il est complet avec ouverture fermeture impression sauvegarde

  6. #6
    Membre du Club
    Profil pro
    Étudiant
    Inscrit en
    Juin 2006
    Messages
    92
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juin 2006
    Messages : 92
    Points : 61
    Points
    61
    Par défaut
    Salut !

    blondelle, si tu pouvais mettre en ligne ton exemple de code (même si c'est en allemand, c'est pas grave, ce qui compte c'est le code), ça serait cool.
    Pour ma part, j'avais déjà posté une discussion "Exemple de code pour l'automation de Word 2003", donc motte82 ce serait cool de ne pas poster deux fois les mêmes trucs quand on demande la même chose

    Par avance merci blondelle, car les sources sur ce sujet sont rares (on trouve plutôt sur Excel...).
    ++

  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
    Points : 3 766
    Points
    3 766
    Par défaut
    Je met le code avec les commentaires.
    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
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
     
     
    	C++ Builder Snippets 	
     
     
    MS-Word Dokument basierend auf einer Vorlage erzeugen, ausfüllen, speichern, drucken:
     
    Die Frage "wie kann ich die Word-Vorlagen für Protokolle, Berichte oder Serienbriefe aus meinem Programm heraus mit Daten füttern und anschließend ausdrucken oder speichern ? " gehört inzwischen zu den FAQs in jedem Programmierforum. 
     
    Eine solche Funktionalität hat mehrere Vorteile: sie lässt sich verhältnismäßig einfach in ein Programm einbinden, erspart eigene (oft programmierintensive) Ausdrucksroutinen, generiert auf vielen Systemen lesbare Dateien und bietet dem Anwender eine hohe Flexibilität bei der Gestaltung des Dokuments.
     
    Das nachfolgende Beispiel zeigt, wie MS-Word via OLE-Schittstelle gesteuert werden kann. Word muss auf Ihrem System natürlich installiert sein. Das Programm initialisiert zunächst den Word OLE-Server, erzeugt ein neues Dokument basierend auf der zuvor erstellten Vorlage (Datei "Vorlage.doc" im Quellcode-Archiv), springt der Reihe nach alle in der Vorlage definierte Textmarken an und trägt die Inhalte der entsprechenden Eingabefeldern an den Textmarkenpositionen ein. Anschließend wird das Dokument gespeichert oder gedruckt und Word beendet (nur wenn das Programm nicht schon vorher aktiv war).
     
    Gespeichert wird das generierte Dokument in der Datei "Brief.doc" im Programmverzeichnis. 
     
    Hinweis zum Erstellen der Word-Vorlage: die Textmarken werden mit Hilfe des Menüpunktes " Einfügen > Textmarke" definiert bzw. gelöscht. Vor dem Setzen der Textmarke muss der aus ihrem Programm heraus zu überschreibende Textabschnitt selektiert sein !
     
    Durch die Verwendung der späten Bindung (zur Programmlaufzeit) ist der Code mit allen Word-Versionen ab 95 lauffähig, es müssen keine Type Libraries eingebunden werden. Das Beispielprogramm wurde mit Word 97, 2000 und 2002 (Office XP) getestet. 
     
     
    Programm-Quellcode: 
     
    Header-Datei FormMain.h zum Hauptformular der Anwendung: 
    //---------------------------------------------------------------------------
    #ifndef FormMainH
    #define FormMainH
    //---------------------------------------------------------------------------
    #include <Classes.hpp>
    #include <Controls.hpp>
    #include <StdCtrls.hpp>
    #include <Forms.hpp>
    #include <ComObj.hpp>
    #include <utilcls.h>
     
    // Definition der Variablen für die
    // Übergabe an "GoTo"-Funktion
    typedef enum WdGoToItem
    {
      wdGoToBookmark = 0xFFFFFFFF,
      wdGoToSection = 0,
      wdGoToPage = 1,
      wdGoToTable = 2,
      wdGoToLine = 3,
      wdGoToFootnote = 4,
      wdGoToEndnote = 5,
      wdGoToComment = 6,
      wdGoToField = 7,
      wdGoToGraphic = 8,
      wdGoToObject = 9,
      wdGoToEquation = 10,
      wdGoToHeading = 11,
      wdGoToPercent = 12,
      wdGoToSpellingError = 13,
      wdGoToGrammaticalError = 14,
      wdGoToProofreadingError = 15
    } WdGoToItem;
     
    //---------------------------------------------------------------------------
    // Hauptfenster der Anwendung
    //---------------------------------------------------------------------------
    class TfrmMain : public TForm
    {
    __published:  // Von der IDE verwaltete Komponenten
      TButton *btnCreateAndSaveDoc;
      TEdit *editAnrede;
      TLabel *Label1;
      TEdit *editEmpfaenger;
      TLabel *Label2;
      TEdit *editAnschrift;
      TLabel *Label3;
      TLabel *Label4;
      TEdit *editPLZ_Ort;
      TLabel *lblBetreff;
      TEdit *editBetreff;
      TMemo *memoText;
      TLabel *Label6;
      TLabel *Label7;
      TEdit *editErsteller;
      TButton *btnCreateAndPrintDoc;
      void __fastcall btnCreateAndSaveDocClick(TObject *Sender);
      void __fastcall btnCreateAndPrintDocClick(TObject *Sender);
     
    private:  // Anwender-Deklarationen
     
      // Initialisierung des Word OLE-Servers
      bool __fastcall InitOleServer(Variant &vOLEWord);
     
      // Word-Dokument erzeugen, ggf. speichern und/oder drucken
      void __fastcall CreateDocument(Variant &vOLEWord,
        AnsiString slTemplateFile, AnsiString slSaveFile, bool blPrint);
     
      // Beenden des Word OLE-Servers
      void __fastcall TerminateOleServer(Variant &vOLEWord);
     
    public:    // Anwender-Deklarationen
      __fastcall TfrmMain(TComponent* Owner);
    };
    //---------------------------------------------------------------------------
    extern PACKAGE TfrmMain *frmMain;
    //---------------------------------------------------------------------------
    #endif
    //---------------------------------------------------------------------------
    // Hauptfenster der WordControl-Beispielanwendung
    // http://rad.bytesandmore.de, Serge Kreutzmann, 2002
    //---------------------------------------------------------------------------
    #include <vcl.h>
    #pragma hdrstop
    #include "FormMain.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TfrmMain *frmMain;
    //---------------------------------------------------------------------------
    __fastcall TfrmMain::TfrmMain(TComponent* Owner)
      : TForm(Owner)
    {
    }
     
    //---------------------------------------------------------------------------
    // Initialisierung des Word OLE-Servers
    //---------------------------------------------------------------------------
    bool __fastcall TfrmMain::InitOleServer(Variant &vOLEWord)
    {
      vOLEWord = Unassigned;
      try
      {
        // vielleicht läuft Word ja bereits (wenn nicht, wird hier
        // eine Exception generiert - aber die ist ja behandelt :)
        vOLEWord = GetActiveOleObject("Word.Application");
        return false;
      }
      catch(...)
      {
         try
         {
           // vielleicht läuft Word ja bereits...
           vOLEWord = CreateOleObject("Word.Application");
           return true;
         }
         catch(Exception& e)
         {
            // Wahrscheinlich kein Word auf dem Rechner...
            Application->MessageBox(e.Message.c_str(),"OLE-Error",MB_ICONERROR);
            vOLEWord = Unassigned;
         }
      }
      return true;
    }
     
    //---------------------------------------------------------------------------
    // Beenden des Word OLE-Servers
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::TerminateOleServer(Variant &vOLEWord)
    {
      // Word OLE-Server beenden:
      if(!vOLEWord.IsEmpty())
      {
        vOLEWord.OlePropertyGet("Application").OleProcedure("Quit");
        vOLEWord = Unassigned;
      }
    }
     
    //---------------------------------------------------------------------------
    // Word-Dokument erzeugen, ggf. speichern und/oder drucken
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::CreateDocument(Variant &vOLEWord,
      AnsiString slTemplateFile, AnsiString slSaveFile, bool blPrint)
    {
      try
      {
        // falls Word-Fenster sichtbar sein soll,
        // muss folgende Zeile aktiviert werden:
        // vOLEWord.OlePropertySet("Visible", true);
     
        // Falls die Vorlagen-Datei nicht existiert, schimpfen und beenden:
        if(!FileExists(slTemplateFile))
        {
          Application->MessageBox("Vorlagendatei nicht gefunden !",
            "Abbruch",MB_ICONERROR);
          return;
        }
     
        // Referenz auf die "Documents"-Eigenschaft besorgen
        Variant vDokuments = vOLEWord.OlePropertyGet("Documents");
        // neues Dokument mit slTemplateFile als Dokumentenvorlage erzeugen:
        vDokuments.Exec(Procedure("Add") << slTemplateFile << 0);
     
        //--------------------------------------------------
        // Formular-Felder ausfüllen
        //--------------------------------------------------
     
        // Referenz auf die "Selection"-Eigenschaft besorgen
        Variant vSelection = vOLEWord.OlePropertyGet("Selection");
     
        // Zu Textmarke "Empfaenger" gehen:
        vSelection.Exec(Procedure("GoTo") << (int)wdGoToBookmark <<
           0 << 0 << "Empfaenger");
        // den Text aus dem Eingabefeld eintragen:
        vSelection.OleProcedure("TypeText",(VARIANT)
          ((Variant)editEmpfaenger->Text.c_str()));
     
        // Zu Textmarke "Anschrift" gehen:
        vSelection.Exec(Procedure("GoTo") << (int)wdGoToBookmark <<
           0 << 0 << "Anschrift");
        // den Text aus dem Eingabefeld eintragen:
        vSelection.OleProcedure("TypeText",(VARIANT)
          ((Variant)editAnschrift->Text.c_str()));
     
        // Zu Textmarke "PLZ_Ort" gehen:
        vSelection.Exec(Procedure("GoTo") << (int)wdGoToBookmark <<
           0 << 0 << "PLZ_Ort");
        // den Text aus dem Eingabefeld eintragen:
        vSelection.OleProcedure("TypeText",(VARIANT)
          ((Variant)editPLZ_Ort->Text.c_str()));
     
        // Zu Textmarke "Betreff" gehen:
        vSelection.Exec(Procedure("GoTo") << (int)wdGoToBookmark <<
           0 << 0 << "Betreff");
        // den Text aus dem Eingabefeld eintragen:
        vSelection.OleProcedure("TypeText",(VARIANT)
         ((Variant)editBetreff->Text.c_str()));
     
        // Zu Textmarke "Anrede" gehen:
        vSelection.Exec(Procedure("GoTo") << (int)wdGoToBookmark <<
           0 << 0 << "Anrede");
        // den Text aus dem Eingabefeld eintragen:
        vSelection.OleProcedure("TypeText",(VARIANT)
          ((Variant)editAnrede->Text.c_str()));
     
        // Zu Textmarke "Text" gehen:
        vSelection.Exec(Procedure("GoTo") << (int)wdGoToBookmark <<
           0 << 0 << "Text");
        // den Text aus dem Eingabefeld eintragen:
        vSelection.OleProcedure("TypeText",(VARIANT)
          ((Variant)memoText->Lines->Text.c_str()));
     
        // Zu Textmarke "Ersteller" gehen:
        vSelection.Exec(Procedure("GoTo") << (int)wdGoToBookmark <<
           0 << 0 << "Ersteller");
        // den Text aus dem Eingabefeld eintragen:
        vSelection.OleProcedure("TypeText",(VARIANT)
          ((Variant)editErsteller->Text.c_str()));
     
        // ggf. drucken:
        if(blPrint)
          vOLEWord.OlePropertyGet("Application").OleProcedure("PrintOut");
     
        // ggf. speichern:
        Variant vActiveDocument = vOLEWord.OlePropertyGet("ActiveDocument");
        if(slSaveFile != EmptyStr)
          vActiveDocument.Exec(Procedure("SaveAs") << slSaveFile << 0 << 0
            << "" << 1 << "" << 0 << 0 << 0 << 0 << 0 );
     
        // "gespeichert"-Flag in Word setzen, damit keine Nachfragen kommen
        vActiveDocument.OlePropertySet("Saved", true);
     
        // Datei schließen:
        vActiveDocument.Exec(Procedure("Close"));
      }
      catch(Exception& e)
      {
        Application->MessageBox( e.Message.c_str(),"OLE-Error",MB_ICONERROR);
      }
    }
     
    //---------------------------------------------------------------------------
    // Dokument erzeugen und speichern
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::btnCreateAndSaveDocClick(TObject *Sender)
    {
      // OLE-Server initialisieren, die Variable blWordStarted
      // wird auf true gesetzt falls Word nicht bereits aktiv war
      Variant vOLEWord;
      bool blWordStarted = InitOleServer(vOLEWord);
     
      // Falls die Initialisierung des OLE-Servers erfolgreich,
      // Dokument erzeugen und speichern:
      if(!vOLEWord.IsEmpty())
        CreateDocument(vOLEWord, ExtractFilePath(ParamStr(0))+"vorlage.doc",
          ExtractFilePath(ParamStr(0))+"Brief.doc", false);
     
      // Word nur beenden, wenn es von uns zuvor gestartet wurde:
      if(blWordStarted) TerminateOleServer(vOLEWord);
    }
     
    //---------------------------------------------------------------------------
    // Dokument erzeugen und drucken
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::btnCreateAndPrintDocClick(TObject *Sender)
    {
      // OLE-Server initialisieren, die Variable blWordStarted
      // wird auf true gesetzt falls Word nicht bereits aktiv war
      Variant vOLEWord;
      bool blWordStarted = InitOleServer(vOLEWord);
     
      // Falls die Initialisierung des OLE-Servers erfolgreich,
      // Dokument erzeugen und drucken:
      if(!vOLEWord.IsEmpty())
        CreateDocument(vOLEWord, ExtractFilePath(ParamStr(0))+"vorlage.doc",
          EmptyStr, true);
      // Word nur beenden, wenn es von uns zuvor gestartet wurde:
      if(blWordStarted) TerminateOleServer(vOLEWord);
    }
    //---------------------------------------------------------------------------
    ________________________________________
     
    Download BCB5
    Projekt-Quellcode 
     
    Download 
    Demo-Exe 
     
    ________________________________________
    © '99-2002 by S. Kreutzmann 
    WERBUNG:
    www.ebay.de
    eBay ist der weltweit groesste Online-Marktplatz auf dem jeder praktisch alles kaufen und verkaufen kann! Handeln Sie mit ueber 65 Millionen Menschen weltweit! Stoebern Sie in ueber 10 Millionen Angeboten! eBay.de bietet Ihnen mit über 1,5 Million Artikeln das Beste aus deutschen und internationalen Angeboten.
    bon courage si tu peu en tirer quelque chose fais en part.

  8. #8
    Membre du Club
    Profil pro
    Étudiant
    Inscrit en
    Juin 2006
    Messages
    92
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juin 2006
    Messages : 92
    Points : 61
    Points
    61
    Par défaut
    merci blondelle pour le code. J'avoue que c'est un peu bizarre...
    J'ai un exemple de code que j'avais fait en cours mais c'était sous Microsoft Visual C++ 6.0.
    Je le met ici car peut-être que quelqu'un pourra l'adapter pour C++ Builder

    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
     
    #include <windows.h>
    #include <stdio.h>
     
    #import "C:\Program Files\Fichiers communs\Microsoft Shared\OFFICE11\MSO.DLL"
    using namespace Office;
    #import "C:\Program Files\Fichiers communs\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB"
    using namespace VBIDE;
    #undef ExitWindows
    #import "C:\Program Files\Microsoft Office\OFFICE11\MSWORD.OLB"
    using namespace Word;
     
    void AddText(TablePtr& tab, int row, int col, const char* text)
    {
    	tab->Cell(row,col)->Range->InsertAfter(text);
    }
     
    void main()
    {
    	::CoInitialize(0);
    	{
    		//Création d'une apllication Word
    		_ApplicationPtr app("Word.Application");
    		app->Visible=VARIANT_TRUE;
     
    		//Création d'un nouveau document
    		_DocumentPtr doc=app->Documents->Add();
     
    		//Création d'un nouveau paragraphe
    		ParagraphPtr para=doc->Paragraphs->Item(1);
    		para->Range->InsertAfter("Informations système\n");
    		doc->Paragraphs->Item(1)->Alignment=wdAlignParagraphCenter;
     
    		//Création d'un tableau
    		TablePtr tab=doc->Tables->Add(doc->Paragraphs->Item(2)->Range,6,2);
     
    		//Remplissage du tableau
    		AddText(tab,1,1,"Nom ordinateur");
    		AddText(tab,2,1,"Nom utilisateur");
    		AddText(tab,3,1,"Version systeme");
    		AddText(tab,4,1,"Service Pack");
    		AddText(tab,5,1,"Repertoire systeme");
    		AddText(tab,6,1,"Taille ecran");
     
    		char buffer[MAX_PATH];
    		DWORD StrLen;
     
    		//Nom ordinateur
    		StrLen = MAX_COMPUTERNAME_LENGTH;
    		GetComputerName(buffer,&StrLen); 
    		AddText(tab,1,2,buffer);
     
    		//Nom utilisateur
    		GetUserName(buffer,&StrLen);
    		AddText(tab,2,2,buffer);	
     
    		//Service Pack
    		OSVERSIONINFO osInfo;
    		osInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
    		GetVersionEx(&osInfo);
    		AddText(tab,4,2,osInfo.szCSDVersion);
     
    		//Version Système
    		wsprintf(buffer,"%lu",osInfo.dwMajorVersion);
    		AddText(tab,3,2,buffer);
     
    		//Répertoire Système
    		GetSystemDirectory(buffer,255);
    		AddText(tab,5,2,buffer);
     
    		//Taille écran
    		int x,y;
    		x=GetSystemMetrics(SM_CXSCREEN);
    		y=GetSystemMetrics(SM_CYSCREEN);
    		wsprintf(buffer,"%d * %d",x,y);
    		AddText(tab,6,2,buffer);
     
    		tab->Range->Bold = VARIANT_TRUE;
    		tab->Range->Font->Name = "Arial";
    		tab->Range->Font->Color = wdColorBlue;
    		tab->Range->Font->Italic = VARIANT_TRUE;
     
    		//Sauvegarde du document
    		doc->SaveAs(&variant_t("Z:\\Programmation Avancé\\TP2\\infos.doc"));
    		Sleep(3000);
     
    		//Fermeture de l'application
    		app->Quit();
    	}
     
    	::CoUninitialize();
    }

  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
    Points : 3 766
    Points
    3 766
    Par défaut
    Je pense qu'une traduction des commentaires aiderait a comprendre le code.
    Je pense que pour readapter ton code il faut un meilleur programmeur que moi. Mais merci quand meme on ne sais jamais cela pourrait inspirer quelqu'un.

  10. #10
    Futur Membre du Club
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    9
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2006
    Messages : 9
    Points : 6
    Points
    6
    Par défaut
    Citation Envoyé par BigMike
    Salut !

    Pour ma part, j'avais déjà posté une discussion "Exemple de code pour l'automation de Word 2003", donc motte82 ce serait cool de ne pas poster deux fois les mêmes trucs quand on demande la même chose

    ++
    Excuse moi... J'ai cherché mais apparemment pas assez bien...


    Sinon, j'ai essayé d'adapté le code (en allemand) dans mon prog et........
    çà marche toujours pas...

    Je commence à desesperer...

  11. #11
    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
    Points : 3 766
    Points
    3 766
    Par défaut
    Le message d'erreur que tu a decrit dans ton premier post est du a une mauvaise declaration de variable elle ne sont pas public.
    J'ai fait un test simple qui marche chez moi.
    dans 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
    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #pragma hdrstop
    #pragma link "Word_2K_SRVR"
    
    #include "Open_Word.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    Variant vMSWord , vWDocument , vWDocuments;
    Variant vFileName;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
    //Variant vMSWord , vWDocument , vWDocuments;
    }
    //----------------------------------------------
    
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    //----------------------------------------------
    //Variant vMSWord , vWDocument , vWDocuments;
    Variant vFileName;
    //----------------------------------------------
    vMSWord = Variant::CreateObject("Word.Application");
    vMSWord.OlePropertySet("Visible",true);
    
    vFileName = "C:\\Documents and Settings\\blondelle\\Mes documents\\plan_gps\\PLAN_GPS\\télégraphe.csv";
    vWDocuments = vMSWord.OlePropertyGet("Documents");
    vWDocument = vWDocuments.OleFunction("Open", vFileName);
    }
    //-----------------------------------------------
    void __fastcall TForm1::Button2Click(TObject *Sender)
    {
    vMSWord.OleFunction("Quit");
    vMSWord = Unassigned;
    }
    //---------------------------------------------------------------------------
    dans 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
     
    //---------------------------------------------------------------------------
     
    #ifndef Open_WordH
    #define Open_WordH
    //---------------------------------------------------------------------------
    #include <Classes.hpp>
    #include <Controls.hpp>
    #include <StdCtrls.hpp>
    #include <Forms.hpp>
    //---------------------------------------------------------------------------
    class TForm1 : public TForm
    {
    __published:	// IDE-managed Components
            TButton *Button1;
            TButton *Button2;
            void __fastcall Button1Click(TObject *Sender);
            void __fastcall Button2Click(TObject *Sender);
    private:	// User declarations
    public:		// User declarations
            __fastcall TForm1(TComponent* Owner);
    };
    //---------------------------------------------------------------------------
    extern PACKAGE TForm1 *Form1;
    //---------------------------------------------------------------------------
    #endif
    il te faut juste changer le chemin du fichier en rouge il te faut une forme et deux boutons

  12. #12
    Membre du Club
    Profil pro
    Étudiant
    Inscrit en
    Juin 2006
    Messages
    92
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juin 2006
    Messages : 92
    Points : 61
    Points
    61
    Par défaut
    Salut !

    J'ai rajouté les mêmes lignes que toi blondelle mais je n'ai pas le fichier Open_Word.h
    Sinon, est ce que t'as réussis à insérer du texte ?

  13. #13
    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
    Points : 3 766
    Points
    3 766
    Par défaut
    BigMike a ecrit
    Salut !

    J'ai rajouté les mêmes lignes que toi blondelle mais je n'ai pas le fichier Open_Word.h
    Sinon, est ce que t'as réussis à insérer du texte ?
    1 - Il n'y a pas de fichier Open_Word.h c'est le .h de la form que tu utilise quand j'ai sauvegarde mon programme je lui ai donne ce nom.
    2 - J'ai reussi a inserer du texte mais on ne peu le faire qu'une seule fois apres on a un erreur.

    Le code .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
     
    //---------------------------------------------------------------------------
     
    #ifndef Open_WordH
    #define Open_WordH
    //---------------------------------------------------------------------------
    #include <Classes.hpp>
    #include <Controls.hpp>
    #include <StdCtrls.hpp>
    #include <Forms.hpp>
    //---------------------------------------------------------------------------
    class TForm1 : public TForm
    {
    __published:	// IDE-managed Components
            TButton *Button1;
            TButton *Button2;
            TMemo *Memo1;
            TButton *Button3;
            void __fastcall Button1Click(TObject *Sender);
            void __fastcall Button2Click(TObject *Sender);
            void __fastcall Button3Click(TObject *Sender);
     
    private:	// User declarations
    public:		// User declarations
            __fastcall TForm1(TComponent* Owner);
    };
    //---------------------------------------------------------------------------
    extern PACKAGE TForm1 *Form1;
    //---------------------------------------------------------------------------
    #endif
    Le code .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
     
    //---------------------------------------------------------------------------
     
    #include <vcl.h>
    #pragma hdrstop
    #pragma link "Word_2K_SRVR"
    #include <utilcls.h>
    #include "Open_Word.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    // declaration des variables public
    Variant vMSWord , vWDocument , vWDocuments;
    Variant vFileName, vContent, Sel, TypeText;
    //String texte;
     
    PropertyGet GetSel("Selection");
    Procedure insert("InsertAfter");
    Procedure AddText("TypeText");
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
    }
     
    //----------------------------------------------
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
     
    try
    {
        vMSWord = Variant::GetActiveObject("Word.Application");
    } catch(...) {
        vMSWord = Variant::CreateObject("Word.Application");
    }
     
    //vMSWord = Variant::CreateObject("Word.Application");
    vMSWord.OlePropertySet("Visible",true);
     
    vFileName = "C:\\Documents and Settings\\blondelle\\Mes documents\\plan_gps\\PLAN_GPS\\télégraphe.csv";
    vWDocuments = vMSWord.OlePropertyGet("Documents");
    vWDocument = vWDocuments.OleFunction("Open", vFileName);
    Sel = vMSWord.Exec(GetSel);
    }
     
    //-----------------------------------------------
    void __fastcall TForm1::Button2Click(TObject *Sender)
    {
    vMSWord.OleFunction("Quit");
    vMSWord = Unassigned;
    Application->Terminate();
    }
     
     
    void __fastcall TForm1::Button3Click(TObject *Sender)
    {
    //Variant TypeText;
    TypeText = (Memo1->Lines->Text); // soit chaine recuperee dans un TMemo
     
    //Variant Sel = vMSWord.Exec(GetSel);
    //AddText.ClearArgs();
     
    //TypeText = "insertion de texte "; // soit chaine a afficher
    Sel.Exec(insert << TypeText);
    }
    //---------------------------------------------------------------------------
    Je me suis appercu quand faisant F1 sur un mot de commande pae exemple "OlePropertyGet" il y avait quelques explications (borland est avare d'exemples)
    Je n'ai pas trouve d'autre facon de faire mais je pense que ce n'est pas la seule.
    Il y a des declarations de procedures que je ne connais pas mais ca marche.

  14. #14
    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
    Points : 3 766
    Points
    3 766
    Par défaut
    J'ai trouve pour inserer plusieur fois du texte
    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
     
    void __fastcall TForm1::Button3Click(TObject *Sender)
    {
    //Variant TypeText;
    TypeText = (Memo1->Lines->Text);
     
    //Variant Sel = vMSWord.Exec(GetSel);
    AddText.ClearArgs();
     
    //TypeText = "insertion de texte ";
    Sel.Exec(AddText << TypeText);
    AddText.ClearArgs();
    }

  15. #15
    Futur Membre du Club
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    9
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2006
    Messages : 9
    Points : 6
    Points
    6
    Par défaut
    Ok çà marche merci beaucoup

    en fait, j'utilisais çà pour essayer d'entrer du texte :

    vContent.OleFunction("InsertAfter", "TEST");

    alor qu'avec vos fonctions, çà va vraiment mieux.

    Merci encore

    @+

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Créer un document Word avec ASP.Net
    Par Coroebus dans le forum ASP.NET
    Réponses: 1
    Dernier message: 02/03/2013, 13h44
  2. Créer un document Word avec Flex
    Par trotters213 dans le forum Flex
    Réponses: 3
    Dernier message: 15/05/2008, 10h07
  3. Réponses: 3
    Dernier message: 03/10/2007, 09h12
  4. [Word] Comment ouvrir un document word avec PHP ?
    Par jojoquiasa dans le forum Bibliothèques et frameworks
    Réponses: 4
    Dernier message: 17/07/2007, 16h10
  5. Réponses: 1
    Dernier message: 19/06/2007, 12h43

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