How to Add data to MS word

I want to add some data in MSword via Delphi application. There are lots of examples available on the internet, but they are very complex. Can someone help me with a simple function that accept string parameter and then append that string in a word document and save that document to the desktop

Answers


Below is the simplest example of creating a Word document using Delphi that I can think of. I am providing this simply to get you started, so that you can start experimenting from an example that works. Try it, then read that tutorial I gave you the link to - I am not going to extend this answer into a tutorial.

When you start reading about automation in Delphi, you'll find that there are two ways of doing it, "early binding", where you use classes which are derived by importing the automation objects "Type Library", and "Late Binding", where you use Variants, rather than class instances, to represent the automation objects. The example below uses late binding, because it's simplest for a basic example. Note that the special named-parameter syntax (FileName := xxx) in the argument to SaveAs

  Document.SaveAs(FileName := AFileName)

was added to Delphi to support late binding of automation objects.

Apart from tutorials like Deborah Pate's, another way to learn the basics of Delpi automation of Word is to record a Word macro that does what you want and then examine the macro's code in Word's VB editor (on the Developer tab of Word's ribbon) - usually, it's straightforward to translate the code into Delphi.

unit SimpleWordu;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    procedure CreateWordDoc;
  protected
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  CreateWordDoc;
end;

procedure TForm1.CreateWordDoc;
var
  UserName,
  AFileName : String;
  DocText : String;
  MSWord,
  Document : OleVariant;
begin
  MSWord := CreateOleObject('Word.Application');
  MSWord.Visible := True;
  Document := MSWord.Documents.Add;

  DocText := 'Hello Word!';
  MSWord.Selection.TypeText(DocText);

  UserName := 'MA';
  AFileName := Format('c:\users\%s\desktop\worddoc.doc', [UserName]);

  Document.SaveAs(FileName := AFileName);
end;

end.

Need Your Help

When I create a table in iOS7, the label in the cell appears to be much smaller than the cell

iphone objective-c uitableview ios7 uilabel

I have an iPhone app which i'm currently testing with iOS7. I've noticed a few changes such as iOS7 not taking into account nav bars and tab bars when giving the screen size and I've accounted for ...