这个方法只能把word转换成ANSI格式的txt,有些字符编码会丢失,怎样才可以写成UTF8或者是Unicode的txt呢,或者有什么其他的较方便的方法读取word的字符数据呢,希望知道的可以给我清楚的解答,谢谢了。
void DocToTxt(BSTR bstrOpenFile, BSTR bstrSaveFile)
{
// ******************* Declare Some Variables ********************
// Variables that will be used and re-used in our calls
DISPPARAMS m_dpNoArgs = {NULL, NULL, 0, 0};
VARIANT m_vResult;
OLECHAR FAR* m_szFunction;
// IDispatch pointers for Word's objects
IDispatch* m_pDispDocs; //Documents collection
IDispatch* m_pDispActiveDoc; //ActiveDocument object
// DISPID's
DISPID m_dispid_Docs; //Documents property of Application object
DISPID m_dispid_ActiveDoc; //ActiveDocument property of Application
DISPID m_dispid_SaveAs; //SaveAs method of the Document object
DISPID m_dispid_Quit; //Quit method of the Application object
DISPID m_dispid_Open; //Open method of the Application object
BSTR m_bstrEmptyString ;
// ******************** Start Automation ***********************
//Initialize the COM libraries
::CoInitialize(NULL);
// Create an instance of the Word application and obtain the pointer
// to the application's IDispatch interface.
CLSID m_clsid;
CLSIDFromProgID(L"Word.Application.12", &m_clsid);
IUnknown* m_pUnk;
HRESULT m_hr = ::CoCreateInstance( m_clsid, NULL, CLSCTX_SERVER,
IID_IUnknown, (void**) &m_pUnk);
IDispatch* m_pDispApp;
m_hr = m_pUnk->QueryInterface( IID_IDispatch, (void**)&m_pDispApp);
// Get IDispatch* for the Documents collection object
m_szFunction = OLESTR("Documents");
m_hr = m_pDispApp->GetIDsOfNames (IID_NULL, &m_szFunction, 1,
LOCALE_USER_DEFAULT, &m_dispid_Docs);
m_hr = m_pDispApp->Invoke (m_dispid_Docs, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_PROPERTYGET, &m_dpNoArgs, &m_vResult,
NULL, NULL);
m_pDispDocs = m_vResult.pdispVal;
// Query id of method open
m_szFunction = OLESTR("Open");
m_hr = m_pDispDocs->GetIDsOfNames(IID_NULL, &m_szFunction,1,
LOCALE_USER_DEFAULT, &m_dispid_Open);
// Prepare parameters for open method
VARIANT vArgsOpen[6];
DISPPARAMS dpOpen;
dpOpen.cArgs = 6;
dpOpen.cNamedArgs = 0;
dpOpen.rgvarg = vArgsOpen;
VARIANT vFalse, vTrue;
vFalse.vt = VT_BOOL;
vFalse.boolVal = FALSE;
vTrue.vt = VT_BOOL;
vTrue.boolVal = TRUE;
m_bstrEmptyString = ::SysAllocString(OLESTR("")) ;
vArgsOpen[5].vt = VT_BSTR;
vArgsOpen[5].bstrVal = bstrOpenFile;
vArgsOpen[4] = vFalse;
vArgsOpen[3] = vTrue;
vArgsOpen[2] = vFalse;
vArgsOpen[1].vt = VT_BSTR;
vArgsOpen[1].bstrVal = m_bstrEmptyString;
vArgsOpen[0].vt = VT_BSTR;
vArgsOpen[0].bstrVal = m_bstrEmptyString;
//Execute open method
m_hr=m_pDispDocs->Invoke(m_dispid_Open,IID_NULL,LOCALE_USER_DEFAULT,
DISPATCH_METHOD,&dpOpen,NULL,NULL,NULL);
//Query activedocument interface
m_szFunction = OLESTR("ActiveDocument");
m_hr = m_pDispApp->GetIDsOfNames(IID_NULL, &m_szFunction, 1,
LOCALE_USER_DEFAULT,&m_dispid_ActiveDoc);
m_hr = m_pDispApp->Invoke(m_dispid_ActiveDoc,IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET,
&m_dpNoArgs,&m_vResult,NULL,NULL);
m_pDispActiveDoc = m_vResult.pdispVal;
//Prepare arguments for save as .txt
VARIANT vArgsSaveAs[11];
DISPPARAMS dpSaveAs;
dpSaveAs.cArgs = 11;
dpSaveAs.cNamedArgs = 0;
dpSaveAs.rgvarg = vArgsSaveAs;
vArgsSaveAs[10].vt = VT_BSTR;
vArgsSaveAs[10].bstrVal = bstrSaveFile; //Filename
vArgsSaveAs[9].vt = VT_I4;
vArgsSaveAs[9].lVal = wdFormatUnicodeText;// 7;//2; //FileFormat(wdFormatText = 2)
vArgsSaveAs[8] = vFalse; //LockComments
vArgsSaveAs[7].vt = VT_BSTR;
vArgsSaveAs[7].bstrVal = m_bstrEmptyString; //Password
vArgsSaveAs[6].vt = VT_BOOL;
vArgsSaveAs[6].boolVal = TRUE; //AddToRecentFiles
vArgsSaveAs[5].vt = VT_BSTR;
vArgsSaveAs[5].bstrVal = m_bstrEmptyString; //WritePassword
vArgsSaveAs[4] = vFalse; //ReadOnlyRecommended
vArgsSaveAs[3] = vFalse; //EmbedTrueTypeFonts
vArgsSaveAs[2] = vFalse; //SaveNativePictureFormat
vArgsSaveAs[1] = vFalse; //SaveFormsData
vArgsSaveAs[0] = vFalse; //SaveAsOCELetter
// Query and execute SaveAs method
m_szFunction = OLESTR("SaveAs");
m_hr = m_pDispActiveDoc->GetIDsOfNames(IID_NULL,&m_szFunction,1,
LOCALE_USER_DEFAULT,&m_dispid_SaveAs);
m_hr = m_pDispActiveDoc->Invoke(m_dispid_SaveAs, IID_NULL,
LOCALE_USER_DEFAULT,DISPATCH_METHOD,
&dpSaveAs,NULL,NULL,NULL);
// Invoke the Quit method
m_szFunction = OLESTR("Quit");
m_hr = m_pDispApp->GetIDsOfNames(IID_NULL, &m_szFunction, 1,
LOCALE_USER_DEFAULT, &m_dispid_Quit);
m_hr = m_pDispApp->Invoke(m_dispid_Quit, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_METHOD, &m_dpNoArgs, NULL, NULL, NULL);
//
cout << m_bstrEmptyString << endl;
cout << (char*)m_bstrEmptyString << endl;
//Clean-up
::SysFreeString(bstrOpenFile) ;
::SysFreeString(bstrSaveFile) ;
::SysFreeString(m_bstrEmptyString) ;
m_pDispActiveDoc->Release();
m_pDispDocs->Release();
m_pDispApp->Release();
m_pUnk->Release();
::CoUninitialize();
}
10 个解决方案
#1
刚才的代码发错了,有个地方调试的时候修改了,有问题,再发一遍
void DocToTxt(BSTR bstrOpenFile, BSTR bstrSaveFile)
{
// ******************* Declare Some Variables ********************
// Variables that will be used and re-used in our calls
DISPPARAMS m_dpNoArgs = {NULL, NULL, 0, 0};
VARIANT m_vResult;
OLECHAR FAR* m_szFunction;
// IDispatch pointers for Word's objects
IDispatch* m_pDispDocs; //Documents collection
IDispatch* m_pDispActiveDoc; //ActiveDocument object
// DISPID's
DISPID m_dispid_Docs; //Documents property of Application object
DISPID m_dispid_ActiveDoc; //ActiveDocument property of Application
DISPID m_dispid_SaveAs; //SaveAs method of the Document object
DISPID m_dispid_Quit; //Quit method of the Application object
DISPID m_dispid_Open; //Open method of the Application object
BSTR m_bstrEmptyString ;
// ******************** Start Automation ***********************
//Initialize the COM libraries
::CoInitialize(NULL);
// Create an instance of the Word application and obtain the pointer
// to the application's IDispatch interface.
CLSID m_clsid;
CLSIDFromProgID(L"Word.Application.12", &m_clsid);
IUnknown* m_pUnk;
HRESULT m_hr = ::CoCreateInstance( m_clsid, NULL, CLSCTX_SERVER,
IID_IUnknown, (void**) &m_pUnk);
IDispatch* m_pDispApp;
m_hr = m_pUnk->QueryInterface( IID_IDispatch, (void**)&m_pDispApp);
// Get IDispatch* for the Documents collection object
m_szFunction = OLESTR("Documents");
m_hr = m_pDispApp->GetIDsOfNames (IID_NULL, &m_szFunction, 1,
LOCALE_USER_DEFAULT, &m_dispid_Docs);
m_hr = m_pDispApp->Invoke (m_dispid_Docs, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_PROPERTYGET, &m_dpNoArgs, &m_vResult,
NULL, NULL);
m_pDispDocs = m_vResult.pdispVal;
// Query id of method open
m_szFunction = OLESTR("Open");
m_hr = m_pDispDocs->GetIDsOfNames(IID_NULL, &m_szFunction,1,
LOCALE_USER_DEFAULT, &m_dispid_Open);
// Prepare parameters for open method
VARIANT vArgsOpen[6];
DISPPARAMS dpOpen;
dpOpen.cArgs = 6;
dpOpen.cNamedArgs = 0;
dpOpen.rgvarg = vArgsOpen;
VARIANT vFalse, vTrue;
vFalse.vt = VT_BOOL;
vFalse.boolVal = FALSE;
vTrue.vt = VT_BOOL;
vTrue.boolVal = TRUE;
m_bstrEmptyString = ::SysAllocString(OLESTR("")) ;
vArgsOpen[5].vt = VT_BSTR;
vArgsOpen[5].bstrVal = bstrOpenFile;
vArgsOpen[4] = vFalse;
vArgsOpen[3] = vTrue;
vArgsOpen[2] = vFalse;
vArgsOpen[1].vt = VT_BSTR;
vArgsOpen[1].bstrVal = m_bstrEmptyString;
vArgsOpen[0].vt = VT_BSTR;
vArgsOpen[0].bstrVal = m_bstrEmptyString;
//Execute open method
m_hr=m_pDispDocs->Invoke(m_dispid_Open,IID_NULL,LOCALE_USER_DEFAULT,
DISPATCH_METHOD,&dpOpen,NULL,NULL,NULL);
//Query activedocument interface
m_szFunction = OLESTR("ActiveDocument");
m_hr = m_pDispApp->GetIDsOfNames(IID_NULL, &m_szFunction, 1,
LOCALE_USER_DEFAULT,&m_dispid_ActiveDoc);
m_hr = m_pDispApp->Invoke(m_dispid_ActiveDoc,IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET,
&m_dpNoArgs,&m_vResult,NULL,NULL);
m_pDispActiveDoc = m_vResult.pdispVal;
//Prepare arguments for save as .txt
VARIANT vArgsSaveAs[11];
DISPPARAMS dpSaveAs;
dpSaveAs.cArgs = 11;
dpSaveAs.cNamedArgs = 0;
dpSaveAs.rgvarg = vArgsSaveAs;
vArgsSaveAs[10].vt = VT_BSTR;
vArgsSaveAs[10].bstrVal = bstrSaveFile; //Filename
vArgsSaveAs[9].vt = VT_I4;
vArgsSaveAs[9].lVal = 7;//wdFormatUnicodeText;// 7;//2; //FileFormat(wdFormatText = 2)
vArgsSaveAs[8] = vFalse; //LockComments
vArgsSaveAs[7].vt = VT_BSTR;
vArgsSaveAs[7].bstrVal = m_bstrEmptyString; //Password
vArgsSaveAs[6].vt = VT_BOOL;
vArgsSaveAs[6].boolVal = TRUE; //AddToRecentFiles
vArgsSaveAs[5].vt = VT_BSTR;
vArgsSaveAs[5].bstrVal = m_bstrEmptyString; //WritePassword
vArgsSaveAs[4] = vFalse; //ReadOnlyRecommended
vArgsSaveAs[3] = vFalse; //EmbedTrueTypeFonts
vArgsSaveAs[2] = vFalse; //SaveNativePictureFormat
vArgsSaveAs[1] = vFalse; //SaveFormsData
vArgsSaveAs[0] = vFalse; //SaveAsOCELetter
// Query and execute SaveAs method
m_szFunction = OLESTR("SaveAs");
m_hr = m_pDispActiveDoc->GetIDsOfNames(IID_NULL,&m_szFunction,1,
LOCALE_USER_DEFAULT,&m_dispid_SaveAs);
m_hr = m_pDispActiveDoc->Invoke(m_dispid_SaveAs, IID_NULL,
LOCALE_USER_DEFAULT,DISPATCH_METHOD,
&dpSaveAs,NULL,NULL,NULL);
// Invoke the Quit method
m_szFunction = OLESTR("Quit");
m_hr = m_pDispApp->GetIDsOfNames(IID_NULL, &m_szFunction, 1,
LOCALE_USER_DEFAULT, &m_dispid_Quit);
m_hr = m_pDispApp->Invoke(m_dispid_Quit, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_METHOD, &m_dpNoArgs, NULL, NULL, NULL);
//
cout << m_bstrEmptyString << endl;
cout << (char*)m_bstrEmptyString << endl;
//Clean-up
::SysFreeString(bstrOpenFile) ;
::SysFreeString(bstrSaveFile) ;
::SysFreeString(m_bstrEmptyString) ;
m_pDispActiveDoc->Release();
m_pDispDocs->Release();
m_pDispApp->Release();
m_pUnk->Release();
::CoUninitialize();
}
void DocToTxt(BSTR bstrOpenFile, BSTR bstrSaveFile)
{
// ******************* Declare Some Variables ********************
// Variables that will be used and re-used in our calls
DISPPARAMS m_dpNoArgs = {NULL, NULL, 0, 0};
VARIANT m_vResult;
OLECHAR FAR* m_szFunction;
// IDispatch pointers for Word's objects
IDispatch* m_pDispDocs; //Documents collection
IDispatch* m_pDispActiveDoc; //ActiveDocument object
// DISPID's
DISPID m_dispid_Docs; //Documents property of Application object
DISPID m_dispid_ActiveDoc; //ActiveDocument property of Application
DISPID m_dispid_SaveAs; //SaveAs method of the Document object
DISPID m_dispid_Quit; //Quit method of the Application object
DISPID m_dispid_Open; //Open method of the Application object
BSTR m_bstrEmptyString ;
// ******************** Start Automation ***********************
//Initialize the COM libraries
::CoInitialize(NULL);
// Create an instance of the Word application and obtain the pointer
// to the application's IDispatch interface.
CLSID m_clsid;
CLSIDFromProgID(L"Word.Application.12", &m_clsid);
IUnknown* m_pUnk;
HRESULT m_hr = ::CoCreateInstance( m_clsid, NULL, CLSCTX_SERVER,
IID_IUnknown, (void**) &m_pUnk);
IDispatch* m_pDispApp;
m_hr = m_pUnk->QueryInterface( IID_IDispatch, (void**)&m_pDispApp);
// Get IDispatch* for the Documents collection object
m_szFunction = OLESTR("Documents");
m_hr = m_pDispApp->GetIDsOfNames (IID_NULL, &m_szFunction, 1,
LOCALE_USER_DEFAULT, &m_dispid_Docs);
m_hr = m_pDispApp->Invoke (m_dispid_Docs, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_PROPERTYGET, &m_dpNoArgs, &m_vResult,
NULL, NULL);
m_pDispDocs = m_vResult.pdispVal;
// Query id of method open
m_szFunction = OLESTR("Open");
m_hr = m_pDispDocs->GetIDsOfNames(IID_NULL, &m_szFunction,1,
LOCALE_USER_DEFAULT, &m_dispid_Open);
// Prepare parameters for open method
VARIANT vArgsOpen[6];
DISPPARAMS dpOpen;
dpOpen.cArgs = 6;
dpOpen.cNamedArgs = 0;
dpOpen.rgvarg = vArgsOpen;
VARIANT vFalse, vTrue;
vFalse.vt = VT_BOOL;
vFalse.boolVal = FALSE;
vTrue.vt = VT_BOOL;
vTrue.boolVal = TRUE;
m_bstrEmptyString = ::SysAllocString(OLESTR("")) ;
vArgsOpen[5].vt = VT_BSTR;
vArgsOpen[5].bstrVal = bstrOpenFile;
vArgsOpen[4] = vFalse;
vArgsOpen[3] = vTrue;
vArgsOpen[2] = vFalse;
vArgsOpen[1].vt = VT_BSTR;
vArgsOpen[1].bstrVal = m_bstrEmptyString;
vArgsOpen[0].vt = VT_BSTR;
vArgsOpen[0].bstrVal = m_bstrEmptyString;
//Execute open method
m_hr=m_pDispDocs->Invoke(m_dispid_Open,IID_NULL,LOCALE_USER_DEFAULT,
DISPATCH_METHOD,&dpOpen,NULL,NULL,NULL);
//Query activedocument interface
m_szFunction = OLESTR("ActiveDocument");
m_hr = m_pDispApp->GetIDsOfNames(IID_NULL, &m_szFunction, 1,
LOCALE_USER_DEFAULT,&m_dispid_ActiveDoc);
m_hr = m_pDispApp->Invoke(m_dispid_ActiveDoc,IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET,
&m_dpNoArgs,&m_vResult,NULL,NULL);
m_pDispActiveDoc = m_vResult.pdispVal;
//Prepare arguments for save as .txt
VARIANT vArgsSaveAs[11];
DISPPARAMS dpSaveAs;
dpSaveAs.cArgs = 11;
dpSaveAs.cNamedArgs = 0;
dpSaveAs.rgvarg = vArgsSaveAs;
vArgsSaveAs[10].vt = VT_BSTR;
vArgsSaveAs[10].bstrVal = bstrSaveFile; //Filename
vArgsSaveAs[9].vt = VT_I4;
vArgsSaveAs[9].lVal = 7;//wdFormatUnicodeText;// 7;//2; //FileFormat(wdFormatText = 2)
vArgsSaveAs[8] = vFalse; //LockComments
vArgsSaveAs[7].vt = VT_BSTR;
vArgsSaveAs[7].bstrVal = m_bstrEmptyString; //Password
vArgsSaveAs[6].vt = VT_BOOL;
vArgsSaveAs[6].boolVal = TRUE; //AddToRecentFiles
vArgsSaveAs[5].vt = VT_BSTR;
vArgsSaveAs[5].bstrVal = m_bstrEmptyString; //WritePassword
vArgsSaveAs[4] = vFalse; //ReadOnlyRecommended
vArgsSaveAs[3] = vFalse; //EmbedTrueTypeFonts
vArgsSaveAs[2] = vFalse; //SaveNativePictureFormat
vArgsSaveAs[1] = vFalse; //SaveFormsData
vArgsSaveAs[0] = vFalse; //SaveAsOCELetter
// Query and execute SaveAs method
m_szFunction = OLESTR("SaveAs");
m_hr = m_pDispActiveDoc->GetIDsOfNames(IID_NULL,&m_szFunction,1,
LOCALE_USER_DEFAULT,&m_dispid_SaveAs);
m_hr = m_pDispActiveDoc->Invoke(m_dispid_SaveAs, IID_NULL,
LOCALE_USER_DEFAULT,DISPATCH_METHOD,
&dpSaveAs,NULL,NULL,NULL);
// Invoke the Quit method
m_szFunction = OLESTR("Quit");
m_hr = m_pDispApp->GetIDsOfNames(IID_NULL, &m_szFunction, 1,
LOCALE_USER_DEFAULT, &m_dispid_Quit);
m_hr = m_pDispApp->Invoke(m_dispid_Quit, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_METHOD, &m_dpNoArgs, NULL, NULL, NULL);
//
cout << m_bstrEmptyString << endl;
cout << (char*)m_bstrEmptyString << endl;
//Clean-up
::SysFreeString(bstrOpenFile) ;
::SysFreeString(bstrSaveFile) ;
::SysFreeString(m_bstrEmptyString) ;
m_pDispActiveDoc->Release();
m_pDispDocs->Release();
m_pDispApp->Release();
m_pUnk->Release();
::CoUninitialize();
}
#2
office不是提供编程接口了吗
#3
调用
DocToTxt(::SysAllocString(OLESTR("D:\\09\\10\\from.doc")), ::SysAllocString(OLESTR("D:\\09\\10\\to.txt")));
你说的是添加word.h word.cpp文件的那个方法吗,那个方法还没有用过
DocToTxt(::SysAllocString(OLESTR("D:\\09\\10\\from.doc")), ::SysAllocString(OLESTR("D:\\09\\10\\to.txt")));
你说的是添加word.h word.cpp文件的那个方法吗,那个方法还没有用过
#4
请问怎么用?
#5
博主可以参考一下这个博客的文章
http://blog.csdn.net/yangsh3002/article/details/3547297#comments
另外,我也很想知道,如何通过COM获取word文本。
上面那个贴子只讲到写入word文本
http://blog.csdn.net/yangsh3002/article/details/3547297#comments
另外,我也很想知道,如何通过COM获取word文本。
上面那个贴子只讲到写入word文本
#6
同求,关注!
#7
这是我写的,看看这样行不行
::OleInitialize (NULL);
// TODO: 在此添加控件通知处理程序代码
IStorage* pStorage=NULL;
IStream* pStream=NULL;
//检测是否是复合文档
HRESULT hResult=::StgIsStorageFile(TEXT("E:\\资料集锦\\思想汇报\\try.doc"));
if(FAILED(hResult))
{
MessageBox(L"不是复合文档,i am sorry",0);
return;
}
//打开复合文档
hResult=::StgOpenStorage(TEXT("E:\\资料集锦\\思想汇报\\try.doc"),NULL, STGM_READWRITE | STGM_SHARE_EXCLUSIVE ,NULL,0,&pStorage);
//枚举复合文档
IEnumSTATSTG *iEnum=NULL;
hResult=pStorage->EnumElements(0,NULL,0,&iEnum);
if(FAILED(hResult))
{
MessageBox(L"枚举器获取失败,so i am sorry",0,0);
return;
}
STATSTG stg={0};
//怎么样才能给WordDocument
while(NOERROR==iEnum->Next(1,&stg,NULL))
{
if(STGTY_STREAM==stg.type)
{
MessageBox(stg.pwcsName,L"istream",0);
}
else if(STGTY_STORAGE==stg.type )
{
MessageBox(L"STORAGE",0,0);
}
::CoTaskMemFree(stg.pwcsName);
}
hResult=pStorage->OpenStream(TEXT("WordDocument"),0,STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE,0,&pStream);
//CComVariant var(_T("dsada"));
// hResult=pStream->Write(var.bstrVal,var.GetSize(),NULL); 这里的写操作总是出问题,读的话还行
pStorage->Release();
pStream->Release();
::OleUninitialize ();
#8
msaa试试。我没试过,不过理论上应该是行的通的。
#9
//MSAA入门参考此文章
//http://www.vckbase.com/index.php/wv/651
//http://msdn.microsoft.com/zh-cn/magazine/cc301312(en-us).aspx
//代码引用自此处
//http://msdn.microsoft.com/zh-cn/magazine/bb984889(en-us).aspx
//http://www.vckbase.com/index.php/wv/651
//http://msdn.microsoft.com/zh-cn/magazine/cc301312(en-us).aspx
//代码引用自此处
//http://msdn.microsoft.com/zh-cn/magazine/bb984889(en-us).aspx
#10
多谢解答,
这个问题我当时用了2天的时间解决了,方法写在了博客中
http://blog.csdn.net/aaajj/article/details/8800414
那几天耽误了时间,无暇顾及我的投资,结果造成了重大的损失,现在我已跌落深渊,伤痕累累
这个解决方案对我已经没有任何意义了,希望可以给有需要的朋友一些帮助
这个问题我当时用了2天的时间解决了,方法写在了博客中
http://blog.csdn.net/aaajj/article/details/8800414
那几天耽误了时间,无暇顾及我的投资,结果造成了重大的损失,现在我已跌落深渊,伤痕累累
这个解决方案对我已经没有任何意义了,希望可以给有需要的朋友一些帮助
#1
刚才的代码发错了,有个地方调试的时候修改了,有问题,再发一遍
void DocToTxt(BSTR bstrOpenFile, BSTR bstrSaveFile)
{
// ******************* Declare Some Variables ********************
// Variables that will be used and re-used in our calls
DISPPARAMS m_dpNoArgs = {NULL, NULL, 0, 0};
VARIANT m_vResult;
OLECHAR FAR* m_szFunction;
// IDispatch pointers for Word's objects
IDispatch* m_pDispDocs; //Documents collection
IDispatch* m_pDispActiveDoc; //ActiveDocument object
// DISPID's
DISPID m_dispid_Docs; //Documents property of Application object
DISPID m_dispid_ActiveDoc; //ActiveDocument property of Application
DISPID m_dispid_SaveAs; //SaveAs method of the Document object
DISPID m_dispid_Quit; //Quit method of the Application object
DISPID m_dispid_Open; //Open method of the Application object
BSTR m_bstrEmptyString ;
// ******************** Start Automation ***********************
//Initialize the COM libraries
::CoInitialize(NULL);
// Create an instance of the Word application and obtain the pointer
// to the application's IDispatch interface.
CLSID m_clsid;
CLSIDFromProgID(L"Word.Application.12", &m_clsid);
IUnknown* m_pUnk;
HRESULT m_hr = ::CoCreateInstance( m_clsid, NULL, CLSCTX_SERVER,
IID_IUnknown, (void**) &m_pUnk);
IDispatch* m_pDispApp;
m_hr = m_pUnk->QueryInterface( IID_IDispatch, (void**)&m_pDispApp);
// Get IDispatch* for the Documents collection object
m_szFunction = OLESTR("Documents");
m_hr = m_pDispApp->GetIDsOfNames (IID_NULL, &m_szFunction, 1,
LOCALE_USER_DEFAULT, &m_dispid_Docs);
m_hr = m_pDispApp->Invoke (m_dispid_Docs, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_PROPERTYGET, &m_dpNoArgs, &m_vResult,
NULL, NULL);
m_pDispDocs = m_vResult.pdispVal;
// Query id of method open
m_szFunction = OLESTR("Open");
m_hr = m_pDispDocs->GetIDsOfNames(IID_NULL, &m_szFunction,1,
LOCALE_USER_DEFAULT, &m_dispid_Open);
// Prepare parameters for open method
VARIANT vArgsOpen[6];
DISPPARAMS dpOpen;
dpOpen.cArgs = 6;
dpOpen.cNamedArgs = 0;
dpOpen.rgvarg = vArgsOpen;
VARIANT vFalse, vTrue;
vFalse.vt = VT_BOOL;
vFalse.boolVal = FALSE;
vTrue.vt = VT_BOOL;
vTrue.boolVal = TRUE;
m_bstrEmptyString = ::SysAllocString(OLESTR("")) ;
vArgsOpen[5].vt = VT_BSTR;
vArgsOpen[5].bstrVal = bstrOpenFile;
vArgsOpen[4] = vFalse;
vArgsOpen[3] = vTrue;
vArgsOpen[2] = vFalse;
vArgsOpen[1].vt = VT_BSTR;
vArgsOpen[1].bstrVal = m_bstrEmptyString;
vArgsOpen[0].vt = VT_BSTR;
vArgsOpen[0].bstrVal = m_bstrEmptyString;
//Execute open method
m_hr=m_pDispDocs->Invoke(m_dispid_Open,IID_NULL,LOCALE_USER_DEFAULT,
DISPATCH_METHOD,&dpOpen,NULL,NULL,NULL);
//Query activedocument interface
m_szFunction = OLESTR("ActiveDocument");
m_hr = m_pDispApp->GetIDsOfNames(IID_NULL, &m_szFunction, 1,
LOCALE_USER_DEFAULT,&m_dispid_ActiveDoc);
m_hr = m_pDispApp->Invoke(m_dispid_ActiveDoc,IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET,
&m_dpNoArgs,&m_vResult,NULL,NULL);
m_pDispActiveDoc = m_vResult.pdispVal;
//Prepare arguments for save as .txt
VARIANT vArgsSaveAs[11];
DISPPARAMS dpSaveAs;
dpSaveAs.cArgs = 11;
dpSaveAs.cNamedArgs = 0;
dpSaveAs.rgvarg = vArgsSaveAs;
vArgsSaveAs[10].vt = VT_BSTR;
vArgsSaveAs[10].bstrVal = bstrSaveFile; //Filename
vArgsSaveAs[9].vt = VT_I4;
vArgsSaveAs[9].lVal = 7;//wdFormatUnicodeText;// 7;//2; //FileFormat(wdFormatText = 2)
vArgsSaveAs[8] = vFalse; //LockComments
vArgsSaveAs[7].vt = VT_BSTR;
vArgsSaveAs[7].bstrVal = m_bstrEmptyString; //Password
vArgsSaveAs[6].vt = VT_BOOL;
vArgsSaveAs[6].boolVal = TRUE; //AddToRecentFiles
vArgsSaveAs[5].vt = VT_BSTR;
vArgsSaveAs[5].bstrVal = m_bstrEmptyString; //WritePassword
vArgsSaveAs[4] = vFalse; //ReadOnlyRecommended
vArgsSaveAs[3] = vFalse; //EmbedTrueTypeFonts
vArgsSaveAs[2] = vFalse; //SaveNativePictureFormat
vArgsSaveAs[1] = vFalse; //SaveFormsData
vArgsSaveAs[0] = vFalse; //SaveAsOCELetter
// Query and execute SaveAs method
m_szFunction = OLESTR("SaveAs");
m_hr = m_pDispActiveDoc->GetIDsOfNames(IID_NULL,&m_szFunction,1,
LOCALE_USER_DEFAULT,&m_dispid_SaveAs);
m_hr = m_pDispActiveDoc->Invoke(m_dispid_SaveAs, IID_NULL,
LOCALE_USER_DEFAULT,DISPATCH_METHOD,
&dpSaveAs,NULL,NULL,NULL);
// Invoke the Quit method
m_szFunction = OLESTR("Quit");
m_hr = m_pDispApp->GetIDsOfNames(IID_NULL, &m_szFunction, 1,
LOCALE_USER_DEFAULT, &m_dispid_Quit);
m_hr = m_pDispApp->Invoke(m_dispid_Quit, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_METHOD, &m_dpNoArgs, NULL, NULL, NULL);
//
cout << m_bstrEmptyString << endl;
cout << (char*)m_bstrEmptyString << endl;
//Clean-up
::SysFreeString(bstrOpenFile) ;
::SysFreeString(bstrSaveFile) ;
::SysFreeString(m_bstrEmptyString) ;
m_pDispActiveDoc->Release();
m_pDispDocs->Release();
m_pDispApp->Release();
m_pUnk->Release();
::CoUninitialize();
}
void DocToTxt(BSTR bstrOpenFile, BSTR bstrSaveFile)
{
// ******************* Declare Some Variables ********************
// Variables that will be used and re-used in our calls
DISPPARAMS m_dpNoArgs = {NULL, NULL, 0, 0};
VARIANT m_vResult;
OLECHAR FAR* m_szFunction;
// IDispatch pointers for Word's objects
IDispatch* m_pDispDocs; //Documents collection
IDispatch* m_pDispActiveDoc; //ActiveDocument object
// DISPID's
DISPID m_dispid_Docs; //Documents property of Application object
DISPID m_dispid_ActiveDoc; //ActiveDocument property of Application
DISPID m_dispid_SaveAs; //SaveAs method of the Document object
DISPID m_dispid_Quit; //Quit method of the Application object
DISPID m_dispid_Open; //Open method of the Application object
BSTR m_bstrEmptyString ;
// ******************** Start Automation ***********************
//Initialize the COM libraries
::CoInitialize(NULL);
// Create an instance of the Word application and obtain the pointer
// to the application's IDispatch interface.
CLSID m_clsid;
CLSIDFromProgID(L"Word.Application.12", &m_clsid);
IUnknown* m_pUnk;
HRESULT m_hr = ::CoCreateInstance( m_clsid, NULL, CLSCTX_SERVER,
IID_IUnknown, (void**) &m_pUnk);
IDispatch* m_pDispApp;
m_hr = m_pUnk->QueryInterface( IID_IDispatch, (void**)&m_pDispApp);
// Get IDispatch* for the Documents collection object
m_szFunction = OLESTR("Documents");
m_hr = m_pDispApp->GetIDsOfNames (IID_NULL, &m_szFunction, 1,
LOCALE_USER_DEFAULT, &m_dispid_Docs);
m_hr = m_pDispApp->Invoke (m_dispid_Docs, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_PROPERTYGET, &m_dpNoArgs, &m_vResult,
NULL, NULL);
m_pDispDocs = m_vResult.pdispVal;
// Query id of method open
m_szFunction = OLESTR("Open");
m_hr = m_pDispDocs->GetIDsOfNames(IID_NULL, &m_szFunction,1,
LOCALE_USER_DEFAULT, &m_dispid_Open);
// Prepare parameters for open method
VARIANT vArgsOpen[6];
DISPPARAMS dpOpen;
dpOpen.cArgs = 6;
dpOpen.cNamedArgs = 0;
dpOpen.rgvarg = vArgsOpen;
VARIANT vFalse, vTrue;
vFalse.vt = VT_BOOL;
vFalse.boolVal = FALSE;
vTrue.vt = VT_BOOL;
vTrue.boolVal = TRUE;
m_bstrEmptyString = ::SysAllocString(OLESTR("")) ;
vArgsOpen[5].vt = VT_BSTR;
vArgsOpen[5].bstrVal = bstrOpenFile;
vArgsOpen[4] = vFalse;
vArgsOpen[3] = vTrue;
vArgsOpen[2] = vFalse;
vArgsOpen[1].vt = VT_BSTR;
vArgsOpen[1].bstrVal = m_bstrEmptyString;
vArgsOpen[0].vt = VT_BSTR;
vArgsOpen[0].bstrVal = m_bstrEmptyString;
//Execute open method
m_hr=m_pDispDocs->Invoke(m_dispid_Open,IID_NULL,LOCALE_USER_DEFAULT,
DISPATCH_METHOD,&dpOpen,NULL,NULL,NULL);
//Query activedocument interface
m_szFunction = OLESTR("ActiveDocument");
m_hr = m_pDispApp->GetIDsOfNames(IID_NULL, &m_szFunction, 1,
LOCALE_USER_DEFAULT,&m_dispid_ActiveDoc);
m_hr = m_pDispApp->Invoke(m_dispid_ActiveDoc,IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET,
&m_dpNoArgs,&m_vResult,NULL,NULL);
m_pDispActiveDoc = m_vResult.pdispVal;
//Prepare arguments for save as .txt
VARIANT vArgsSaveAs[11];
DISPPARAMS dpSaveAs;
dpSaveAs.cArgs = 11;
dpSaveAs.cNamedArgs = 0;
dpSaveAs.rgvarg = vArgsSaveAs;
vArgsSaveAs[10].vt = VT_BSTR;
vArgsSaveAs[10].bstrVal = bstrSaveFile; //Filename
vArgsSaveAs[9].vt = VT_I4;
vArgsSaveAs[9].lVal = 7;//wdFormatUnicodeText;// 7;//2; //FileFormat(wdFormatText = 2)
vArgsSaveAs[8] = vFalse; //LockComments
vArgsSaveAs[7].vt = VT_BSTR;
vArgsSaveAs[7].bstrVal = m_bstrEmptyString; //Password
vArgsSaveAs[6].vt = VT_BOOL;
vArgsSaveAs[6].boolVal = TRUE; //AddToRecentFiles
vArgsSaveAs[5].vt = VT_BSTR;
vArgsSaveAs[5].bstrVal = m_bstrEmptyString; //WritePassword
vArgsSaveAs[4] = vFalse; //ReadOnlyRecommended
vArgsSaveAs[3] = vFalse; //EmbedTrueTypeFonts
vArgsSaveAs[2] = vFalse; //SaveNativePictureFormat
vArgsSaveAs[1] = vFalse; //SaveFormsData
vArgsSaveAs[0] = vFalse; //SaveAsOCELetter
// Query and execute SaveAs method
m_szFunction = OLESTR("SaveAs");
m_hr = m_pDispActiveDoc->GetIDsOfNames(IID_NULL,&m_szFunction,1,
LOCALE_USER_DEFAULT,&m_dispid_SaveAs);
m_hr = m_pDispActiveDoc->Invoke(m_dispid_SaveAs, IID_NULL,
LOCALE_USER_DEFAULT,DISPATCH_METHOD,
&dpSaveAs,NULL,NULL,NULL);
// Invoke the Quit method
m_szFunction = OLESTR("Quit");
m_hr = m_pDispApp->GetIDsOfNames(IID_NULL, &m_szFunction, 1,
LOCALE_USER_DEFAULT, &m_dispid_Quit);
m_hr = m_pDispApp->Invoke(m_dispid_Quit, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_METHOD, &m_dpNoArgs, NULL, NULL, NULL);
//
cout << m_bstrEmptyString << endl;
cout << (char*)m_bstrEmptyString << endl;
//Clean-up
::SysFreeString(bstrOpenFile) ;
::SysFreeString(bstrSaveFile) ;
::SysFreeString(m_bstrEmptyString) ;
m_pDispActiveDoc->Release();
m_pDispDocs->Release();
m_pDispApp->Release();
m_pUnk->Release();
::CoUninitialize();
}
#2
office不是提供编程接口了吗
#3
调用
DocToTxt(::SysAllocString(OLESTR("D:\\09\\10\\from.doc")), ::SysAllocString(OLESTR("D:\\09\\10\\to.txt")));
你说的是添加word.h word.cpp文件的那个方法吗,那个方法还没有用过
DocToTxt(::SysAllocString(OLESTR("D:\\09\\10\\from.doc")), ::SysAllocString(OLESTR("D:\\09\\10\\to.txt")));
你说的是添加word.h word.cpp文件的那个方法吗,那个方法还没有用过
#4
请问怎么用?
#5
博主可以参考一下这个博客的文章
http://blog.csdn.net/yangsh3002/article/details/3547297#comments
另外,我也很想知道,如何通过COM获取word文本。
上面那个贴子只讲到写入word文本
http://blog.csdn.net/yangsh3002/article/details/3547297#comments
另外,我也很想知道,如何通过COM获取word文本。
上面那个贴子只讲到写入word文本
#6
同求,关注!
#7
这是我写的,看看这样行不行
::OleInitialize (NULL);
// TODO: 在此添加控件通知处理程序代码
IStorage* pStorage=NULL;
IStream* pStream=NULL;
//检测是否是复合文档
HRESULT hResult=::StgIsStorageFile(TEXT("E:\\资料集锦\\思想汇报\\try.doc"));
if(FAILED(hResult))
{
MessageBox(L"不是复合文档,i am sorry",0);
return;
}
//打开复合文档
hResult=::StgOpenStorage(TEXT("E:\\资料集锦\\思想汇报\\try.doc"),NULL, STGM_READWRITE | STGM_SHARE_EXCLUSIVE ,NULL,0,&pStorage);
//枚举复合文档
IEnumSTATSTG *iEnum=NULL;
hResult=pStorage->EnumElements(0,NULL,0,&iEnum);
if(FAILED(hResult))
{
MessageBox(L"枚举器获取失败,so i am sorry",0,0);
return;
}
STATSTG stg={0};
//怎么样才能给WordDocument
while(NOERROR==iEnum->Next(1,&stg,NULL))
{
if(STGTY_STREAM==stg.type)
{
MessageBox(stg.pwcsName,L"istream",0);
}
else if(STGTY_STORAGE==stg.type )
{
MessageBox(L"STORAGE",0,0);
}
::CoTaskMemFree(stg.pwcsName);
}
hResult=pStorage->OpenStream(TEXT("WordDocument"),0,STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE,0,&pStream);
//CComVariant var(_T("dsada"));
// hResult=pStream->Write(var.bstrVal,var.GetSize(),NULL); 这里的写操作总是出问题,读的话还行
pStorage->Release();
pStream->Release();
::OleUninitialize ();
#8
msaa试试。我没试过,不过理论上应该是行的通的。
#9
//MSAA入门参考此文章
//http://www.vckbase.com/index.php/wv/651
//http://msdn.microsoft.com/zh-cn/magazine/cc301312(en-us).aspx
//代码引用自此处
//http://msdn.microsoft.com/zh-cn/magazine/bb984889(en-us).aspx
//http://www.vckbase.com/index.php/wv/651
//http://msdn.microsoft.com/zh-cn/magazine/cc301312(en-us).aspx
//代码引用自此处
//http://msdn.microsoft.com/zh-cn/magazine/bb984889(en-us).aspx
#10
多谢解答,
这个问题我当时用了2天的时间解决了,方法写在了博客中
http://blog.csdn.net/aaajj/article/details/8800414
那几天耽误了时间,无暇顾及我的投资,结果造成了重大的损失,现在我已跌落深渊,伤痕累累
这个解决方案对我已经没有任何意义了,希望可以给有需要的朋友一些帮助
这个问题我当时用了2天的时间解决了,方法写在了博客中
http://blog.csdn.net/aaajj/article/details/8800414
那几天耽误了时间,无暇顾及我的投资,结果造成了重大的损失,现在我已跌落深渊,伤痕累累
这个解决方案对我已经没有任何意义了,希望可以给有需要的朋友一些帮助