Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 755
  • Last Modified:

Delphi and MS Word 2000

Hello,

I am working on a Delphi program that creates word document and writes to the document. I am using Delphi 5.0 and MS Word 2000 programs. I am able to create a blank document but not able to write anything to it .... Everytime I do, I get access violation.


PLEASEEEEEEEEEEEEE HELP

Sample code or actual code that I can use and practice on that writes to the document would be a great help thanks.
0
Digitalanolog1
Asked:
Digitalanolog1
1 Solution
 
geobulCommented:
Hi,

Try this:

uses ComObj;

procedure TForm1.Button1Click(Sender: TObject);
var
  WordApp, WordDoc: OLEVariant;
begin
 WordApp := CreateOleObject('Word.Application');
 WordApp.Visible := True; // show MS Word
 WordApp.Caption:='Winword From Delphi';
 // to open an existing doc
 // WordDoc:=WordApp.Documents.Open('Letter.dot');
 // to create new doc
 WordDoc:=WordApp.Documents.Add;
 WordDoc.Paragraphs.Item(1).Range.Bold :=True;
 WordDoc.Paragraphs.Item(1).Range.Font.Size :=30;
 WordDoc.Paragraphs.Item(1).Range.Font.Name:='Tahoma';
 WordDoc.Paragraphs.Item(1).Range.Text:= 'Welcome To Delphi World';
 // save the doc
 WordDoc.SaveAs('c:\mydoc.doc');
 // print the doc
 // WordDoc.PrintOut;
 WordDoc.Close; // closes the current document
 WordApp.Quit; // exits MS Word
end;

Regards, Geo
0
 
DeerBearCommented:
Hi,

To do what you want, I would use Server components in the
Delphi palette( yes, there's the updated version for D5 too ).

I would only use them for testing, though, since if you use them in
production code your app will be bound to that specific Word version.

HTH,

Andrew
0
 
Digitalanolog1Author Commented:
Hi Geo,

does your sample code work with any version of MS word program? Oh by the way your code works.

:) thanks man

0
 
geobulCommented:
Hi,

It works with MS Word 97 and 2000. It should work with XP also but I haven't tried that yet.

Regards, Geo
0
 
alvictorCommented:
Hello guys.

I am working with Delphi and Word just like you seem to work. But the difference is that I'm not working with desktop application, but ActiveX Components embed into web pages.

Until now I was able to use the TOleContainer just fine, I can let the user edit and modify the document. But when I need to automate the changes, using the TWordApplication component (coding just like as coding for desktop applications), it just doesn't work. I've tried several differents implementations, but every one of them give me an unespecified error message.

Do you know if there are some limitations of using ActiveX Components such as these into web pages???

Thanks a lot.
- Al Victor.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now