一個小故事:我有一個小應用程序,它將使用Word根據Word模板和公司活動目錄中的數據生成Outlook簽名。它在Office 2007的電腦上效果很好,因爲我在自己的電腦上編寫了「Microsoft Word 12.0 Object Library」。檢測並動態加載已安裝的Microsoft Word對象庫
儘管Office 2003的網絡上有不少計算機,並且在這些計算機上缺少「Microsoft Word 12.0對象庫」,從而導致左右異常。
我的問題是:如何檢測其版本的Office安裝,因此其中的「Microsoft Word對象庫」的版本,並隨後加載它。我很確定我使用的功能在「Microsoft Word 12.0 Object Library」和「Microsoft Word 11.0 Object Library」中。
如果有人有興趣,這是我用來生成簽名當前代碼:
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庫的經驗,所以我確信有些事情我可以做不同的事情。
最好的問候,埃吉爾。
我似乎還記得一個開源項目,提供Office interop DLL和您的應用程序之間的抽象層。不知道它是否僅適用於Excel或更廣泛的Office產品,並且似乎無法找到合適的Google條款。任何人都知道那是什麼項目?我的家用PC上應該有一個鏈接。埃吉爾,如果您在幾個小時內沒有解決方案,請回復此評論,我會查看。 – 2009-10-08 21:03:50