Printing an RTF File

pedders

Member
Joined
May 4, 2007
Messages
6
Programming Experience
Beginner
I have an application that takes form inputs, searches an RTF document and replaces where appropriate, and writes back out to an RTF.

I want a button that i can press that will then pick up that RTF and send it to the printer.

I've got it to communicate with the printer but all i get is a blank sheet of paper.

My print button code is:


Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim printdoc As New Printing.PrintDocument
PrintDialog1.PrinterSettings = printdoc.PrinterSettings
If PrintDialog1.ShowDialog() <> Windows.Forms.DialogResult.Cancel Then
If File.Exists(System.Windows.Forms.Application.StartupPath() & "\Outputs\dataprep-output.rtf") Then
MsgBox("File exists", MsgBoxStyle.OkOnly)
printdoc.DocumentName = System.Windows.Forms.Application.StartupPath() & "\Outputs\dataprep-output.rtf"
printdoc.Print()
Else
MsgBox("File does not exist", MsgBoxStyle.OkOnly)
End If
End If
printdoc.Dispose()
End Sub

Or without the dialog:

Dim printme As New Printing.PrintDocument
printme.DocumentName = "c:\dataprep-output.rtf"
printme.Print()

I will be grateful if anyone can shed any light on why all i get at the end is a blank piece of paper!
Or if anyone can tell me an easier way to do the same function (i do need it to output to an RTF before printing so cannot simplify it there)

BTW i'm using VB 2005 Express

Thanks

Pedro
 
Last edited:
check out this link

There is a inherited RichTextBox control class available from MS that implements FormatRange API which lets you with ease print rich formatted text from the control, see Getting WYSIWYG Print Results from a .NET RichTextBox

Alternative is a real painful lots of work analyzing each characters formatting, measuring size conseqences of different formatting and using this info with Graphics.DrawString (one call for each difference) when printing.
 
Thanks for your help but it turned out that that didn'tdo the trick, as my output file was more complex. (a word codument saves as RTF so not a strict RTF)

Managed to get it working by calling the MS Word Object library and opening the word application in the background and printing that way whhich works fine.

Just 1 problem though!

Nothings ever simple! It doesn't shutdown the winword.EXE process in task manager.

I have included the appWord.Quit() statement but it doesnt seem to work properly.

Regards

Pedro
 
I've fixed it now.

Here is my working code:


Dim LResponse As Integer

LResponse = MsgBox("Do you want to print this form?", vbYesNo, "Continue")

If LResponse = vbYes Then
If File.Exists(System.Windows.Forms.Application.StartupPath() & "\Outputs\dataprep-output" & LBLSurvyTypeCode.Text & TXTNode.Text & ".rtf") = True Then
Dim appMSWord As New Word.Application
appMSWord.Documents.Open(System.Windows.Forms.Application.StartupPath() & "\Outputs\dataprep-output" & LBLSurvyTypeCode.Text & TXTNode.Text & ".rtf")
Dim dlg As Word.Dialog
dlg = appMSWord.Dialogs.Item(Word.WdWordDialog.wdDialogFilePrint)
dlg.Display()
dlg.Execute()
appMSWord.Quit(Word.WdSaveOptions.wdDoNotSaveChanges)
appMSWord = Nothing
Else
MsgBox("File does not exist", MsgBoxStyle.Critical)
End If
End If

I moved "Dim appMSWord As New Word.Application" into the private sub.

The red text can be replaced with an absolute file location reference e.g. "C:\Document.RTF"

 
Back
Top