IPersistFile的Save函数在WORD2003中的缺陷
IPersistFile的Save函数可以保存当前文件,如果在WORD2003中打开一个WORD2007格式的文件,并调用Save函数,你会发现原先的2007格式丢失,当前文档成为了97-03格式。由于后缀名依然为.docx,导致WORD2007打开文档时出错。如果你将出错文档的扩展名修改为.doc就可以打开。也就是说在调用Save函数时,WORD2003并没有考虑被打开文档的格式,而是统一以97-03格式保存。测试代码如下:
1 CComPtr<IOleObject> pole = NULL; 2 CComPtr<IBindCtx> pbctx = NULL; 3 CComPtr<IMoniker> pmkfile = NULL; 4 5 HRESULT hr = S_OK; 6 BIND_OPTS bopts = { sizeof(BIND_OPTS), BIND_MAYBOTHERUSER, 0, 10000 }; 7 8 LPOLESTR pwszFile = _T(“D:\\Test.docx”); 9 10 bopts.grfMode = (STGM_TRANSACTED | STGM_SHARE_DENY_WRITE | STGM_READWRITE); 11 12 ::CoInitialize(NULL); 13 14 hr = CreateBindCtx(0, &pbctx); 15 hr = pbctx->SetBindOptions(&bopts); 16 hr = CreateFileMoniker(pwszFile, &pmkfile); 17 hr = pmkfile->BindToObject(pbctx, NULL, IID_IOleObject, (void**)&pole); 18 19 CComPtr<IPersistFile> ppfile; 20 21 hr = pole.QueryInterface<IPersistFile>(&ppfile); 22 hr = ppfile->Save(NULL, TRUE); 23 24 ::CoUninitialize();