user3319895
user3319895

Reputation: 111

How to print a .txt file?

I'm using the code below to open a .txt file and parse information into an Excel sheet.

I'd like to add a feature that would print the txt file before the other part of the program runs.

Sub fxfeed()
Dim myFile As String, text As String, textline As String, FX As Long, _
  nrow As Integer, i As Integer, data As String

data = Format(Range("f4"), "yyyymmdd")

myFile = "Z:\ARCHIVES\FX." & data

Open myFile For Input As #1
Do Until EOF(1)
    Line Input #1, textline
    text = text & textline
Loop

Close #1

nrow = Cells(Rows.Count, "P").End(xlUp).Row

For i = 6 To nrow
    FX = InStr(text, Range("p" & i))
    Range("o" & i).Value = Mid(text, FX + 20, 5)
Next

End Sub

Upvotes: 1

Views: 3155

Answers (2)

Siddharth Rout
Siddharth Rout

Reputation: 149325

Here is a one liner which prints to the default printer using Notepad. Note that if the path to your filename has spaces in it then you will need to enclose it in quotes.

Shell ("notepad.exe /p " & yourfilename)

Upvotes: 1

PeterT
PeterT

Reputation: 8557

VBA does not have any direct access to the Windows default printer, so you'll have to work outside of the regular execution environment to make this happen. (Notice that this will print to the default Windows printer, which is defined for your installation.)

Option Explicit

Declare Function apiShellExecute 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

Public Sub PrintFile(ByVal strPathAndFilename As String)
    Call apiShellExecute(Application.hwnd, "print", strPathAndFilename, _
                         vbNullString, vbNullString, 0)
End Sub

Sub Test()
    PrintFile ("C:\Test.pdf")
End Sub

(See OzGrid, entry #2)

Upvotes: 0

Related Questions