Solved

Delphi and MS Word

Posted on 2006-11-15
5
187 Views
Last Modified: 2010-04-05
Hi all,

I need to insert lines, tabs and spaces in a word doc.
I want to do that using TWordApllication component.
Example:

    Line1
        Line2
            Line3

   Line4
   Line5

and so on...

regards
0
Comment
Question by:paulo_psa
5 Comments
 
LVL 19

Accepted Solution

by:
Limbeck earned 63 total points
ID: 17953940
0
 
LVL 9

Assisted Solution

by:bernani
bernani earned 62 total points
ID: 17960604
Hi,

You can use this code which will open Word, create a new doc, insert lines and tabs and finally show word with a new document containing this result:


Line 1
      Line 2
            Line 3
Line 4
Line 5



unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ComObj, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
   
    procedure Button1Click(Sender: TObject);
  private
    { Déclarations privées }
  public
    { Déclarations publiques }
  end;

var
  Form1            : TForm1;
  Word             : OLEVariant;
  Sel, Doc         : Variant;

const
vbTab = Chr(9);

implementation

{$R *.DFM}

procedure Return;
begin
Sel.TypeParagraph;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  try
  Word := CreateOleObject('Word.Application');
  except
  ShowMessage('Cannot start Word !');
  Exit;
  end;

// Word will only be visible when the text is inserted
Doc:=Word.Documents.Add;
Sel:=Word.Selection;

Sel.Font.Name     := 'Times New Roman';
Sel.Font.Size     := 12;
Sel.ParagraphFormat.Alignment := 0; //left aligned

Sel.TypeText('Line 1');
Return;
Sel.TypeText(Text := vbtab + 'Line 2');
Return;
Sel.TypeText(Text := vbtab + vbtab + 'Line 3');
Return;
Sel.TypeText(Text := 'Line 4');
Return;
Sel.TypeText(Text := 'Line 5');
Return;

Word.Visible := True;

end;

end.

_____________________________

Hope this help

PS:

Try to have a look at those pages http://www.djpate.freeserve.co.uk/Automation.htm

You got good infos how to use the compos TWordApplication and others.

See the provided demo: http://www.djpate.freeserve.co.uk/D5Word.zip.

0
 

Expert Comment

by:landscapeEco
ID: 17979353
I think this should be the accepted answer
0
 
LVL 1

Expert Comment

by:Computer101
ID: 21133166
Forced accept.

Computer101
EE Admin
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Making delphi communicate with a c# service 16 103
Performance of SQL statement 37 111
TEMBEDDEDWB how can i change its user agent ? 8 58
enhance the following code 3 32
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question