有了这个,可以替换掉word的菜单了:)也就是说,你可以完全不把word框架显示出来就可以完全实现WORD的功能了。
在View菜单中,选ClassWizard,选Automation选项卡,选Add Class,选择From a TypeLibrary, 在Office目录中选中Microsoft Word 97/2000 类型库Word8.olb或Word9.olb,选中Dialog,Dialogs,Selection。单击ok
调用word的对话框如下:
TRY{
LPDISPATCH lpDisp;
if (m_pSelection)
{
lpDisp = m_pSelection->GetIDispatch();
Documents docs;
_Application app;
_Document mydoc;
mydoc.AttachDispatch (lpDisp,TRUE);
app=mydoc.GetApplication ();
mydoc.Activate ();
Dialogs mydialogs;
Dialog mydialog;
mydialogs.AttachDispatch (app.GetDialogs (),TRUE);
mydialog.AttachDispatch (mydialogs.Item (112),TRUE);
/*112是查找替换*/
mydialog.Show (COleVariant((short)0));
mydialog.Execute ();
mydialog.DetachDispatch ();
}
}
CATCH(CException, e)
{
/*your pleaser to display error message*/
TCHAR errormsg[255];
e->GetErrorMessage (errormsg,255,NULL);
}
END_CATCH
return 0;
/*在WORD中录制新宏,输入Dialogs(这时会自动出现智能填充随便选择一个然后在其下右键单击定义。可以查看其详细信息。像新建文件是79,打开文件是80,页面设置是178 等等具体的吗?看字面意思了。再不成,就一个一个地试吧。把上面的112替换成你想要的就成了。show(COleVariant((short)0)就是一直显示,直到用户做出选择可以设成你想要的毫秒数。*/
下面是翻页的具体代码(带COPY&PASTE):
int CQsofficeView::adjustpage(int oldstartpage, int totalpages,int nowstartpage,BOOL downorup)
{
TRY{
LPDISPATCH lpDisp;
if (m_pSelection)
{
lpDisp = m_pSelection->GetIDispatch();
Documents docs;
_Application app;
_Document mydoc;
mydoc.AttachDispatch (lpDisp,TRUE);
app=mydoc.GetApplication ();
mydoc.Activate ();
Selection myselection;
myselection.AttachDispatch (app.GetSelection (),TRUE);
myselection.GoTo (COleVariant((short)1),COleVariant((short)2),COleVariant((short)0),COleVariant((short)oldstartpage));
myselection.MoveDown (COleVariant((short)7),COleVariant((short)totalpages),COleVariant((short)2));
myselection.Cut ();
myselection.GoTo (COleVariant((short)1),COleVariant((short)2),COleVariant((short)0),COleVariant((short)nowstartpage));
myselection.Paste ();
}
}
CATCH(CException, e)
{
/*your pleaser to display error message*/
TCHAR errormsg[255];
e->GetErrorMessage (errormsg,255,NULL);
}
END_CATCH
return 0;
}
实在太高兴了,所以又把源码给放上来了。
实现WORD功能技术上没什么难题了。以后不会有这种文档了。:)