• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 397
  • Last Modified:

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.
0
toreot
Asked:
toreot
  • 5
  • 4
  • 3
1 Solution
 
ZifNabCommented:
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.
0
 
toreotAuthor Commented:
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?
0
 
toreotAuthor Commented:
I need specific code showing how to order Word to cut the text from a document and also pasting text from the clipboard.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
dwwangCommented:
Hi, hope this code fragment will help:

When having a MSWord running, just write:

     MSWord:Variant;
     ...
     ...
     MSWord:=GetActiveOleObject('Word.Basic');
     MSWord.EditSelectAll;
     MsWord.EditCopy;
     Form1.richedit1.PasteFromClipBoard;

    When need to paste back:

     //     Form1.richedit1.copytoclipboard
     MSWord.EditSelectAll;
     MsWord.EditPaste;

    Also if you want to start MSWord automatically:

     MSWord:=CreateOleObject('Word.Basic');
     MsWord.Appshow;
     Msword.FileNew; or FileOpen('FileName')

0
 
toreotAuthor Commented:
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?
0
 
dwwangCommented:
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').
0
 
ZifNabCommented:
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).
0
 
toreotAuthor Commented:
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?
0
 
dwwangCommented:
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.

0
 
ZifNabCommented:
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.

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

Regards,
Wang
0
 
toreotAuthor Commented:
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).
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.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now