2

I use OLE Automation to work with Word document. I can get the content of the cell using

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

it returns 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 (e.g. superscripted text)

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Alexander
  • 153
  • 1
  • 2
  • 8
  • MS word is using RTF, and the RTF features is changing overtime. Delphi TRichedit has limited support for RTF, and I think it does not support word's table. I don't know what kind of formatting features you need, but keep in mind that TRichedit may not give the results you need (i.e. advance formatting may be lost or give weird results). An alternative is by using TRichedit replacement component. – Hendra Aug 27 '12 at 03:23
  • Ooops...correction, the above should read ...and I think it has limited support for word's table. BTW, I assumed the richedit is version 4.1. – Hendra Aug 27 '12 at 03:32
  • Actually, I do not need it to support word's table. I need to read each cell separately and then store or save it's content as rich text without losing text formating. There is no advance formatting incide cells, just subscript, superscript, bold, italic and that's about all. Efficiency does not play a part. – Alexander Aug 27 '12 at 13:04

2 Answers2

4

I set up a mock up form with just a richedit and a button on it. The code below may not the best way to achive this, but it works with Word 2007 on Win XP.

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: The result screen dump. The background is the word document having a table, and the forground is the delphi form with a richedit .

The only thing is the multiline text appeared as a single line in the richedit.

Hendra
  • 720
  • 4
  • 8
  • As you can see in your own screenshot, you did cut off a character at the end. So, you have to set `count` to -1 and not -2. Also, for Word 2010 on Windows 7, I had to change following in the code: 1) Change type of `aRange` from `Range` to `WordRange` and 2) Insert following line between your `aRange.MoveEnd` and `aRange.Copy` code: `wordApp.Selection.Range.SetRange(aRange.Start, aRange.End_);` 3) Optionally, I've defined the 3rd argument of `wordApp.documents.open` as an `OleVariant` variable `readonly` which I set to true. This prevents problems when the document is opened elsewhere – Daniel Marschall Aug 19 '16 at 10:15
0

I gave up solving this problem with OLE Automation. TRichView gives desired functionality, but it's not free...

Alexander
  • 153
  • 1
  • 2
  • 8