Solved

Print from DLL

Posted on 2001-06-25
10
150 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 210 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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