检测并动态加载已安装的 Microsoft Word 对象库

发布于 2024-08-08 01:51:43 字数 3760 浏览 3 评论 0原文

一个小背景故事:我有一个小型应用程序,它将使用 Word 根据 Word 模板和公司活动目录中的数据生成 Outlook 签名。它在装有 Office 2007 的计算机上运行得非常好,因为我在自己的计算机上编写代码时使用了“Microsoft Word 12.0 对象库”。

但网络上有相当多的计算机装有 Office 2003,并且在这些计算机上缺少“Microsoft Word 12.0 对象库”,导致左右出现异常。

我的问题是:如何检测安装的 Office 版本以及可用的“Microsoft Word 对象库”版本,然后加载它。我非常确定我正在使用的功能位于“Microsoft Word 12.0 对象库”和“Microsoft Word 11.0 对象库”中。

如果有人感兴趣,这是我当前用于生成签名的代码:

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);
    }
}

任何帮助将不胜感激。也欢迎输入上面的代码;我没有任何针对 Office Interop 库进行编码的经验,因此我确信我可以做一些不同的事情。

最好的问候,埃吉尔。

A little back story: I have small application that will use Word to generate a Outlook signature based on a Word template and data from the company active directory. It works wonderful on computers with Office 2007, since I used "Microsoft Word 12.0 Object Library" when I coded it on my own computer.

There are quite a few computers on the network with Office 2003 though, and on those computers "Microsoft Word 12.0 Object Library" is missing, resulting in exceptions left and right.

My question is: How do I detect which version of Office is installed and thus which version of the "Microsoft Word Object Library" is available, and subsequently load it. I am quite sure that the functionality I am using are in both the "Microsoft Word 12.0 Object Library" and the "Microsoft Word 11.0 Object Library".

In case anybody is interested, here is my current code used to generate the signature:

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);
    }
}

Any help will be much appreciated. Input on the code above is also welcome; I do not have any experience coding against Office Interop library, so I am sure there are things I can do differently.

Best regards, Egil.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你是年少的欢喜 2024-08-15 01:51:43

好的,找到了我要找的东西。

MS Office For Net 消除了底层 Office 互操作程序集版本的麻烦。也许您可以直接在项目中使用它,或者研究它的实现以了解如何解决这个问题。从事该项目的人员可能也是提出互操作问题的重要资源。

OK found what I was looking for.

MS Office For Net abstracts away the headaches of the underlying Office interop assembly versions. Perhaps you can use it directly in your project, else study it's implementation to understand how to address this issue. The folks working on that project are probably a great resource to ask interop questions as well..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文