我正在使用应用程序将word文件转换为XML格式。
应用程序运行良好,直到我在我的机器上安装了2007。
但是,一旦我的计算机升级到MS Office 2013,应用程序就会在调用AcceptAllRevisions()
的行中崩溃,抛出KernelBase.dll
例外。
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
cout << "This is a SaveWordToXML ...! " << endl;
// word_mask can be a full, relative, or no path but must have a file mask (wildcards ok)
CString word_mask;
// xml_mask can be a full, relative, or not specified but no filename should be given
CString xml_path;
bool got_word_mask = false;
bool got_xml_path = false;
got_word_mask = true;
word_mask = "C:\\TestFolder\\*.doc";
xml_path = "C:\\TestFolder\\XML\\";
if(got_word_mask)
{
CFileUtils fu_word_mask(word_mask);
CFileUtils fu_xml_path(xml_path);
// Go through each file that matches the input mask and save it to XML
WIN32_FIND_DATA fd;
HANDLE handle;
if((handle = FindFirstFile(fu_word_mask.GetFullPath_File(),&fd)) != INVALID_HANDLE_VALUE)
{
do
{
CFileUtils fu_word_file(fu_word_mask.GetFullPath()+fd.cFileName);
CFileUtils fu_xml_file(fu_xml_path.GetFullPath() + fd.cFileName);
fu_xml_file.SetFileExt("XML");
CWordDoc worddoc(fu_word_file.GetFullPath_File(),true,false);
cout << " Word Doc created Successfully..! "<< endl;
if(worddoc.IsOpen())
{
cout << " Word Doc opened Successfully..! "<< endl;
worddoc.AcceptChanges(); // ***** Application crashes here *******
cout << " Word Doc AcceptChanges Successfully..! "<< endl;
worddoc.SaveAs(fu_xml_file.GetFullPath_File(),WORD_FILE_FORMAT_XML); // ***** Application crashes here *******
cout << " Word Doc SaveAs Successfully..! "<< endl;
}
else
cout << "*** Error opening " << fu_word_file.GetFullPath_File() << endl;
}
while(FindNextFile(handle,&fd));
FindClose(handle);
}
}
else
Usage();
}
return nRetCode;
}
// Defination of AcceptChanges
bool CWordDoc::AcceptChanges()
{
// If change tracking is currently on, accept all changes
wordDoc.AcceptAllRevisions();
return true;
}
int CWordDoc::SaveAs(CString filename, short file_format)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CString new_filename;
if(filename.GetLength() == 0)
{
new_filename = filename;
}
// COleVariant var_filename((_bstr_t) filename);
COleVariant var_filename(filename);
COleVariant var_fileformat(file_format);
#if MSWORD8 // Word 97
wordDoc.SaveAs(&var_bstr,
&vtMissing,
&vtMissing,
&vtMissing,
&vtMissing,
&vtMissing,
&vtMissing,
&vtMissing,
&vtMissing,
&vtMissing,
&vtMissing);
#elif MSWORD11 // Word 2003
wordDoc.SaveAs2(&var_filename,
&var_fileformat,
&vtMissing,
&vtMissing,
&vtMissing,
&vtMissing,
&vtMissing,
&vtMissing,
&vtMissing,
&vtMissing,
&vtMissing,
&vtMissing,
&vtMissing,
&vtMissing,
&vtMissing,
&vtMissing,
&vtMissing
);
#else
#pragma message("Unknown MSWord version (must define MSWORD8 or MSWORD11)")
#endif
return 1;
}
Application throws exception upon calling AcceptChanges() and if i
comment the line AcceptChanges(),it further throws exception at saveAs2()
****** Any clue to solve this issue is greatly appreciated.*****