以文本方式查看主题

-  计算机科学论坛  (http://bbs.xml.org.cn/index.asp)
--  『 C/C++编程思想 』  (http://bbs.xml.org.cn/list.asp?boardid=61)
----  在VC中调用WORD(显示,修改,存盘,运行宏)  (http://bbs.xml.org.cn/dispbbs.asp?boardid=61&rootid=&id=37192)


--  作者:卷积内核
--  发布时间:8/25/2006 1:26:00 PM

--  在VC中调用WORD(显示,修改,存盘,运行宏)
在VC中调用WORD(显示,修改,存盘,运行宏)
  


(1)使用AppWizard创建一个新的MFC AppWizard(EXE)工程,命名为"office"  
(2)选择单文档视图(SDI)结构,在第3步中需要选中Container,以提供容器支持,并且选中active document container 其它都为默认

(3)在View菜单中,选ClassWizard,选Automation选项卡,选Add Class,选择From a TypeLibrary, 在Office目录中选中Microsoft Word 97/2000 类型库Word8.olb或Word9.olb,选中application,document,_document。单击ok

(4)给COfficeCntrItem添加一方法。GetIDispatch()

其源码如下:

  ASSERT_VALID(this);  
ASSERT(m_lpObject != NULL);   
LPUNKNOWN lpUnk = m_lpObject;   
Run();   
LPOLELINK lpOleLink = NULL;  
if (m_lpObject->QueryInterface(IID_IOleLink, (LPVOID FAR*)&lpOleLink) == NOERROR)   
{   
  ASSERT(lpOleLink != NULL);   
  lpUnk = NULL;       
  if (lpOleLink->GetBoundSource(&lpUnk) != NOERROR)    
  {    
   TRACE0("Warning: Link is not connected!/n");    
   lpOleLink->Release();    
   return NULL;    
  }   
  ASSERT(lpUnk != NULL);   
}    
LPDISPATCH lpDispatch = NULL;  
if (lpUnk->QueryInterface(IID_IDispatch, (LPVOID FAR*)&lpDispatch)!=NOERROR)   
{
    TRACE0("Warning: does not support IDispatch!/n");   
  return NULL;
  
}
ASSERT(lpDispatch != NULL);  
return lpDispatch;
(5)。在officeView.h添加#include "msword8.h"

(6)。修改void COfficeView::OnInsertObject(),源码如下:

BeginWaitCursor();

COfficeCntrItem* pItem = NULL;
TRY
{
  // Create new item connected to this document.
  COfficeDoc* pDoc = GetDocument();
  ASSERT_VALID(pDoc);
  pItem = new COfficeCntrItem(pDoc);
  ASSERT_VALID(pItem);

  // Initialize the item from the dialog data.
/* if (!dlg.CreateItem(pItem))
   AfxThrowMemoryException();  // any exception will do
  ASSERT_VALID(pItem);*/
   CLSID clsid; //
  if(FAILED(::CLSIDFromProgID(L"Word.document",&clsid)))
   AfxThrowMemoryException();
  
  if(!pItem->CreateNewItem(clsid))
  /*if(!pItem->CreateFromFile (filename,clsid)) */
   AfxThrowMemoryException();
  ASSERT_VALID(pItem);
  
  pItem->Activate (OLEIVERB_SHOW,this);
  

  ASSERT_VALID(pItem);
         m_pSelection = pItem;   // set selection to last inserted item
  pDoc->UpdateAllViews(NULL);
  // As an arbitrary user interface design, this sets the selection
  //  to the last item inserted.

  // TODO: reimplement selection as appropriate for your application

  m_pSelection = pItem;   // set selection to last inserted item
  pDoc->UpdateAllViews(NULL);
}
CATCH(CException, e)
{
  if (pItem != NULL)
  {
   ASSERT_VALID(pItem);
   pItem->Delete();
  }
  AfxMessageBox(IDP_FAILED_TO_CREATE);
}
END_CATCH

EndWaitCursor();

(7)重载ID—FILE—SAVE,

void COfficeView::OnFileSave()
{
// TODO: Add your command handler code here
  TRY{
     LPDISPATCH lpDisp;
  
  lpDisp = m_pSelection->GetIDispatch();

    Documents docs;
  
  _Application app;
  
  _Document mydoc;
  Documents my;

  mydoc.AttachDispatch (lpDisp,TRUE);
  app=mydoc.GetApplication ();
    /*   app.Run ("Macro3");*/
     mydoc.Activate ();
  BOOL password=mydoc.GetHasPassword ();
  mydoc.SetPassword ("love");
  password=mydoc.GetHasPassword ();  
  COleVariant vFalse((short)FALSE);
  mydoc.SaveAs (COleVariant("c://love.doc"),vFalse,vFalse, COleVariant(""),vFalse,
   COleVariant(""),vFalse,vFalse,vFalse,vFalse,vFalse);
  
}
CATCH(CException, e)
{
        }
END_CATCH
}

bulid,click insert object,and edit ,and save.maybe run macro.




--  作者:卷积内核
--  发布时间:8/25/2006 1:28:00 PM

--  
去掉word的常用,格式工具栏

在View菜单中,选ClassWizard,选Automation选项卡,选Add Class,选择From a TypeLibrary, 在Office目录中选中Microsoft Word 97/2000 类型库MSO9.DLL,选中_CommandBars,CommandBar。单击ok

在officeView.h添加#include "mso9.h"

添加方法:

int CQsofficeView::hidetoolbar(BOOL hide)
{
   TRY{
  LPDISPATCH lpDisp;   
  lpDisp = m_pSelection->GetIDispatch();   
  Documents docs;  
  _Application app;   
  _Document mydoc;
  _CommandBars mybars;
        CommandBar  mybar;
  mydoc.AttachDispatch (lpDisp,TRUE);
  app=mydoc.GetApplication ();       
  mydoc.Activate ();

  mybars.AttachDispatch (mydoc.GetCommandBars (),TRUE);
     mybar.AttachDispatch (mybars.GetItem (COleVariant(/*(short)1)*/"Standard")),TRUE);
  mybar.SetVisible (hide);
   mybar.AttachDispatch (mybars.GetItem (COleVariant(/*(short)2*/"Formatting")),TRUE);
  mybar.SetVisible (hide);
  CMDIFrameWnd * pwnd=(CMDIFrameWnd *)AfxGetMainWnd();
        pwnd->GetActiveFrame ()->UpdateWindow ();

}
CATCH(CException, e)
{
    TCHAR errormsg[255];
  e->GetErrorMessage (errormsg,255,NULL);
}
END_CATCH
return 0;
}

bulid,insert object后,便可以调用hidetoolbar了。

我的经验:先用WORD的宏录制功能录制好宏后,选择工具下宏下VB编辑器看它的源码然后再根据msword.h和mso*.h的相应类来实现。

在VC中调用WORD(显示,修改,存盘,运行宏)一在这儿。

http://www.csdn.net/develop/article/10/10570.shtm


--  作者:卷积内核
--  发布时间:8/25/2006 1:33:00 PM

--  
调用word的各种对话框翻页等

有了这个,可以替换掉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;

}



W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
7,285.156ms