VC检测Office版本

现在做的一个项目,里边用到了ActiveX控件,内嵌了Word,在安装Word的电脑运行是没问题的,但是在没有Word环境的电脑上就出问题,所以需要检测一下当前机器是否安装了Office,然后做出友好提示。在网上找了一个类(类的声明实现都在.h文件里,用的时候包含一下就行了),挺好用的,原理是搜索注册表,查看是否存在Office注册表文件,如果已安装,便获得当前Office版本的详细信息,如果没安装,返回 Not found. 这个类贴在下边了,后边还附了使用示例,兄弟们拿走不谢(借花献佛吧)。


class OfficeVersion
{
public:
enum eOfficeVersion
{
eOfficeVersion_Unknown, // error return value
eOfficeVersion_95,
eOfficeVersion_97,
eOfficeVersion_2000,
eOfficeVersion_XP,   // XP = 2002 + marketing
eOfficeVersion_2003,
eOfficeVersion_2007,
eOfficeVersion_2010,
eOfficeVersion_2013,
};


enum eOfficeApp // in case you are looking for a particular app
{
eOfficeApp_Word,
eOfficeApp_Excel,
eOfficeApp_Outlook,
eOfficeApp_Access,
eOfficeApp_PowerPoint,
};


static CString GetVersionAsString(const eOfficeVersion officeVersion)
{
switch(officeVersion) {
case eOfficeVersion_Unknown: { return _T("Not found");       }break;
case eOfficeVersion_95:      { return _T("Office 95");       }break;
case eOfficeVersion_97:      { return _T("Office 97");       }break;
case eOfficeVersion_2000:    { return _T("Office 2000");     }break;
case eOfficeVersion_XP:      { return _T("Office XP");       }break;
case eOfficeVersion_2003:    { return _T("Office 2003");     }break;
case eOfficeVersion_2007:    { return _T("Office 2007");     }break;
case eOfficeVersion_2010:    { return _T("Office 2010");     }break;
case eOfficeVersion_2013:    { return _T("Office 2013");     }break;
default:                     { ASSERT(false); return _T(""); }break; // added another ???
}
}


static CString GetApplicationAsString(const eOfficeApp officeApp)
{
switch(officeApp) {
case eOfficeApp_Word:       { return _T("Word");            }break;
case eOfficeApp_Excel:      { return _T("Excel");           }break;
case eOfficeApp_Outlook:    { return _T("Outlook");         }break;
case eOfficeApp_Access:     { return _T("Access");          }break;
case eOfficeApp_PowerPoint: { return _T("Powerpoint");      }break;
default:                    { ASSERT(false); return _T(""); }break; // added another ???
}
}




static CString GetProgID(const eOfficeApp officeApp)
{
// ProgIDs from http://support.microsoft.com/kb/240794/EN-US/
switch(officeApp) {
case eOfficeApp_Word:       { return _T("Word.Application");       }break;
case eOfficeApp_Excel:      { return _T("Excel.Application");      }break;
case eOfficeApp_Outlook:    { return _T("Outlook.Application");    }break;
case eOfficeApp_Access:     { return _T("Access.Application");     }break;
case eOfficeApp_PowerPoint: { return _T("Powerpoint.Application"); }break;
default:                    { ASSERT(false); return _T("");        }break; // added another ???
}
}


static eOfficeVersion StringToVersion(const CString& versionString)
{
// mapping between the marketing version (e.g. 2003) and the behind-the-scenes version
if(_T("7") == versionString){
return eOfficeVersion_95;
}else if(_T("8") == versionString){
return eOfficeVersion_97;
}else if(_T("9") == versionString){
return eOfficeVersion_2000;
}else if(_T("10") == versionString){
return eOfficeVersion_XP;
}else if(_T("11") == versionString){
return eOfficeVersion_2003;
}else if(_T("12") == versionString){
return eOfficeVersion_2007;
}else if(_T("14") == versionString){
return eOfficeVersion_2010;
}else if(_T("15") == versionString){
return eOfficeVersion_2013;
}else{
return eOfficeVersion_Unknown; // added another ???
}
}

static eOfficeVersion GetOfficeVersion()
{
// by default we use Word (and so on, down the list) as a proxy for "Office" 
// (i.e. if word is there then "Office" is there)
// if you want something more specific, then call GetApplicationVersion()


static const eOfficeApp appsToCheck[] = {
eOfficeApp_Word,
eOfficeApp_Excel,
eOfficeApp_Outlook,
eOfficeApp_Access,
eOfficeApp_PowerPoint,
};
const int numItems( sizeof(appsToCheck) / sizeof(appsToCheck[0]) );

for(int i=0; i



使用示例:

 

OfficeVersion::eOfficeVersion eOffVer = OfficeVersion::GetApplicationVersion( OfficeVersion::eOfficeApp_Word )//OfficeVersion::eOfficeApp_Word 代表查询的是word版本, OfficeVersion::eOfficeApp_Excel, OfficeVersion::eOfficeApp_Outlook, OfficeVersion::eOfficeApp_Access, OfficeVersion::eOfficeApp_PowerPoint,分别对应的是各自版本(就不一一说了,含义大家都看得懂的)
CString strVersion = OfficeVersion::GetVersionAsString( eOffVer);//返回版本信息

你可能感兴趣的:(MFCWin32)