print txt file in dir

User avatar
sal21
PlatinumLounger
Posts: 4364
Joined: 26 Apr 2010, 17:36

print txt file in dir

Post by sal21 »

I just have a txt file in c:\mydir\test.txt

Is possible to print direcly to a DEFAULT printer a single copy in orizontal orintation?

via vba for excel code, please.
:thankyou:

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: print txt file in dir

Post by HansV »

Try this:

Code: Select all

Sub PrintMyFile()
    PrintTextFile "c:\mydir\test.txt"
End Sub

Sub PrintTextFile(strFile As String)
    Dim objWrd As Object
    Dim objDoc As Object
    Dim blnStart As Boolean
    On Error Resume Next
    Set objWrd = GetObject(Class:="Word.Application")
    If objWrd Is Nothing Then
        Set objWrd = CreateObject(Class:="Word.Application")
        If objWrd Is Nothing Then
            MsgBox "Cannot start Word. Exiting...", vbCritical
            Exit Sub
        End If
        blnStart = True
    End If
    On Error GoTo ErrHandler
    Set objDoc = objWrd.Documents.Open(strFile, Format:=5) ' 5 = wdOpenFormatUnicodeText
    objDoc.PageSetup.Orientation = 1 ' 1 = wdOrientLandscape
    objDoc.PrintOut
ExitHandler:
    On Error Resume Next
    objDoc.Close SaveChanges:=False
    If blnStart Then
        objWrd.Quit SaveChanges:=False
    End If
    Exit Sub
ErrHandler:
    MsgBox Err.Description, vbCritical
    Resume ExitHandler
End Sub
The procedure PrintTextFile can be used to print any text file.
Best wishes,
Hans

MSingh
3StarLounger
Posts: 366
Joined: 12 May 2010, 06:49

Re: print txt file in dir

Post by MSingh »

Many Thanks Hans,
I copied & used the same to print a text file from Access.
Kind Regards,
Mohamed

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: print txt file in dir

Post by HansV »

Hi Mohamed,

In the code shown above, I used Word to print the text file since Sal21 wanted to print the file in landscape mode. If you don't require landscape mode, you can use the following:

Code: Select all

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

Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMAXIMIZED As Long = 3

Public Sub PrintFile(strFile As String)
    If ShellExecute(Application.hWndAccessApp, "PRINT", _
            strFile, 0&, 0&, SW_SHOWNORMAL) < 32 Then
        MsgBox "Something went wrong!", vbCritical
    End If
End Sub
PrintFile will print any file using the application associated with it (if it supports printing): text files will be printed from Notepad, Word documents from Word, Excel workbooks from Excel, etc., using the default settings - you have no control over the print settings.

Examples:

PrintFile "C:\MyFiles\MyTextFile.txt"
PrintFile "C:\Word\MyDocument.docx"
PrintFile "C:\Scans\MyScan.pdf"
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4364
Joined: 26 Apr 2010, 17:36

Re: print txt file in dir

Post by sal21 »

HansV wrote:Try this:

Code: Select all

Sub PrintMyFile()
    PrintTextFile "c:\mydir\test.txt"
End Sub

Sub PrintTextFile(strFile As String)
    Dim objWrd As Object
    Dim objDoc As Object
    Dim blnStart As Boolean
    On Error Resume Next
    Set objWrd = GetObject(Class:="Word.Application")
    If objWrd Is Nothing Then
        Set objWrd = CreateObject(Class:="Word.Application")
        If objWrd Is Nothing Then
            MsgBox "Cannot start Word. Exiting...", vbCritical
            Exit Sub
        End If
        blnStart = True
    End If
    On Error GoTo ErrHandler
    Set objDoc = objWrd.Documents.Open(strFile, Format:=5) ' 5 = wdOpenFormatUnicodeText
    objDoc.PageSetup.Orientation = 1 ' 1 = wdOrientLandscape
    objDoc.PrintOut
ExitHandler:
    On Error Resume Next
    objDoc.Close SaveChanges:=False
    If blnStart Then
        objWrd.Quit SaveChanges:=False
    End If
    Exit Sub
ErrHandler:
    MsgBox Err.Description, vbCritical
    Resume ExitHandler
End Sub
The procedure PrintTextFile can be used to print any text file.
WORK PERECT!

Is possible to change the defulat fonto in Arial size 8, before to printout?

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: print txt file in dir

Post by HansV »

Yes, before the line

Code: Select all

    objDoc.PrintOut
insert these new lines:

Code: Select all

    with objDoc.Content.Font
        .Name = "Arial"
        .Size = 8
    End With
Best wishes,
Hans

MSingh
3StarLounger
Posts: 366
Joined: 12 May 2010, 06:49

Re: print txt file in dir

Post by MSingh »

Thank You Hans,
Much appreciated.
Kind Regards,
Mohamed