Solved

Delphi and MS Word

Posted on 2006-11-15
5
183 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
Comment Utility
0
 
LVL 9

Assisted Solution

by:bernani
bernani earned 62 total points
Comment Utility
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
Comment Utility
I think this should be the accepted answer
0
 
LVL 1

Expert Comment

by:Computer101
Comment Utility
Forced accept.

Computer101
EE Admin
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now