Solved

Windows 2000 and MS-Word

Posted on 2002-06-24
8
768 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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now