简体   繁体   English

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

[英]Copy formated text from a cell in a Table in Word document to TRichEdit

I use OLE Automation to work with Word document. 我使用OLE自动化来处理Word文档。 I can get the content of the cell using 我可以使用获取单元格的内容

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

it returns OleVariant. 它返回OleVariant。 I'm not sure if I'm using right property and have no idea how to paste this text in TRichEdit without losing formating (eg superscripted text) 我不确定我是否使用正确的属性,并且不知道如何在不丢失格式的情况下将其粘贴到TRichEdit中(例如,上标的文本)

I set up a mock up form with just a richedit and a button on it. 我建立了一个只有Richedit和一个按钮的模拟表格。 The code below may not the best way to achive this, but it works with Word 2007 on Win XP. 下面的代码可能不是实现此目的的最佳方法,但是它可以与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;

And, this is the result: 而且,这是结果: 结果屏幕转储。背景是具有表格的单词文档,而前景是带有Richedit的delphi格式 .

The only thing is the multiline text appeared as a single line in the richedit. 唯一的事情是多行文本在richedit中显示为单行。

I gave up solving this problem with OLE Automation. 我放弃了使用OLE Automation解决此问题的方法。 TRichView gives desired functionality, but it's not free... TRichView提供了所需的功能,但它不是免费的...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM