Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print a Text File using VBA 2

Status
Not open for further replies.

Steve101

Programmer
Mar 29, 2002
1,473
AU
Hi,

My requirement is very simple. I have a text file, which I want to spool to a network printer. The file is actually an HTML file, and I want to programatically direct it to the default (or another) network printer.

How do I do this from VBA in an Access2000 environment.

I recognise that I should be able to shell something like:

Print /D:Device Filename

but I'm not sure what device reference to use, and my attempt to just Print Filename results in nothing being printed (perhaps the bits fall out of the back of the laptop onto the carpet from the lpt1: port, but they certainly dont seem to end up in the default printers spool queue - its a networked printer).

Any help on this would be very appreciated;

Many thanks in advance,



Steve Lewy
Solutions Developer
steve@simplydata.com.au
(dont cut corners or you'll go round in circles)
 
If you wer ein VBA in Word or Excel you would use the Document.Printout method.

If you want the raw HTML to be printed, then you can spool the file to the printer using a some api calls.

But if you want the HTML to be printed and formatted nicely, then you will have to select an application that you want to use and basically open the file in that application and print it all within code.

I don't like this much myself (and I have had to do it) due to what happens if the application you spawn crashes or something. In any case here is some code to do it, but be warned, you will have to see a few of the problems that can happen to get a feel for how annoying they can be. If it's only a file you are generating and you're sure the HTML tags are supported by Word then my code should work for you as is.

Code:
Public Sub PrintFile(sFileName As String)
'  Dim oWord As Word.Application
'  Dim oDoc As Word.Document
  Dim oWord As Object
  Dim oDoc As Object
  
  Set oWord = CreateObject("Word.Application")
  
  If oWord Is Nothing Then Exit Sub
  
  oWord.Visible = False
  Set oDoc = oWord.Documents.Open(sFileName)
  ' set the active printer if you want or leave it as default
  oWord.ActivePrinter = "\\akprnt03\Xerox DC 400--PCL"
  oDoc.PrintOut
  oDoc.Close
  If oWord.Documents.Count = 0 Then
    ' also, if there was no Word open when we started then we should close word
    oWord.Quit
  End If
  
  Set oDoc = Nothing
  Set oWord = Nothing
End Sub
 
PCLewis,

Concur with all you say, and many thanks for providing a solution which at least works. I was hoping that I would have been able to avoid the application hopping, but alas, for now that is not to be.

Just one problem which you may be able to assist with. When I run the application, I get the message:

"Word is currently printing. Quitting word will cancel all pending print jobs. Do you want to quit word"

Do you know how I can "sleep" the routine, until the spooling is completed, before running the oWord.Quit statement. Seems to be a minor synchronisation problem between the word spooling completing, and the attempt to close the application.

Thanks again, and a star for your help,


Steve Lewy
Solutions Developer
steve@simplydata.com.au
(dont cut corners or you'll go round in circles)
 
oDoc.PrintOut False

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks PHV. That did the trick,



Steve Lewy
Solutions Developer
steve@simplydata.com.au
(dont cut corners or you'll go round in circles)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top