+ Reply to Thread
Results 1 to 2 of 2

Printing document from code

  1. #1

    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. #2
    NickHK
    Guest

    Re: 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
    ....Print
    ....Close
    ....delete

    NickHK

    <[email protected]> wrote in message
    news:[email protected]...
    > 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 to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1