OLE automatisatie in MS Word

0000002386     -      23-08-2016

Ziehier de gelijkaardige code in 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);
}

Het is noodzakelijk dat er bovenaan using System.Reflection; wordt toegevoegd in de code.

De code is, 100% identiek in de Logic.
 
Het voordeel van reflection te gebruiken in plaats van "Add Reference / Com / Microsoft Word xx.x / library" is dat het een code zal produceren die compatibel is met alle versies van Word en niet gerelateerd is aan de versie van de ontwikkelaar.
 
Het bijgevoegd zip-bestand bevat Lettre.docx, die adequate referenties (datum en adres) meekrijgt om het systeem te laten werken.

Hoe is het mogelijk om deze code in C# te herschrijven?

                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
 
 


Te laden : 0000002386.zip (9 Kb - 26-12-2014)