Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检测并动态加载已安装的Microsoft Word对象库_C#_.net_Office Interop_Dynamic Loading_Com Object - Fatal编程技术网

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

C# 检测并动态加载已安装的Microsoft Word对象库,c#,.net,office-interop,dynamic-loading,com-object,C#,.net,Office Interop,Dynamic Loading,Com Object,一个小背景故事:我有一个小应用程序,它将使用Word根据Word模板和公司active directory的数据生成Outlook签名。它在使用Office2007的计算机上运行得非常好,因为我在自己的计算机上编码时使用了“MicrosoftWord12.0对象库” 不过,网络上使用Office 2003的计算机相当多,这些计算机上缺少“Microsoft Word 12.0对象库”,导致左右两侧出现异常 我的问题是:如何检测安装了哪个版本的Office,从而检测出哪个版本的“Microsoft

一个小背景故事:我有一个小应用程序,它将使用Word根据Word模板和公司active directory的数据生成Outlook签名。它在使用Office2007的计算机上运行得非常好,因为我在自己的计算机上编码时使用了“MicrosoftWord12.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);
    }
}
类签名
{
公共字典模板映射{get;set;}
公共字符串SignatureTemplateFileName{get;set;}
公共字符串SignatureName{get;set;}
public bool UseSignatureWithNewMessages{get;set;}
公共bool UseSignatureInReplyMessages{get;set;}
公共签名()
{
UseSignatureWithNewMessages=true;
UseSignatureInReplyMessages=true;
TemplateMappings=新字典();
}
公共void Create()
{
如果(string.IsNullOrEmpty(SignatureTemplateFileName)| |!File.Exists(SignatureTemplateFileName))
{
抛出新的InvalidOperationException(“SignatureTemplateFileName为null或文件不存在”);
}
if(string.IsNullOrEmpty(SignatureName))
{
抛出新的InvalidOperationException(“未指定SignatureName”);
}
object nullObject=System.Reflection.Missing.Value;
对象signatureTemplate=SignatureTemplateFileName;
//开放式word文档
var word=新应用程序类();
var doc=word.Documents.Add(ref signatureTemplate、ref nullObject、ref nullObject、ref nullObject);
//搜索/替换用户信息
对象wdReplaceAll=WdReplace.wdReplaceAll;
var find=word.Selection.find;
foreach(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/*匹配案例*/,,
ref nullObject/*MATCHOWELWORD*/,,
ref nullObject/*匹配通配符*/,,
ref nullObject/*MatchSoundsLike*/,,
ref nullObject/*MatchAllWordForms*/,,
ref nullObject/*Forward*/,,
ref nullObject/*Wrap*/,,
ref nullObject/*格式*/,,
ref nullObject/*替换为*/,
参考wdReplaceAll/*Replace*/,,
ref nullObject/*MatchKashida*/,,
ref nullObject/*MatchDiacritics*/,,
ref nullObject/*MatchAlefHamza*/,,
ref nullObject/*MatchControl*/);
}
//向outlook添加签名
var signatureRange=单据范围(ref nullObject,ref nullObject);
word.EmailOptions.EmailSignature.EmailSignatureEntries.Add(SignatureName,signatureRange);
//将新签名设置为新闻消息和回复的默认签名
如果(使用SignatureWithNewMessages)
word.EmailOptions.EmailSignature.NewMessageSignature=SignatureName;
如果(使用SignatureInReplymessages)
word.EmailOptions.EmailSignature.ReplyMessageSignature=SignatureName;
//关闭并清理
doc.Saved=true;
单据关闭(ref nullObject,ref nullObject,ref nullObject);
Quit(ref-nullObject,ref-nullObject,ref-nullObject);
}
}
任何帮助都将不胜感激。也欢迎输入上述代码;我没有任何针对Office互操作库进行编码的经验,所以我确信我可以做一些不同的事情


向你问好,艾吉尔。

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


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

我似乎想起了一个开源项目,它在Office互操作DLL和应用程序之间提供了一个抽象层。不确定它是否只是针对Excel或更广泛的Office产品,而且似乎找不到合适的谷歌术语。有人知道那是什么项目吗?应该在我的家用电脑上有一个链接。Egil,如果你在几个小时内没有解决方案,请回复此评论,我会查看。非常感谢Eric。这正是我要找的。