Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Print from DLL

Posted on 2001-06-25
10
152 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
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: 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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Multiple image collision 13 80
Unique identifier on a terminal server (rdp) 4 67
Delphi: barcode reading on android platform 1 51
Firemonkey BASS_Init into a thread 17 30
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…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

840 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