Solved

How can a Delphi program cut text from Word?

Posted on 1998-11-04
12
371 Views
Last Modified: 2010-05-18
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
Comment
Question by:toreot
  • 5
  • 4
  • 3
12 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 1345623
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
 

Author Comment

by:toreot
ID: 1345624
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
 

Author Comment

by:toreot
ID: 1345625
I need specific code showing how to order Word to cut the text from a document and also pasting text from the clipboard.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 4

Accepted Solution

by:
dwwang earned 150 total points
ID: 1345626
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
 

Author Comment

by:toreot
ID: 1345627
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
 
LVL 4

Expert Comment

by:dwwang
ID: 1345628
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
 
LVL 8

Expert Comment

by:ZifNab
ID: 1345629
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
 

Author Comment

by:toreot
ID: 1345630
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
 
LVL 4

Expert Comment

by:dwwang
ID: 1345631
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
 
LVL 8

Expert Comment

by:ZifNab
ID: 1345632
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
 
LVL 4

Expert Comment

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

Regards,
Wang
0
 

Author Comment

by:toreot
ID: 1345634
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…

820 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