Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print newly created document automatically

Status
Not open for further replies.

wooonelly

IS-IT--Management
Feb 23, 2006
19
0
0
US
I have a macro that modifies an open document and creates a new document. The new document does not open.

Is there a way to just have the newly created document print right away? It does not need to be opened or saved, just printed.

Here is the code:

Sub YoYo()

Dim MyFile As Variant
Dim OutputFile As String
Dim count As Integer
Dim LineArray() As String
Dim MyLine As String
Dim arrNum As Long
Dim openfile As Long
Dim temp As String

On Error GoTo MyErrorHandler:

ReDim LineArray(10000000000#)
OutputFile = Mid$(CStr(ActiveDocument), 1, Len(ActiveDocument) - 3) & "doc"
Open ActiveDocument For Input As #1
Open OutputFile For Output As #2


Line Input #1, MyLine
While InStr(1, MyLine, "(") = 0
Print #2, MyLine
Line Input #1, MyLine
Wend

arrNum = 1
While Not EOF(1)
If InStr(1, MyLine, "(") <> 0 Then
Print #2, MyLine
For i = 1 To 30
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
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(10000000000#)
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
 
I'm assuming this is running in MS Word as you are using ActiveDocument. However, I don't see where you are creating a new document. Opening an existing document, yes. Creating a new text file, yes. Creating a new Word document, ??


Regards,
Mike
 
OutputFile = Mid$(CStr(ActiveDocument), 1, Len(ActiveDocument) - 3) & "doc"

This code takes a .prg file and creates a file of the same name but it is .doc. I just want the OutputFile to be printed automatically.

Sorry, yes this is in Word.

 
Uh
Code:
OutputFile = Mid$(CStr(ActiveDocument), 1, Len(ActiveDocument) - 3) & "doc"
does NOT create a Word file that will take a Word Print command - or it sure does not seem to. OutputFile is a STRING. And that string is used to make a text file.
Code:
Open OutputFile For Output As #2
If you want to use Word to print it, then make it a Word document.

Gerry
 
OK,

After seeing your ealier thread, I think I see what you are doing. Here is a suggestion to try. This assumes the output file is a text file or other file readable by MS Word. Add these lines after closing the other files (I'm duplicating those existing lines for clarity):
Code:
Close #1
Close #2
Set oDoc = Documents.Open(PATH & OutputFile)
With oDoc
  .PrintOut
  .Close SaveChanges:=False
End With
Set oDoc = Nothing
where oDoc is declared as Document


Regards,
Mike
 
It might help clarify things if you mention that you already sought advice in thread707-1197270 and mention why the solutions proposed there didn't work.

[tt]_____
[blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ181-2886 before posting.
 
Sorry I didn't mention the other thread but I didn't want to confuse people anymore than I thought I would. Obviously that was not the case.

Mike, Thank you very much.
That is exactly what I was looking for.
It works great.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top