OLE automation sur MS Word

0000002386     -      23/08/2016

Voici le code équivalent en C# :

Zoom
object ox_word = null;
try
{
    Type type = Type.GetTypeFromProgID("word.basic");
    ox_word = Activator.CreateInstance(type);
    type.InvokeMember("FileOpen", BindingFlags.Default | BindingFlags.InvokeMethod, null, ox_word, new object[1] { @"C:\Test\lettre.docx" });
    type.InvokeMember("ww7_editGoto", BindingFlags.Default | BindingFlags.InvokeMethod | BindingFlags.IgnoreReturn, null, ox_word, new object[1] { "Date" });
    type.InvokeMember("insert", BindingFlags.Default | BindingFlags.InvokeMethod | BindingFlags.IgnoreReturn, null, ox_word, new object[1] { "28 novembre 2014" });
    type.InvokeMember("ww7_editGoto", BindingFlags.Default | BindingFlags.InvokeMethod | BindingFlags.IgnoreReturn, null, ox_word, new object[1] { "Adresse" });
    type.InvokeMember("insert", BindingFlags.Default | BindingFlags.InvokeMethod | BindingFlags.IgnoreReturn, null, ox_word, new object[1] { "Nom" });
    type.InvokeMember("insertPara", BindingFlags.Default | BindingFlags.InvokeMethod | BindingFlags.IgnoreReturn, null, ox_word, new object[0]);
    type.InvokeMember("insert", BindingFlags.Default | BindingFlags.InvokeMethod | BindingFlags.IgnoreReturn, null, ox_word, new object[1] { "Adresse" });
    type.InvokeMember("insertPara", BindingFlags.Default | BindingFlags.InvokeMethod | BindingFlags.IgnoreReturn, null, ox_word, new object[0]);
    type.InvokeMember("insert", BindingFlags.Default | BindingFlags.InvokeMethod | BindingFlags.IgnoreReturn, null, ox_word, new object[1] { "Adresse 2" });
    type.InvokeMember("insertPara", BindingFlags.Default | BindingFlags.InvokeMethod | BindingFlags.IgnoreReturn, null, ox_word, new object[0]);
    type.InvokeMember("insert", BindingFlags.Default | BindingFlags.InvokeMethod | BindingFlags.IgnoreReturn, null, ox_word, new object[1] { "CodeP Ville" });
    type.InvokeMember("insertPara", BindingFlags.Default | BindingFlags.InvokeMethod | BindingFlags.IgnoreReturn, null, ox_word, new object[0]);
    type.InvokeMember("AppShow", BindingFlags.Default | BindingFlags.InvokeMethod | BindingFlags.IgnoreReturn, null, ox_word, new object[0]);
}
catch (Exception ex)
{
    MercatorUi.Dialogs.Stop(ex.Message + (ex.InnerException != null ? "\r\n" + ex.InnerException.Message : ""));
}
finally
{
    if (ox_word != null)
        System.Runtime.InteropServices.Marshal.ReleaseComObject(ox_word);
}

Il est nécessaire d'ajouter using System.Reflection; dans haut du code.

Le code est, dans la logique, 100% identique.
 
L’avantage d’utiliser la réflexion plutôt que de faire « Ajouter Référence / COM /  Microsoft Word xx.x type library » est que l’on produit un code qui est compatible avec toutes les versions de Word et non pas lié à la version utilisée par le programmeur pour son développement.
 
Le fichier zip ci-joint contient Lettre.docx qui reprend les signets adéquats (Date et Adresse) pour que le système fonctionne.

Comment est-il possible de réécrire le code suivant en C# :

                ox_word=createobject('word.basic')
                ox_word.fileOpen(l_action.dir+'\lettre.doc')
                ox_word.ww7_editGoto('Date')
                ox_word.insert(xlongdate(date(),users.langue))
                ox_word.ww7_editGoto('Adresse')
                ox_word.insert(alltrim(nom))
                ox_word.insertPara
                ox_word.insert(alltrim(adresse))
                ox_word.insertPara
                ox_word.insert(alltrim(adresse2))
                ox_word.insertPara
                ox_word.insert(alltrim(codep)+'   '+alltrim(ville))
                ox_word.insertPara
                ox_word.appShow
                release ox_word
 
 


A télécharger : 0000002386.zip (9 Kb - 26/12/2014)