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.
print txt file in dir
-
- Administrator
- Posts: 78549
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: print txt file in dir
Try this:
The procedure PrintTextFile can be used to print any text file.
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
Best wishes,
Hans
Hans
-
- 3StarLounger
- Posts: 366
- Joined: 12 May 2010, 06:49
Re: print txt file in dir
Many Thanks Hans,
I copied & used the same to print a text file from Access.
Kind Regards,
Mohamed
I copied & used the same to print a text file from Access.
Kind Regards,
Mohamed
-
- Administrator
- Posts: 78549
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: print txt file in dir
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:
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"
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
Examples:
PrintFile "C:\MyFiles\MyTextFile.txt"
PrintFile "C:\Word\MyDocument.docx"
PrintFile "C:\Scans\MyScan.pdf"
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4364
- Joined: 26 Apr 2010, 17:36
Re: print txt file in dir
WORK PERECT!HansV wrote:Try this:
The procedure PrintTextFile can be used to print any text file.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
Is possible to change the defulat fonto in Arial size 8, before to printout?
-
- Administrator
- Posts: 78549
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: print txt file in dir
Yes, before the line
insert these new lines:
Code: Select all
objDoc.PrintOut
Code: Select all
with objDoc.Content.Font
.Name = "Arial"
.Size = 8
End With
Best wishes,
Hans
Hans
-
- 3StarLounger
- Posts: 366
- Joined: 12 May 2010, 06:49
Re: print txt file in dir
Thank You Hans,
Much appreciated.
Kind Regards,
Mohamed
Much appreciated.
Kind Regards,
Mohamed