2 Replies - 5338 Views - Last Post: 10 December 2008 - 04:07 PM Rate Topic: -----

#1 johnstde   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-December 08

Print a text file

Posted 09 December 2008 - 11:41 AM

I've tried quite a few different scenarios from snippets to try to get a simple text file to print on my default printer, and can't get any of them to work.
I've tried:
Printer.print (get object 464 error)
Shell printing
FSO printing

I don't care how I do the print, although I'm not a big fan of one of the options, reading the file line by line and printing it line by line. The code below gives you an idea of what I have tried. The commented out sections gave an error when I first tested, hence they are commented out. The variable assignments at the top are not the right ones for the commented out code, so although I kept the commented out code to remind me what I have tried, I didn't keep all the supporting code.

Private Sub CommandButtonPrnt_Click()
	Dim ofso As New FileSystemObject
	Dim sPath As String
	Dim Item As Collection
	Set ofolder = ofso.GetFolder("F:\temp\")
			'MsgBox "whatever" + (ofolder.Files.Count)
 '   Set ofso = ofolder.Files
	 sPath = "\\vmsalesnusetst\taxware\temp\"
	sFile = "product.txt"
	Debug.Print ("\\vmsalesnusetst\taxware\temp\product.txt")
	'	Call Shell("C:\WINNT\NOTEPAD " & sPath & sFile, 1
			PrintTXTFile = ("F:\temp\verazip.txt")

'Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

'ShellExecute Me.hwnd, "Print", app.path & "temp\verazip.txt", vbNullString, "F:\", SW_SHOWNORMAL

	Call PrintFile("F:\temp\Product.txt")
	Call PrintFile("F:\temp\product.txt")
	Call PrintFile("F:\temp\salestax.txt") 'This prints on the default printer

End Sub

	Private Sub PrintFile(ByVal sFile As String)
		Dim sText As String, iFile As Byte
		iFile = FreeFile
		Open sFile For Input As iFile
		sText = Input(LOF(iFile), iFile)
		Close iFile
		sText = Trim$(sText)
		Printer.Print sText
	End Sub

Is This A Good Question/Topic? 0
  • +

Replies To: Print a text file

#2 kasbaba   User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 134
  • Joined: 03-November 08

Re: Print a text file

Posted 10 December 2008 - 02:54 PM


Please take a look on the attached zip file.

Hope this helps


NOTE: This code was written in VB.NET2008

Attached File(s)

This post has been edited by kasbaba: 10 December 2008 - 02:55 PM

Was This Post Helpful? 0
  • +
  • -

#3 FractalP   User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 03-December 08

Re: Print a text file

Posted 10 December 2008 - 04:07 PM

The method you mentioned above, printing each line separately, isn't the best option, but it is AN option, so I thought I'd chuck in a bit of code just in case.

	Do While Not EOF(1)
		Line Input #1, tempString
		Printer.Print tempString

Change 1 to the appropriate file number and make sure tempString is declared. I used this method on a past assignment and it worked fine. It also might be a good idea to include a common dialog box (I'm not sure of 2005/08, but they do have it in past versions) which allows you to select a printer and return an error.

This post has been edited by FractalP: 10 December 2008 - 04:08 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1