Windows 2000 and MS-Word

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);
rainbowsoftwareAsked:
Who is Participating?
 
chengjianCommented:
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
 
chengjianCommented:
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
 
rainbowsoftwareAuthor Commented:
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
lopemCommented:
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
 
rainbowsoftwareAuthor Commented:
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
 
lopemCommented:
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
 
CleanupPingCommented:
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
 
Lukasz LachCommented:
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
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.

All Courses

From novice to tech pro — start learning today.