?
Solved

Print from DLL

Posted on 2001-06-25
10
Medium Priority
?
159 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

569 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