Link to home
Start Free TrialLog in
Avatar of toreot
toreot

asked on

How can a Delphi program cut text from Word?

How can I programmatically cut from and paste text to Word? By clicking a button, the Delphi program should check if there is an open Word document and then cut the text from Word to the clipboard. I know how to paste from the clipboard, but not how to use Word Basic.
When the text has been processed in my program, the user should paste it back to Word by clicking another button.
Avatar of ZifNab
ZifNab

Go through OLE,

First read this document :

http://members.aol.com/_ht_a/charliecal/TechPapers/DelphiWord.htm

Then if you still got problems, ask them and/or look at freeware components/examples at torries library.

Regards, Zif.
Avatar of toreot

ASKER

I have already taken a quick look at this document, but this is much more than I need and I don't have the time for researching this. Could anybody just supply me with the necessary code snippets?
Avatar of toreot

ASKER

I need specific code showing how to order Word to cut the text from a document and also pasting text from the clipboard.
ASKER CERTIFIED SOLUTION
Avatar of dwwang
dwwang

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of toreot

ASKER

Unfortunately  MSWord:=GetActiveOleObject('Word.Basic') stops with an with an error 'Operation not available'.  This happens on two machines with Word 7 installed.

Also I have a feeling that this code won't work with Office 97. Is that correct?
I don't think so, I have word97 installed on my machine when building the example using this method. Are you sure MSWord is running when you call GetActiveOleObject('Word.Basic')? Or else you shold call CreateOleObject('Word.Basic').
GetActiveOleObject can only be used if the program (in this case, Word) is already opened. Use CreateOleObject when you want to start a program (word in this case). Mostly we combine these two :

First use GetActiveOleObject. If this raises an OLE exception, then use CreateOleObject to start Word. If this raises an exception, than word isn't installed (probably).
Avatar of toreot

ASKER

Sorry, when I have Word 7 open (Norwegian version, is that important?) I still get the EOleSysError.
Using CreateOleObject  nothing seems to happen, but perhaps this instance of Word isn't visible?
If you use MSWord:=CreateOleObject('Word.Basic'); you also need the MsWord.Appshow; to show the application(see my answer above.)

I realized that there are some problems when you use localized MSWord, because name of all the objects and methods of word basic could be localized. Maybe you can check out the help of your word to find the proper object/method name to use. see the "autoctrl" demo in Delphi 3 to find how to deal with this situation, it can process 3 languages.

dwwang, it is not nessecary to make msword visible. I should also work without making it visible.
toreot, if you only make some time to read the examples in that article, then you normally should get away with it.

Hi, Zif:
Yes, I know there's no need to show the application, just for answering toreot's question. :)

Regards,
Wang
Avatar of toreot

ASKER

Sorry about the delay. GetActiveOleObject('Word.Basic') should work for the Norwegian version of Word, but doesn't. However, using CreateOleObject('Word.Basic') and translating the WordBasic function names to Norwegian, dwwang's code works even if Word is already open, so I award him the points.

It would be nice to know, however, why GetActiveOleObject('Word.Basic') raises the EOleSysError 'Operation not available' (translated from Norwegian).