?
Solved

Windows 2000 and MS-Word

Posted on 2002-06-24
8
Medium Priority
?
815 Views
Last Modified: 2012-06-27
How can I open a Word dokument from a Delphi application in Windows XP and 2000 with MS-Word 2000, 9.0.

Following procedure used with Windows 98 doesn't work:

  try
    WordApp := GetActiveOleObject('Word.Application');
  except on exception do
    WordApp := CreateOleObject('Word.Application');
  end;
 WordApp.wordbasic.fileopen('Dokument.doc');
 WordApp.Visible := True;
 WordApp.Wordbasic.EditBookmark(Name, , , , -1);
 WordApp.Wordbasic.insert(Edit1.text);
0
Comment
Question by:rainbowsoftware
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 1

Expert Comment

by:chengjian
ID: 7103522
Hi

You can try this:

to find out how to use OLE with Word, see Delphi example... You can find it normally in

    C:\Program Files\Borland\Delphi5\Demos\Activex\Oleauto\Word8

To read all the text of a document try the code shown below... If you want to add more functionality my best advice is to record a macro in Word and find out the macro code. I found this technique very useful when I want to port the code to delphi...

best regards
Manuel Lopez (lopem)

procedure InsertText(Pal : string);
//inserta text in Word
var
  Sel : Selection;
begin
 Sel := Form1.MsWord.Selection;
 Sel.TypeText(Pal);
end;

procedure MovePreviousWord;;

var
 MoveUnit : OleVariant;
 vCount   : OleVariant;
 Extended : OleVariant;
begin
 MoveUnit := wdWord;
 vCount := 1;
 Extended := unassigned;
 Form1.MsWord.Selection.MoveLeft(MoveUnit, vCount, Extended);
end;

procedure MoveNextWord;
//move cursor to next word
var
 MoveUnit : OleVariant;
 vCount   : OleVariant;
 Extended : OleVariant;
begin
 MoveUnit := wdWord;
 vCount := 1;
 Extended := unassigned;
 Form1.MsWord.Selection.MoveRight(MoveUnit, vCount, Extended);
end;

procedure ReadWord;
//read a word from Word doc
var
  Pal      : string;
  i        : integer;
begin
 ActRange := Form1.MsWord.Selection.Words.Item(1);
 Pal := ActRange;
end;

procedure CountAllWordOnDoc(var Cuenta: integer);
var
  Tot : OleVariant;
  Total : integer;
begin
 Tot := Form1.MsWord.ActiveDocument.Words.Count;  
 Total := Tot;
 Cuenta := Total;
end;

procedure CountWordsHighLighted(var Cuenta : integer);

var
  Tot : OleVariant;
  Total : integer;
begin
 Tot := Form1.MSWord.Selection.Words.Count;
 Cuenta := Tot;
end;

procedure HighLightWord;
//HighLight a word

var
 MoveUnit : OleVariant;
 vCount   : OleVariant;
 Extended : OleVariant;
begin
 MoveUnit := wdWord;
 vCount := 1;
 Extended := wdExtend;
 Form1.MsWord.Selection.MoveRight(MoveUnit, vCount, Extended);
end;


procedure HomeCursor;
//put cursor at the beginning of the document
var
  Unidad : OleVariant;
  Extended : OleVariant;
begin
 Unidad := wdStory;
 Extended := Unassigned;
 Form1.MsWord.Selection.HomeKey(Unidad,Extended);
end;





procedure TForm1.Button1Click(Sender: TObject);
//check if you can read word by word, from beginning to end of document...
//this test will change all word from lower to UPPER case...

var
  Pal      : string;
  i        : integer;
  Total    : integer;
begin
 HomeCursor;
 CountAllWordOnDoc(Total);
 for i := 1 to Total do
 begin
   ActRange := MsWord.Selection.Words.Item(1);
   if i <> 1 then HighLightWord
             else
             begin
               HomeCursor;
               HighLightWord;
             end;
   Pal := ActRange;
   Pal := UpperCase(Pal);
   //ShowMessage(Pal);
 end;
end;

0
 

Author Comment

by:rainbowsoftware
ID: 7103830
Manuel, thanks for your comment.
Your code doesn't work because ActRange and a lot more are not defined.
And the example in C:\Program Files\Borland\Delphi5\Demos\Activex\Oleauto\Word8 doesn't work because Word97.dcu is not found.
Could you please give me a hint.
Joern
0
 
LVL 3

Expert Comment

by:lopem
ID: 7104437
Hi rainbowsoftware...

Do you have a typical installation of Delphi 5? I found Word97.pas and Word97.dcu in

   c:\program files\Borland\Delphi5\Ocx\servers

Try to copy the code (see the first message) to the delphi msword demo. It should work. If still have problems please tell me.

best regards,
Manuel Lopez (lopem)
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:rainbowsoftware
ID: 7104556
I use Delphi 3 c/s.
But I found, that when placing the Word document in Windows Documents folder my original procedure works:

WordApp.wordbasic.fileopen('Dokument.doc');

But placing the document somewhere else and then write:

WordApp.wordbasic.fileopen('C:\NewFolder\Dokument.doc');

it doesn't work. I find this very strange.




0
 
LVL 3

Expert Comment

by:lopem
ID: 7104585
Hi again,

I dont know what is going on here... I checked my Delphi 3 c/s installation and found no OLE Word automation example. In fact, no OCX folder found.

So maybe if you put your complete code (at least the first part, uses, implementation, etc.), I can try to figure out what is happening...

best regards,
Manuel Lopez (lopem)
0
 
LVL 1

Accepted Solution

by:
chengjian earned 800 total points
ID: 7116337
Sorry , My example is for delphi 5.

You can use:
Component->Import ActiveX
Choose Microsoft Word...
It would install a word component on your ActiveX page by default.

Or use CreateOleObject() to create word object and use VBA...
0
 

Expert Comment

by:CleanupPing
ID: 9343168
rainbowsoftware:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 5

Expert Comment

by:Lukasz Lach
ID: 9453545
rainbowsoftware,
No comment has been added lately (17 days), so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:

RECOMMENDATION: Award points to chengjian http:#7116337

Please leave any comments here within 7 days.

-- Please DO NOT accept this comment as an answer ! --

Thanks,

anAKiN
EE Cleanup Volunteer
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…
In the video, one can understand the process of resizing images in single or bulk. Kernel Bulk Image Resizer is an easy to use tool for resizing large number of images. One can add and resize multiple images with this tool in single go. The video sh…

569 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