检测和动态加载安装Microsoft Word对象库
-
20-09-2019 - |
题
小背后的故事:我将使用Word生成基于从公司Active Directory中的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);
}
}
任何帮助将不胜感激。上面的代码输入也是受欢迎;我没有任何经验的编码对办公室的Interop库,所以我肯定有件事情我可以做的不同。
最好的问候,埃吉尔。
解决方案
OK找到了我要找的东西。
MS Office的净抽象了底层办公室互操作组件版本的头痛。也许你可以直接在你的项目中使用它,否则研究它的实现,以了解如何解决这个问题。在该项目上工作的人可能是一个很好的资源要问的互操作问题以及..
不隶属于 StackOverflow