Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Print from DLL

Posted on 2001-06-25
10
Medium Priority
?
158 Views
Last Modified: 2010-04-06
Hi guys/gals

How can I create a DLL that I can either pass a Word Document Filename, and the DLL will open the file and print it and close it again. OR the DLL can maybe loop through a directory and open and print all the Word Documents or whatever documents and print them.

I only have 210 points available, but if someone can help me but need more points I will give him as much points as becomes available.

Thank you very much !

Pieter
0
Comment
Question by:PieterJ
10 Comments
 
LVL 4

Expert Comment

by:jsweby
ID: 6226689
What parts of this do you need help with? Creating the DLL or knowing how to print a Word document programatically?

To print a Word document (requires Word97 or above):

uses ComObj;

procedure PrintWordDoc(sFilename: String);
var
   oleWord, oleDocument: OleVariant;
begin
     {This procedure uses COM to integrate with MS Word (if available) and print a document}

     Try
     oleWord := CreateOLEObject('word.application');
     Except On E: Exception Do
     Begin
          E.Message := 'Microsoft Word is unavailable.';
          Raise;
          End;
     End;
     Screen.Cursor := crHourGlass;
     Application.ProcessMessages;

     Try
     oleWord.Visible := False; {Word is not visible by default so you can run it without the user even knowing}
     oleWord.Documents.Open(sFilename);

     oleDocument := oleWord.ActiveDocument;        
     oleDocument.PrintOut(False); {Background print}

     Finally
          {Close document & quit Word }
          oleDocument.Close(wdDoNotSaveChanges);
          oleWord.Quit;

          Screen.Cursor := crDefault;
          oleWord := UnAssigned;
     End; {try.finally}
end;

If running Delphi versions less than D5, then you might want to import the Word type library so that you can use some of the constants (e.g. wdDoNotSaveChanges).

J.
0
 
LVL 13

Accepted Solution

by:
Epsylon earned 840 total points
ID: 6226709
Use ShellExecute() to print documents:



library Project1;

uses
  SysUtils,
  Classes,
  ShellAPI;

{$R *.RES}

procedure PrintWordDoc(filename: PChar);
begin
  ShellExecute(0, 'print', filename, '', '', 0);
end;

procedure PrintAllWordDocs(directory: PChar);
var sr: TSearchRec;
    sl: TStringList;
    i: Integer;
begin
  sl := TStringList.Create;
  if FindFirst(directory + '*.doc', faAnyFile, sr) = 0 then
  begin
    sl.Add(sr.Name);
    while FindNext(sr) = 0 do
      sl.Add(sr.Name);
  end;
  FindClose(sr);
  for i := 0 to sl.Count - 1 do
    ShellExecute(0, 'print', PChar(sl[i]), '', '', 0);
  sl.Free;
end;

exports
  PrintWordDoc name 'PrintWordDoc',
  PrintAllWordDocs name 'PrintAllWordDocs';

begin
end.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6226712
unit TestUnit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

procedure PrintWordDoc(filename: PChar); external 'PROJECT1.DLL';
procedure PrintAllWordDocs(directory: PChar); external 'PROJECT1.DLL';

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  PrintWordDoc('test.doc');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  PrintAllWordDocs('c:\docs\');
end;

end.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:jsweby
ID: 6226716
Epsylon's method is a valid way of doing it, although it will run Word, print and close for each document. If you use the OLE automation method as I've started off above, you can actually load Word up once (without the user knowing) and iterate through the documents in the directory one by one, printing each one, then closing Word when there are no documents left.

J.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6226742
That's not true. I see only one instance staring up.
0
 
LVL 2

Expert Comment

by:bugroger
ID: 6226773
Hi,
use this together with jsweby's print funtion to print
all files in a directory.
ex.: GetFiles('C:\', *.doc, true)


Procedure GetFiles(Dir, FileMask : String; RecurseSubDirs : Boolean);
 Procedure _GetFiles(StartDir : string);
 var
  SR : tSearchRec;
  ERG : integer;

 begin
  IF StartDir[Length(StartDir)]<>'\' then StartDir := StartDir + '\';

  ERG := findfirst(StartDir+'*.*', faANYFILE, SR);
  While (ERG = 0) do
  begin
   //IF it a directory and RECURSESUBDIRS then step in
   IF (SR.Attr and faDirectory > 0)and(SR.Name[1] <> '.')and RecurseSubDirs
     then _GetFiles(StartDir + SR.Name)
     else
   //else
   IF (SR.Name[1] <> '.')and(ExtractFileExt(SR.Name) = FileMask)then
   Begin
    //jsweby's Print funtion
     PrintWordDoc(StartDir + SR.Name);
   End;
   ERG := FindNext(SR);
   Application.ProcessMessages;
  end;
  FindClose(SR);
 end;
Begin
 Delete(FileMask, 1, 1);
 _GetFiles(Dir);
End;
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6226808
bugroger, you example will also print temporary documents (~somedoc.doc) which may be created when you open documents.
0
 
LVL 4

Expert Comment

by:jsweby
ID: 6226819
Epsylon, you're right, yours does only run one instance of Word, I hadn't spotted the StringList. Pieter, although my method will load Word and allow you to control things much more, Epsylon's method actually might work better because if Word doesn't exist, but a DOC file is associated with something else (e.g. WordPad), then it will load that program and print instead.

J.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6226870
It's not the stringlist that does the trick. It's just that word receives the next print command while it is still processing the previous one, so Word is still in memory...
0
 

Author Comment

by:PieterJ
ID: 6230036
Guys, thank you very much.
All the answers worked well.
Really appreciate the help !

Pieter
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Integration Management Part 2
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

972 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