gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:00:13 26 4
gpt4 key购买 nike

我使用 OLE 自动化来处理 Word 文档。我可以使用获取单元格的内容

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

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

最佳答案

我设置了一个模拟表单,上面只有一个 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 中显示为单行。

关于delphi - 将格式化文本从 Word 文档表格的单元格复制到 RichEdit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12135448/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com