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<numItems; ++i){
const eOfficeVersion thisAppVersion( GetApplicationVersion(eOfficeApp_Word) );
if(eOfficeVersion_Unknown != thisAppVersion){
return thisAppVersion;
}
}

return eOfficeVersion_Unknown; // probably nothing installed
}


static eOfficeVersion GetApplicationVersion(eOfficeApp appToCheck)
{
// some of this function is based on the code in the article at: http://support.microsoft.com/kb/q247985/
const CString progID( GetProgID(appToCheck) );


HKEY hKey( NULL);
HKEY hKey1(NULL);


if(ERROR_SUCCESS != ::RegOpenKeyEx(HKEY_CLASSES_ROOT, progID, 0, KEY_READ, &hKey) ){
return eOfficeVersion_Unknown;
}


if(ERROR_SUCCESS != ::RegOpenKeyEx(hKey, _T("CurVer"), 0, KEY_READ, &hKey1)) {
::RegCloseKey(hKey);
return eOfficeVersion_Unknown;
}
 
// Get the Version information
const int BUFFER_SIZE(255);
ULONG cSize(BUFFER_SIZE);
TCHAR szVersion[BUFFER_SIZE];
const LONG lRet( ::RegQueryValueEx(hKey1, NULL, NULL, NULL, (LPBYTE)szVersion, &cSize) );


// Close the registry keys
::RegCloseKey(hKey1);
::RegCloseKey(hKey);


// Error while querying for value
if(ERROR_SUCCESS != lRet){
return eOfficeVersion_Unknown;
}


const CString progAndVersion(szVersion);
// At this point szVersion contains the ProgID followed by a number. 
// For example, Word 97 will return Word.Application.8 and Word 2000 will return Word.Application.9

const int lastDot( progAndVersion.ReverseFind(_T('.')) );
const int firstCharOfVersion( lastDot + 1); // + 1 to get rid of the dot at the front
const CString versionString( progAndVersion.Right(progAndVersion.GetLength() - firstCharOfVersion) );


return StringToVersion(versionString);
}
};



使用示例:

 

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);//返回版本信息

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值