Solved

Windows 2000 and MS-Word

Posted on 2002-06-24
8
778 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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 200 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…

808 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