将格式化文本从Word文档中的表格中的单元格复制到RichEdit

时间:2012-08-27 01:50:31

标签: delphi ms-word cell ole trichedit

我使用OLE自动化来处理Word文档。 我可以使用

获取单元格的内容

Table.Cell(rowIndex,colIndex).Range.FormattedText

它返回OleVariant。 我不确定我是否使用了正确的属性,并且不知道如何在TRichEdit中粘贴此文本而不会丢失格式化(例如上标文本)

2 个答案:

答案 0 :(得分:4)

我设置了一个模拟表单,上面只有一个richedit和一个按钮。下面的代码可能不是获得此代码的最佳方法,但它适用于Win XP上的Word 2007。

uses  Word_TLB;

procedure TForm1.Button1Click(Sender: TObject);
var
  wordApp : _Application;
  doc : WordDocument;
  table : Word_TLB.Table;
  filename : OleVariant;
  aRange : Range;
  aWdUnits : OleVariant;
  count : OleVariant;
begin
  //need to back up 2 characters from range object to exclude table border.
  //Remove 1 character only if using selection
  count := -2;        
  aWdUnits := wdCharacter;
  filename := '"H:\Documents and Settings\HH\My Documents\testing.docx"';
  RichEdit1.Clear;
  try
    wordApp := CoWordApplication.Create;
    wordApp.visible := False;

    doc := wordApp.documents.open( filename, emptyparam,emptyparam,emptyparam,
      emptyparam,emptyparam,emptyparam,emptyparam,
      emptyparam,emptyparam,emptyparam,emptyparam,
      emptyparam,emptyparam,emptyparam,emptyparam );

    table := doc.tables.item(1);
    aRange := table.cell(3,1).Range;
    aRange.MoveEnd(aWdUnits, count); //This is needed so border is not included
    aRange.Copy;
    RichEdit1.PasteFromClipboard;
    RichEdit1.Lines.Add('');

  finally
    wordApp.quit(EmptyParam, EmptyParam, EmptyParam);
  end;
end;

而且,这是结果: The result screen dump. The background is the word document having a table, and the forground is the delphi form with a richedit

唯一的问题是多行文字在richedit中显示为一行。

答案 1 :(得分:0)

我放弃了使用OLE Automation解决这个问题。 TRichView提供了所需的功能,但它不是免费的......