Solved

How can a Delphi program cut text from Word?

Posted on 1998-11-04
12
366 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

706 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

18 Experts available now in Live!

Get 1:1 Help Now