(Code from Nenad Prekupec) http://www.eggheadcafe.com/community/aspnet/2/10029362/vsto--managing-excel-200.aspx
public enum eOfficeVersion { eOfficeVersion_Unknown, // error return value
eOfficeVersion_95,
eOfficeVersion_97,
eOfficeVersion_2000,
eOfficeVersion_XP, // XP = 2002 + marketing
eOfficeVersion_2003,
eOfficeVersion_2007,
};
public enum eOfficeApp // in case you are looking for a particular app
{
eOfficeApp_Word,
eOfficeApp_Excel,
eOfficeApp_Outlook,
eOfficeApp_Access,
eOfficeApp_PowerPoint,
};public static eOfficeVersion GetApplicationVersion(eOfficeApp appToCheck) { // some of this function is based on the code in the article at: http://support.microsoft.com/kb/q247985/
string progID = GetProgID(appToCheck); RegistryKey hKey = Registry.ClassesRoot.OpenSubKey(progID, RegistryKeyPermissionCheck.ReadSubTree, System.Security.AccessControl.RegistryRights.ReadKey); if (hKey == null) return eOfficeVersion.eOfficeVersion_Unknown; RegistryKey hKey1 = hKey.OpenSubKey("CurVer", RegistryKeyPermissionCheck.ReadSubTree, System.Security.AccessControl.RegistryRights.ReadKey); if (hKey1 == null) { hKey1.Close(); hKey.Close(); return eOfficeVersion.eOfficeVersion_Unknown; } // Get the Version information
string progAndVersion = (string)hKey1.GetValue(""); // Close the registry keys
hKey1.Close();
hKey.Close();
// Error while querying for value
if (progAndVersion == null) return eOfficeVersion.eOfficeVersion_Unknown; // At this point progAndVersion contains the ProgID followed by a number.
// For example, Word 97 will return Word.Application.8 and Word 2000 will return Word.Application.9
int lastDot = progAndVersion.LastIndexOf('.'); int firstCharOfVersion = lastDot + 1; // + 1 to get rid of the dot at the front
string versionString = progAndVersion.Substring(firstCharOfVersion, progAndVersion.Length - firstCharOfVersion); return StringToVersion(versionString); }
public static string GetApplicationAsString(eOfficeApp officeApp) { switch(officeApp)
{ case eOfficeApp.eOfficeApp_Word: { return "Word"; } break; case eOfficeApp.eOfficeApp_Excel: { return "Excel"; } break; case eOfficeApp.eOfficeApp_Outlook: { return "Outlook"; } break; case eOfficeApp.eOfficeApp_Access: { return "Access"; } break; case eOfficeApp.eOfficeApp_PowerPoint: { return "Powerpoint"; } break; default: { /*ASSERT(false);*/ return string.Empty; } break; // added another ??? } }
public static string GetProgID(eOfficeApp officeApp)
{
// ProgIDs from http://support.microsoft.com/kb/240794/EN-US/ switch (officeApp) { case eOfficeApp.eOfficeApp_Word: { return "Word.Application"; } break; case eOfficeApp.eOfficeApp_Excel: { return "Excel.Application"; } break; case eOfficeApp.eOfficeApp_Outlook: { return "Outlook.Application"; } break; case eOfficeApp.eOfficeApp_Access: { return "Access.Application"; } break; case eOfficeApp.eOfficeApp_PowerPoint: { return "Powerpoint.Application"; } break; default: { /*ASSERT(false);*/ return string.Empty; } break; // added another ??? }}
public static eOfficeVersion StringToVersion(string versionString)
{ // mapping between the marketing version (e.g. 2003) and the behind-the-scenes version if("7" == versionString){ return eOfficeVersion.eOfficeVersion_95; }else if("8" == versionString){ return eOfficeVersion.eOfficeVersion_97; }else if("9" == versionString){ return eOfficeVersion.eOfficeVersion_2000; }else if("10" == versionString){ return eOfficeVersion.eOfficeVersion_XP; }else if("11" == versionString){ return eOfficeVersion.eOfficeVersion_2003; }else if("12" == versionString){ return eOfficeVersion.eOfficeVersion_2007; }else{ return eOfficeVersion.eOfficeVersion_Unknown; // added another ??? } }
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment