Printing document from code

B

beer_on

I have a code in excel that has the user pick a document, then certain
lines are omitted and a word doc. is created. Is there a way to just
have it print the created word document after it has been made? It
does not need to be saved, just printed. Here is the code I have:

Private Sub CommandButton1_Click()
filopn1 = Application.GetOpenFilename("Text Files (*.prg), *.prg*")
If filopn1 = "" Then
MsgBox "Please select a file"
Exit Sub
End If

If UCase(Right(filopn1, 3)) <> "PRG" Then
MsgBox "This app only good for program files"
Exit Sub
End If

TextBox1.Text = filopn1
Call Make_Sections(filopn1)

End Sub
Sub Make_Sections(MyFile As Variant)
Dim OutputFile As String
Dim count As Integer
Dim LineArray() As String
Dim MyLine As String
Dim arrNum As Long

On Error GoTo MyErrorHandler:

ReDim LineArray(10000) 'This redimensions this array...I
am assuming there is less than
'10000 lines between tool sets.
OutputFile = Mid$(CStr(MyFile), 1, Len(MyFile) - 3) & "doc"
Open MyFile For Input As #1
Open OutputFile For Output As #2

'This first loop gets you past the header info before the first
parentheses (
Line Input #1, MyLine ' Initialize a line
While InStr(1, MyLine, "(") = 0 'Until you find a ( just keep
kicking out lines)
Print #2, MyLine
Line Input #1, MyLine
Wend

arrNum = 1
While Not EOF(1)
If InStr(1, MyLine, "(") <> 0 Then 'The first instance may or may
not have lines
Print #2, MyLine 'above it...this should be o.k.
For i = 1 To 30 'Print 30 lines after the (
Line Input #1, MyLine
Print #2, MyLine
Next i

For i = 1 To 6
Print #2,
Next i

Line Input #1, MyLine


Do Until InStr(1, MyLine, "(") <> 0 'Until find another (, read
lines into array
LineArray(arrNum) = MyLine
arrNum = arrNum + 1
Line Input #1, MyLine
Loop
If InStr(1, MyLine, "(") <> 0 Then
For i = 20 To 1 Step -1
Print #2, LineArray(arrNum - i)
Next i
End If
ReDim LineArray(10000) 'This just empties out the
lines in the array
arrNum = 1
End If
Wend

Close #1
Close #2
UserForm1.Hide

MyErrorHandler:
If Err.Number = 62 Then
Close #1
Close #2
UserForm1.Hide
Exit Sub

End If

End Sub

Essentially, the document could be hundreds of pages long with twenty
sections. This program keeps the first thirty lines and the last ten
lines of each section and makes the document 20 pages long, one page
per section. Each section starts with a "(" and after the first thirty
lines, there is no "(" until the next section. I would just like the
document that is created to be printed automatically.

Any help would be appreciated.
 
N

NickHK

You could ShellExecute with the "print" operation, then Kill the file.
Or use automation:
Dim MyWord As Word.Application
Set MyWord=New Word.Application
....Open your doc
....Print
....Close
....delete

NickHK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top