利用VC控制OFFICE文件(.doc、.xls、.ppt)的摘要属性(标题、作者、公司和备注)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
利用VC控制OFFICE文件(.doc、.xls、.ppt)的摘要属性
(标题、作者、公司和备注)
#define WIN32_LEAN_AND_MEAN
#define UNICODE
#define _UNICODE
#include <iostream>
#include <tchar.h>
#include <ole2.h>
int _tmain(int argc, _TCHAR* argv[])
{
TCHAR szFileName[MAX_PATH];
printf( "================================= ===================\n ");
printf( "Start Demo\n ");
if(argc != 2)
printf( "usage: summinfodemo filename\n ");
else
{
_tcscpy(szFileName,argv[1]);
_tprintf(_T( "%s\n "),szFileName);
printf( "================================= ===================\n ");
}
IStorage *pStorage = NULL;
IPropertySetStorage *pPropSetStg = NULL;
IPropertyStorage *pPropStg = NULL;
HRESULT hr;
hr = StgOpenStorageEx(szFileName,
STGM_READ | STGM_SHARE_DENY_WRITE,
STGFMT_ANY,
0,
NULL,
NULL,
IID_IPropertySetStorage,
reinterpret_cast <void**> (&pStorage));
if(FAILED(hr))
{
if(hr == STG_E_FILENOTFOUND)
printf( "File not found. ");
else if(hr == STG_E_FILEALREADYEXISTS)
printf( "Not a compound file. ");
else
printf( "StgOpenStorageEx() failed w/error %08lx\n ", hr);
return 0;
}
if(SUCCEEDED(hr))
{
hr = pStorage-> QueryInterface(IID_IPropertySetStorage, (void
**)&pPropSetStg);
if(FAILED(hr))
{
printf( "QI for IPropertySetStorage failed w/error %08l x\n ", hr);
pStorage-> Release();
return 0;
}
}
if(SUCCEEDED(hr))
{
hr = pPropSetStg-> Open(FMTID_SummaryInformation, STGM_READ |
STGM_SHARE_EXCLUSIVE, &pPropStg);
if(FAILED(hr))
{
printf( "No Summary-Information - w/error %08lx\n ",hr);
pPropSetStg-> Release();
pStorage-> Release();
return 0;
}
}
if (SUCCEEDED(hr))
{
struct pidsiStruct
{
char *name;
long pidsi;
}
pidsiArr[] =
{
{ "Title ", PIDSI_TITLE}, // VT_LPSTR
{ "Subject ", PIDSI_SUBJECT}, // ...
{ "Author ", PIDSI_AUTHOR},
{ "Keywords ", PIDSI_KEYWORDS},
{ "Comments ", PIDSI_COMMENTS},
{0, 0}
};
int nPidsi = 0;
for(nPidsi=0; pidsiArr[nPidsi].name; nPidsi++);
PROPSPEC *pPropSpec = new PROPSPEC [nPidsi];
PROPVARIANT *pPropVar = new PROPVARIANT [nPid si];
for(int i=0; i <nPidsi; i++)
{
ZeroMemory(&pPropSpec, sizeof(PROPSPEC));
pPropSpec.ulKind = PRSPEC_PROPID;
pPropSpec.propid = pidsiArr.pidsi;
}
hr = pPropStg-> ReadMultiple(nPidsi, pPropSpec, pPropVar);
if (SUCCEEDED(hr))
{
for(int i=0; i <nPidsi; i++)
{
_TCHAR sValue[1024] = {0};
std::string strTemp = pPropVar-> pszVal;
printf( "\t%s\n ",strTemp.c_str());
pPropVar++;
}
}
}
pPropSetStg-> Release();
pPropStg-> Release();
pStorage-> Release();
printf( "================================= ==================\n ");
printf( "End Demo\n ");
return 0;
}。