用C++ Builder处理 word 的方法 ( 积分: 0 )

  • 主题发起人 主题发起人 唐宗
  • 开始时间 开始时间

唐宗

Unregistered / Unconfirmed
GUEST, unregistred user!
以下是我做的一段代码,主要用于用CBC控制写入WORD编制合同,发给大家共亨,也望大家不断的向里面加入内容.
代码:
#include <vcl.h>
#pragma hdrstop
#include &quot;oleWORD.h&quot;
#include <comobj.hpp>
#include <dir.h>
#include <utilcls.h>
#include &quot;Word_2k_SRVR.h&quot;
//加入这两个,否则在调用如wdSeekCurrentPageFooter参数时会报错
#include &quot;Word_2k.h&quot;

#define OPG OlePropertyGet   //命令太长了,简化一下
#define OPS OlePropertySet
#define OPR OleProcedure
#define OFN OleFunction
#pragma link &quot;Word_2K_SRVR&quot;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
TForm1 *Form1;
Variant WordApp,WordDocs,WordDoc,WordRange,WordTable;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
char curdir[255];
current_directory(curdir);
AnsiString strFullFileName(curdir);
strFullFileName = strFullFileName + &quot;//doc//contract.doc&quot;;
try
  {
  WordApp=GetActiveOleObject(&quot;Word.Application&quot;);
  }
catch(...)
  {
  try
    {
    WordApp=Variant::CreateObject(&quot;Word.Application&quot;);
 //建立WORD的OLE对象
    }
  catch(...)
     {
     Application->MessageBoxA(&quot;无法启动Word&quot;,&quot;错误提示!&quot;);
      return;
      }
   }
  WordApp.OlePropertySet(&quot;Visible&quot;,true);
           //显示WORD程序界面
  WordApp.OPG(&quot;Documents&quot;).OPR(&quot;Open&quot;,strFullFileName.c_str());
//打开文档,注意后面的文件要转为字符串
  Variant WordActiveWindow=WordApp.OPG(&quot;ActiveWindow&quot;);
  Variant WordActivePane  =WordActiveWindow.OPG(&quot;ActivePane&quot;);
  Variant WordView= WordActivePane.OPG(&quot;View&quot;);
  WordView.OPS(&quot;SeekView&quot;,wdSeekCurrentPageFooter);
  //选中文档的页脚
  Variant WordSelection=WordApp.OPG(&quot;Selection&quot;);
  WordSelection.OPR(&quot;TypeText&quot;,DateToStr(Date()).c_str());
  Variant WordFormat=WordSelection.OPG(&quot;ParagraphFormat&quot;);
  WordFormat.OPS(&quot;Alignment&quot;,wdAlignParagraphCenter);
  //在文档中的页脚中插入日期
  WordView.OPS(&quot;SeekView&quot;,wdSeekMainDocument);
  //在WORD中将BOOKMARK替换成数据表中的内容
  //Selection.GoTo What:=wdGoToBookmark, Name:=&quot;GHDW&quot;
这句是WORD中的宏代码,为了对比和C++的不同,GHDW是WORD中的标签名
  try{
  Variant xx=WordSelection.Exec(Procedure(&quot;GoTo&quot;) << (int)wdGoToBookmark <<0 << 0 << &quot;GHDW&quot;);
    WordSelection.OPR(&quot;TypeText&quot;,&quot;中国&quot;);
    }
    catch(...)
    {
    Application->MessageBoxA(&quot;标签不存在!&quot;,&quot;错误提示!&quot;);
    }
  //在WORD中定位到第二个表格
  // Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Count:=2, Name:=&quot;&quot;
只要是GOTO,使用的参数和命令基本是一样的,同样可用于其他元素的查找.
  WordSelection.Exec(Procedure(&quot;GoTo&quot;) << (int)wdGoToTable <<(int)wdGoToFirst << 2 << &quot;&quot;);
   // if(!WordApp.IsEmpty())
  //{
  //  WordApp.OlePropertyGet(&quot;Application&quot;).OleProcedure(&quot;Quit&quot;);
   // WordApp = Unassigned;
  //}
}
//---------------------------------------------------------------------------
char __fastcall TForm1::current_directory(char *path)
{
  strcpy(path, &quot;X://&quot;);
     
  path[0] = 'A' + getdisk();
  
  getcurdir(0, path+3);
 
  return(*path);
}
 
后退
顶部