We help IT Professionals succeed at work.

OLE Automation with word

hulken
hulken asked
on
Medium Priority
422 Views
Last Modified: 2010-04-05
Hello

I'm building a table inside a word document. One column should contain formated text- (I have it in rich text format)

Can anyone tell me how to do this in a better way than I'm doing now.

Currently I create the table with ConvertToTable
and loop through all rich texts and copy to clipboard and paste into the column in the table.

Working with tables are awful slow. I would prefere to use merge field or something like that but can't figure out how to feed them  with RICH text.
Comment
Watch Question

Author

Commented:
And how do I put it to the correct destination? (IE a merge field, or a column)
Hi, I usual use Word tables like that:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, OleServer, WordXP;

type
  TForm1 = class(TForm)
    WordApplication1: TWordApplication;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
    val:OleVariant;
    row,col,i,j:Integer;
    t:Table;
    c:Cell;
begin
    row:=50;
    col:=6;
    val:=true;
    WordApplication1.Documents.Add(EmptyParam,EmptyParam,EmptyParam,val);
    with WordApplication1.ActiveDocument do
    begin
        Content.Text:='Test';
        val:='D:\\123.doc';
        Tables.Add(Content,row,col,EmptyParam,EmptyParam);
        t:=Tables.Item(1);
        for i:=1 to t.Rows.Count do
            for j:=1 to t.Columns.Count do
            begin
                c:=t.Cell(i,j);
                c.Range.Text:=IntToStr(i)+':'+IntToStr(j);
            end;
        SaveAs(val,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam);
    end
end;

end.

This code processing about 2 seconds on my PC. Not very slow for me.
Any questions?

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
However, if you want to RTF file on out - just set:
val:='D:\\123.rtf';
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.