• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 160
  • Last Modified:

Print from DLL

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
PieterJ
Asked:
PieterJ
1 Solution
 
jswebyCommented:
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
 
EpsylonCommented:
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
 
EpsylonCommented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
jswebyCommented:
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
 
EpsylonCommented:
That's not true. I see only one instance staring up.
0
 
bugrogerCommented:
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
 
EpsylonCommented:
bugroger, you example will also print temporary documents (~somedoc.doc) which may be created when you open documents.
0
 
jswebyCommented:
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
 
EpsylonCommented:
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
 
PieterJAuthor Commented:
Guys, thank you very much.
All the answers worked well.
Really appreciate the help !

Pieter
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now