Domanda

Una storiella indietro : Ho piccola applicazione che utilizzerà Word per generare una firma di Outlook in base a un modello di Word e di dati dalla directory attiva dell'azienda. Funziona meravigliosa su computer con Office 2007, dal momento che ho usato "Microsoft Word 12.0 Object Library" quando ho codificato sul mio computer.

Ci sono alcuni computer della rete con Office 2003, però, e su tali computer "di Microsoft Word 12.0 Object Library" non è presente, con conseguente eccezioni a destra ea sinistra.

La mia domanda è : Come faccio a rilevare quale versione di Office è installato e quindi la versione del "Microsoft Word Object Library" è disponibile, e successivamente caricarlo. Sono abbastanza sicuro che la funzionalità sto usando sono sia nel "Microsoft Word Biblioteca 12.0 Object" e "Microsoft Word 11.0 Object Library".

Nel caso in cui qualcuno fosse interessato, ecco il mio codice corrente utilizzato per generare la firma:

class Signature
{
    public Dictionary<string, string> TemplateMappings { get; set;}
    public string SignatureTemplateFileName { get; set; }
    public string SignatureName { get; set;}
    public bool UseSignatureWithNewMessages { get; set; }
    public bool UseSignatureInReplyMessages { get; set; }

    public Signature()
    {
        UseSignatureWithNewMessages = true;
        UseSignatureInReplyMessages = true;
        TemplateMappings = new Dictionary<string, string>();
    }

    public void Create()
    {
        if(string.IsNullOrEmpty(SignatureTemplateFileName) || !File.Exists(SignatureTemplateFileName))
        {
            throw new InvalidOperationException("SignatureTemplateFileName is null or the file do not exists");
        }

        if(string.IsNullOrEmpty(SignatureName))
        {
            throw new InvalidOperationException("No SignatureName specified");
        }

        object nullObject = System.Reflection.Missing.Value;
        object signatureTemplate = SignatureTemplateFileName;

        // open word doc
        var word = new ApplicationClass();
        var doc = word.Documents.Add(ref signatureTemplate, ref nullObject, ref nullObject, ref nullObject);

        // search/replace user info
        object wdReplaceAll = WdReplace.wdReplaceAll;
        var find = word.Selection.Find;
        foreach (var pair in TemplateMappings)
        {
            find.Text = pair.Key;
            find.Forward = true;
            find.MatchCase = true;
            find.MatchWholeWord = true;
            find.Replacement.Text = pair.Value;
            find.Execute(ref nullObject /* FindText */,
                         ref nullObject /* MatchCase*/,
                         ref nullObject /* MatchWholeWord*/,
                         ref nullObject /* MatchWildcards*/,
                         ref nullObject /* MatchSoundsLike*/,
                         ref nullObject /* MatchAllWordForms*/,
                         ref nullObject /* Forward*/,
                         ref nullObject /* Wrap*/,
                         ref nullObject /* Format*/,
                         ref nullObject /* ReplaceWith*/,
                         ref wdReplaceAll /* Replace*/,
                         ref nullObject /* MatchKashida*/,
                         ref nullObject /* MatchDiacritics*/,
                         ref nullObject /* MatchAlefHamza*/,
                         ref nullObject /* MatchControl */);
        }

        // Add signature to outlook
        var signatureRange = doc.Range(ref nullObject, ref nullObject);
        word.EmailOptions.EmailSignature.EmailSignatureEntries.Add(SignatureName, signatureRange);

        // set new signature as default for news messages and replies
        if (UseSignatureWithNewMessages)
            word.EmailOptions.EmailSignature.NewMessageSignature = SignatureName;
        if (UseSignatureInReplyMessages)
            word.EmailOptions.EmailSignature.ReplyMessageSignature = SignatureName;

        // close and clean up
        doc.Saved = true;
        doc.Close(ref nullObject, ref nullObject, ref nullObject);
        word.Quit(ref nullObject, ref nullObject, ref nullObject);
    }
}

Ogni aiuto sarà molto apprezzato. Ingresso sul codice di cui sopra è il benvenuto; Non ho alcuna esperienza di codifica contro biblioteca Ufficio Interop, quindi sono sicuro che ci sono cose che posso fare in modo diverso.

Con i migliori saluti, Egil.

È stato utile?

Soluzione

OK trovato quello che cercavo.

MS Office Net For astrae il mal di testa delle versioni di montaggio Ufficio di interoperabilità sottostanti. Forse lo si può utilizzare direttamente nel progetto, altrimenti studiare la sua implementazione per capire come affrontare questo problema. Le persone che lavorano su quel progetto sono probabilmente una grande risorsa per porre domande di interoperabilità, nonché ..

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top