Solved

Delphi and MS Word

Posted on 2006-11-15
5
196 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month8 days, 3 hours left to enroll

617 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