各位大人:
现在有这样一个需求,delphi项目中需要用到word的打印功能。打印格式已经定制好,相应的地方要从delphi录入到Access数据库中抽取。
各位有好的方案没有,很急,请大家不吝赐教!

解决方案 »

  1.   

    再相应的地方设置标签,定位书签,写下数据库里的字段~代码如下:
      try
       wApp:=GetActiveOleObject('word.Application');
      except
       wApp:=CreateOleObject('word.Application');
      end;
    wApp.Selection.GOTO(What:=wdGoToBook,Name:='书签名称');//定位
    wApp.Selection.typetext(数据库内容);
      

  2.   

    把需要的数据导出到word,直接打印了
      

  3.   

    首先,建立打印word模板,要在相应位置添加标签,以备导出数据所用.
    其次,从数据库中读数,插到对应的标签位置上.
    你所说的 :如果从库里读出多条记录填到表格里就不知道怎么弄了
    很简单就是画表格:
    代码如下:
    procedure CECR(str_id:string;id:string;folderName:string;templateName:string);   
    var
      vWord,vDoc: Variant;
      i,x,y,z:Integer;
    begin
      vWord := CreateOleObject('Word.Application') ;//创建Word线程
      try
        vDoc := vWord.Documents.Open(folderName+'word模板.doc');      with DataModule1.ADOQUBGZL do
          begin
            close;
            sql.Clear;
            sql.Add('select * from Report where CrediteyesNO='+''''+str_id+'''');
            open;
          end;    vDoc.Books.Item('注册资本').Range.InsertAfter(DataModule1.ADOQUBGZL.FieldByName('注册资本').AsString+DataModule1.ADOQUBGZL.FieldByName('注册资本单位').AsString);//向相应标签位置插数
           with DataModule1.ADOQuery1 do
          begin
            close;
            sql.Clear;
            sql.Add('select CrediteyesNO, 进出口方式, 获得进出口权时间, 使用代理机构, 删除标记 from EN_进出口方式 where CrediteyesNO='+''''+str_id+'''');
            open;
            x:=RecordCount;
            y:=Fields.Count-2;
          end;
        vdoc.tables.Add(vDoc.Books.Item('进出口方式').Range,x,y);   // 在标签位置建表(x,y 代表行列)    for i:=1 to y do
        begin
          vDoc.Tables.Item(1).Cell(1,i).range.Paragraphs.Alignment:= wdAlignParagraphleft;
          vDoc.Tables.Item(1).Cell(1,i).range.text:=DataModule1.ADOQuery1.Fields[i].FieldName;//第一行标题
        end;    with DataModule1.ADOQuery1 do//其他行
        begin
          z:=1;
          First ;
          while not Eof do
          begin
            z := z + 1 ;
            for i := 1 to y do
            begin
            vDoc.Tables.Item(1).Cell(z,i).range.Paragraphs.Alignment:= wdAlignParagraphleft;
            vDoc.Tables.Item(1).Cell(z,i).Range.InsertAfter(DataModule1.ADOQuery1.Fields[i].Asstring);
            end;
            Next;
          end;
        end;// vDoc.Tables.Item(1).Cell(z,i)中的Item(1)代表模板中对应的表格数
       
        finally
        vdoc.saveas(templateName+'样本.doc');
        vDoc.Close(True) ; //关闭文并保存
        vWord.Quit(False) ; //退出Word
      end ;
    end;
      

  4.   

    folderName,templateName 分别代表打开和存取模板的路径folderName:=Extractfilepath(application.ExeName)+'word\';
    templateName:=Extractfilepath(application.ExeName)+'report\';
      

  5.   

    aiqikong(艾琦) 谢谢你:)
    再问你一个问题:
    我在word中录制了一个VBA代码如下:
        Selection.Range.ModifyEnclosure Style:=wdEncloseStyleSmall, Symbol:= _
            wdEnclosureSquare, EnclosedText:="√"
    代码意思是输出打勾的方框!在delphi中怎么实现??
      

  6.   

    我把代码在delphi中转换成如下相应的代码:
      WordApp.Selection.Range.ModifyEnclosure.Style := wdEncloseStyleSmall;
      WordApp.Selection.typeText('√');
    可是运行到第一句时,出现“非选择性错误”!有谁明白是怎么回事吗?
      

  7.   

    呵呵,这个自己搞定了!贴出来大家共享一下:)
      wordApp.Selection.Range.ModifyEnclosure(wdEncloseStyleSmall,wdEnclosureSquare, '√');