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.