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
|
// Initialisation des pointeurs vers le document
// et vers les propriétés du document
QAxObject *p_word = new QAxObject( "Word.Application");
QAxObject *documents = p_word->querySubObject("Documents");
QAxObject *doc = documents->querySubObject( "Open(const QString&, bool, bool)", document, false, false);
QAxObject *customProperties = doc->querySubObject( "CustomDocumentProperties");
QAxObject *customProperty;
// Modification des propriétés du document
customProperty = customProperties->querySubObject( "Item(String&)",
"nom_auteur");
customProperty->dynamicCall( "value", "toto");
// Sauvegarde du document modifié dans un document temporaire.
doc->dynamicCall("saveAs2( QString&, int )"," temporaire.doc", 0);
// Mise à jour des champs du document dans le corps du doc
QAxObject *fields = doc->querySubObject("fields");
fields->dynamicCall("update()");
// Mise à jour des champs du document dans les entêtes
QAxObject *sections = doc->querySubObject("sections");
QVariant nombreSection = sections->dynamicCall("count()");
int nbSection = nombreSection.toInt();
QAxObject *section;
QAxObject *headers;
QAxObject *header;
QVariant nombreHeader;
int nbHeader;
QAxObject *range;
// Parcours de toutes les entêtes
for(int i=1; i<nbSection+1;i++){
section = sections->querySubObject("Item(int&)",i);
headers = section->querySubObject("headers");
nombreHeader = headers->dynamicCall("count()");
nbHeader = nombreHeader.toInt();
for(int j=1; j<nbHeader+1;j++)
{
header = headers->querySubObject("Item(int&)",i);
range = header->querySubObject("Range");
fields = range->querySubObject("Fields");
fields->dynamicCall("Update()");
}
}
// Fermeture du document
documents->dynamicCall("close(int, int)", -1, 1);
// Fermeture de Word
p_word->dynamicCall( "quit( )");
delete p_word;
// Suppression du document squelette original
if( !QFile::remove (document) ) return false;
// Copie du document modifié dans le bon répertoire
if( !QFile::copy(" temporaire.doc", document) ) return false;
// Suppression du document temporaire
if( !QFile::remove (" temporaire.doc") ) return false;
return true; |
Partager