用VC做word转pdf总是出问题
我用VC做一个word转pdf,当运行到Documents.Open(),过几秒钟线程就自动退出了,不知道是参数弄错了,还是怎么回事? 显示线程 'Win32 线程' (0xbb9c) 已退出,返回值为 0 (0x0)。
BOOL CConverter::Convert(CString strSourcePath, CString strTargetPath, MSWord::WdExportFormat wdExportFormat)
{
BOOL result = FALSE;
MSWord::_ApplicationPtr pWdApplicationPtr;
MSWord::_DocumentPtr pWdDocumentPtr;
COleVariant sourcePath = strSourcePath;
COleVariant argetPath = strTargetPath;
COleVariant vTrue((short)TRUE);
COleVariant vFalse((short)FALSE);
COleVariant vZero((short)0);
COleVariant vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
CoInitialize(NULL);
try
{
HRESULT hResult = pWdApplicationPtr.CreateInstance("Word.Application");
if (hResult != S_OK)
{
AfxMessageBox(_T("Application创建失败,请确保安装了word 2000或以上版本!"), MB_OK|MB_ICONWARNING);
CoUninitialize();
return result;
}
pWdDocumentPtr = pWdApplicationPtr->Documents->Open(sourcePath,
vTrue, // Confirm Conversion.
vFalse, // ReadOnly.
vFalse, // AddToRecentFiles.
vOptional, // PasswordDocument.
vOptional, // PasswordTemplate.
vOptional, // Revert.
vOptional, // WritePasswordDocument.
vOptional, // WritePasswordTemplate.
vOptional, // Format. // Last argument for Word 97
vOptional, // Encoding // New for Word 2000/2002
vFalse, // visible
vOptional, // openAndRepair
vZero, // docDirection
vOptional, // NoEncodingDialog
vOptional);
if(pWdDocumentPtr == NULL)
{
CoUninitialize();
return result;
}
hResult = pWdDocumentPtr->ExportAsFixedFormat((_bstr_t )strTargetPath, // target path
wdExportFormat, // export format
FALSE, // Open After Export
wdExportOptimizeForPrint, // Export OptimizeFor
wdExportAllDocument, // Export Range
0, // start page
0, // end page
wdExportDocumentContent, // Export Item
TRUE, // Include Doc Props
TRUE, // Keep IRM
wdExportCreateWordBookmarks, // Export Create Bookmarks
TRUE, // Doc Structure Tags
TRUE, // Bitmap Missing Fonts
FALSE); // Use ISO19005_1
if (hResult == S_OK)
{
result = TRUE;
}
}
catch(CException* e)
{
TCHAR szError[1024];
e->GetErrorMessage(szError,1024); // e.GetErrorMessage(szError,1024);
AfxMessageBox(_T("WORD文档转PDF异常:") + (CString)szError);
}
if(pWdDocumentPtr != NULL)
{
pWdDocumentPtr->Close();
pWdDocumentPtr = NULL;
}
if(pWdApplicationPtr != NULL)
{
pWdApplicationPtr->Quit();
pWdApplicationPtr = NULL;
}
CoUninitialize();
return result;
}
不知道是错在哪里,请各位帮帮忙吧。