Een Word-document genereren op basis van de gegevens van de actieve klantenfiche

0000001992     -      26-08-2017

Via onderstaande voorbeelden  (Word 2007 ou alle Word versies) kan Microsoft Word automatisch de naam en het adres van de klant invoeren in een standaarddocument. Deze gegevens worden ingevoerd in een bestaande bladwijzer (bookmark) die de naam "Adres" krijgt. De code dient in een knop te worden geplaatst die in het informatiebestand "Klanten" zal worden geplaatst. Het oorspronkelijke Word-bestand (lettre.docx) moet in de subdirectory Files van de gegevensdirectory van Mercator worden geplaatst. U vindt dit bestand in de zip van deze pagina.

De module zal een nieuw docx-bestand genereren dat in het object "Bestandsbrowser" van deze klantenfiche wordt geplaatst.

Zoom
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MercatorApi;
using MercatorController;
// <ReferenceInclude>"C:\Windows\assembly\GAC\Microsoft.Office.Interop.Word\12.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Word.dll"</ReferenceInclude>

namespace MercatorUi.MovableControls.ButtonsCodes
{
    public static class Script
    {

        public static void Exec(MercatorUi.MovableControls.MovableButton clickedButton)
        {
            // enter your customized code here
            MercatorUi.Forms.Sig.SigForm sigForm = (MercatorUi.Forms.Sig.SigForm)clickedButton.Form;
            string c_id = sigForm.CliRecord.C_ID.TrimEnd();
            if (c_id == "")
                return;
            string text = sigForm.CliRecord.C_NOM.TrimEnd() + "\r\n"
                        + sigForm.CliRecord.C_ADRESSE.TrimEnd() + "\r\n"
                        + sigForm.CliRecord.C_ADRESSE2.TrimEnd() + "\r\n"
                        + sigForm.CliRecord.C_CODEP.TrimEnd() + "    " + sigForm.CliRecord.C_VILLE.TrimEnd();

            object missing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Word.Application ox_word = new Microsoft.Office.Interop.Word.Application();

            object origFileName = MercatorUi.Globals.RepData + "files\\lettre_2007.docx";
            if (!System.IO.File.Exists(origFileName.ToString()))
            {
                MercatorUi.Dialogs.Stop(string.Format("Fichier non trouvé : \"{0}\" !", origFileName));
                return;
            }
            object isVisible = true;
            object readOnly = false;
            Microsoft.Office.Interop.Word.Document ox_document = ox_word.Documents.Open(ref origFileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);

            string destDir = MercatorUi.Globals.RepData + "files\\cli\\" + Api.CleanRep(c_id);
            if (!System.IO.Directory.Exists(destDir))
                System.IO.Directory.CreateDirectory(destDir);
            object destFileName = destDir + "\\" + DateTime.Now.ToString("yyyMMddHHmmss", System.Globalization.DateTimeFormatInfo.InvariantInfo) + ".docx";
            ox_document.SaveAs(ref destFileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

            object bookmarkName = "Adresse";
            Microsoft.Office.Interop.Word.Bookmark bookmark = ox_document.Bookmarks.get_Item(ref bookmarkName);
            if (bookmark != null)
                bookmark.Range.Text = text;

            ox_word.Visible = true;

            System.Runtime.InteropServices.Marshal.ReleaseComObject(ox_document);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(ox_word);
        }
    }
}


Te laden : 0000001992.zip (18 Kb - 01-12-2014)