Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Printing document from code
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. |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Printing document from code
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 ....Close ....delete NickHK wrote in message oups.com... 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. |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Excel 2003 printing problem--printing 1 document on 2 pages | Excel Discussion (Misc queries) | |||
About printing a document | Excel Discussion (Misc queries) | |||
Not printing whole document | Excel Discussion (Misc queries) | |||
Need help with printing Excel document | New Users to Excel | |||
Printing and Document Name | Excel Programming |