我看了网上的资料大部分都是用ole的方式,把delphi的数据输出到word,但是出现如果把word给关了就会出现我写的软件出错,还有就是在delphi输出数据的时候改变word的光标位置或激活另一文档就会出现把数据输出到当前文档的光标之后,怎样才能不使word影响我的程序的稳定性!

解决方案 »

  1.   

    我以前做过用VB输出到Word的,用的不是OLE,个人不喜欢那个,你可以用Dcom的方式来做。在程序里建一个WORD的对象,然后对它进行操作
    MsWord:=CreateOleObject('Word.Application');
    具体的你可以再查一下相关的资料,网上很多的
      

  2.   

    在输出时,创建的Word为不可见,就可以解决这个问题。
    输出完毕时,你可以选择重新打开
      

  3.   

    我试过了,如果在输出数据到WORD过程中,再打开一个WORD文档就会出错!各位有更好的解决方法吗?
    我有见过人家做得很好,输出数据到WORD过程中不影响用户操作WORD,不会因为把光标位置改变而把数据输出到当前光标之后!
      

  4.   

    procedure TDiaryFunc.PrintDiaryList(Grd: TDBGridEh;slTitle: TStringList);
    var
      Count,i,j,iRangeEnd:integer;
      CurrColumn,RecordNum:integer;
      Doc: OleVariant;
      wTable,wRange: Variant;
      Curpos: TBook;
    begin
      if not Grd.DataSource.DataSet.Active then
      begin
        Exit;
      end;
      Curpos := Grd.DataSource.DataSet.GetBook;
      try
        Doc:=CreateOleObject('Word.Application');
        Doc.Visible := False;
        Doc.Documents.Add;
      except
        Exit
      end;
      Count:=Grd.Columns.Count - 1;
      CurrColumn:=0;
      for i:=0 to Count do
      begin
        if Grd.Columns.Items[i].Visible then
          Inc(CurrColumn);
      end;
      RecordNum:=Grd.DataSource.DataSet.RecordCount+1;  try
        Doc.Caption := '控制Word实例';
        Doc.ActiveDocument.PageSetup.Orientation := 1; //1表示横向排列
        iRangeEnd := Doc.ActiveDocument.Range.End - 1 ;
        Doc.ActiveDocument.Range(iRangeEnd,iRangeEnd).Select;
        Doc.Selection.ParagraphFormat.Reset;
        Doc.selection.Font.Name := '隶书';
        Doc.Selection.Font.Size := 20;
        Doc.ActiveDocument.Range.InsertAfter(slTitle[0]);
        Doc.Selection.ParagraphFormat.Alignment := 1; //1表示居中    Doc.ActiveDocument.Range.InsertAfter(#13);
        //设置换行后的字体
        iRangeEnd := Doc.ActiveDocument.Range.End - 1 ;
        Doc.ActiveDocument.Range(iRangeEnd,iRangeEnd).Select;
        Doc.Selection.ParagraphFormat.Reset;
        Doc.selection.Font.Name := '宋体';
        Doc.Selection.Font.Size := 10;
        Doc.ActiveDocument.Range.InsertAfter(slTitle[1] + #13);
        Doc.Selection.ParagraphFormat.Alignment := 1;
        iRangeEnd := Doc.ActiveDocument.Range.End - 1 ;
        Doc.ActiveDocument.Range(iRangeEnd,iRangeEnd).Select;
        Doc.ActiveDocument.Range.InsertAfter(slTitle[2]);
        Doc.Selection.ParagraphFormat.Alignment := 1;
      
        Doc.ActiveDocument.Range.InsertAfter(#13);
        iRangeEnd := Doc.ActiveDocument.Range.End - 1 ; //在文档末尾插入表格
        wRange := Doc.ActiveDocument.Range(iRangeEnd,iRangeEnd);
        wTable:=Doc.ActiveDocument.Tables.Add(wRange, RecordNum, CurrColumn);
        wTable.Columns.AutoFit;
        CurrColumn:=0;
        for i:=0 to Count do
        begin
          if Grd.Columns.Items[i].Visible then
          begin
            wTable.Cell(1,CurrColumn+1).Range.InsertAfter(Grd.Columns.Items[i].Title.Caption);
            wTable.Cell(1,CurrColumn+1).Range.Select;
            wTable.Cell(1,CurrColumn+1).Range.Font.Bold:=true;
            Inc(CurrColumn);
          end;
        end;
        j := 1;
        Grd.DataSource.DataSet.First;
        while not Grd.DataSource.DataSet.Eof do
        begin
          Inc(J);
          CurrColumn := 0;
          for i:=0 to Count do
          begin
            if Grd.Columns.Items[i].Visible then
            begin
              if Grd.Columns[i].Field.DisplayText = 'False' then
                wTable.Cell(j,CurrColumn+1).Range.InsertAfter('否')
              else if Grd.Columns[i].Field.DisplayText = 'True' then
                wTable.Cell(j,CurrColumn+1).Range.InsertAfter('是')
              else
                wTable.Cell(j,CurrColumn+1).Range.InsertAfter(
                  Grd.Columns[i].Field.DisplayText);
              Inc(CurrColumn);
            end;
          end;
          Grd.DataSource.DataSet.Next;
        end;
        Doc.ActiveWindow.ActivePane.View.SeekView := wdSeekCurrentPageHeader;
        Doc.Selection.TypeText(Text := '控制Word实例');
        Doc.ActiveWindow.ActivePane.View.SeekView := wdSeekCurrentPageFooter;
        Doc.Selection.TypeText(Text := ‘控制Word实例');
        Doc.ActiveWindow.ActivePane.View.SeekView:=wdSeekMainDocument;
      finally
        Grd.DataSource.DataSet.GotoBook(Curpos);
        Doc.Visible := True;
      end;
    end;试试我的函数,里面有移动光标的代码。
    是移动到文档的末尾,还有设置页眉和页脚的代码。
      

  5.   

    >>如果在输出数据到WORD过程中,再打开一个WORD文档就会出错!
    避免使用ActiveDocument,Selection这样的全局对象,而使用Documents(1),Range这样不会因用户操作而改变的对象