Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I work with Word Documents without using COM Automation?

I have read multiple posts on the issue, and none seem to come to a decent conclusion to my question. (Perhaps I'm trying to see if anything has popped up lately.)

I have a small charity app that handles pledges. In doing so, it needs to work with and print documents.

Thing is, if Word is open in the background, the app thread will hang and won't respond to the closure of Word, and I have to roll back manually and close word. Sure, that all works fine, but I simply cannot guarantee that the end user will close Word, even if I put the instruction in a user manual.

I'm not too fussed about speed, but I guess that if it can be enhanced, it would be a nice little bonus.

Have any libraries been released for Delphi that will allow me to open, edit, print, and save documents? If not, is there a way to use Word Automation in such a way that it will not conflict with another open handle of Word when opened?

like image 628
Mike Rockétt Avatar asked Oct 31 '12 13:10

Mike Rockétt


People also ask

Is there a way to update multiple Word documents at once?

Unfortunately, Word does not include this type of capability. Your options are either to rely on a third-party solution or write your own macro to do the changes. There are a number of third-party programs that offer the type of search-and-replace function necessary when working with multiple documents.


3 Answers

If you use GetActiveOleObject, you will get the running instance of Word. By using CreateOleObject, you will get a new instance and shouldn't be troubled by other running instances.

In case you use the TWordApplication, wrapper you can set ConnectKind to ckNewInstance to accomplish this. By default, TWordApplication will try to connect with a running instance.

like image 56
GolezTrol Avatar answered Oct 14 '22 05:10

GolezTrol


If you want to open edit and print Word documents and you don't mind using RTF format for what you're doing, investigate TRichView.

It will generate rich documents that are in RTF format, which is one of the formats MS word supports. I don't think it directly reads .DOC files but you can convert .DOC and .DOCX into RTF, for most simple files, but certain advanced formatting features would be lost in the conversion.

It has the advantage of working without any need for even any copy of MS Word to be installed on the machine that is going to do the document processing. For production of receipts and other simple documents, this would be the most reliable technique; Don't use Word directly, at all.

like image 26
Warren P Avatar answered Oct 14 '22 04:10

Warren P


procedure PrintViaWord (const filename: string);
const
 wdUserTemplatesPath = 2;

var
  wrdApp, wrdDoc, wrdSel: variant;

begin
 wrdApp:= CreateOleObject ('Word.Application');   // create new instance
 sleep (5000);    // this fixes a bug in Word 2010 to do with opening templates
 wrdDoc:= wrdApp.documents.add (
          wrdApp.Options.DefaultFilePath[wdUserTemplatesPath] + '\mytemplate.dot');

 wrdDoc.Select;
 wrdSel:= wrdApp.selection;
 wrdApp.Options.CheckSpellingAsYouType:= 0;
 wrdSel.paragraphformat.alignment:= 1;
 wrdSel.typetext ('This is a program demonstrating how to open Word in the background'
                  + ' and add some text, print it, save it and exit Word');
 wrdDoc.SaveAs (filename + '.doc');
 wrdApp.ActivePrinter:= 'Konica Minolta 211';
 wrdApp.PrintOut;
 WrdDoc.SaveAs (filename + '.doc');
 wrdApp.quit;

 wrdSel:= unassigned;
 wrdDoc:= unassigned;
 wrdApp:= unassigned
end;
like image 29
No'am Newman Avatar answered Oct 14 '22 05:10

No'am Newman