Convert this VB code to VBA

K

Kumar

Hello,

What will be the equivalent VBA code for the following VB code?? (The
code prints RichTextBox control).


Private Sub cmdDirPrint_Click()

ComDiaPropPrint.Flags = cdlPDReturnDC + cdlPDNoPageNums

If rtbPropDirections.SelLength = 0 Then
ComDiaPropPrint.Flags = ComDiaPropPrint.Flags + cdlPDAllPages
Else
ComDiaPropPrint.Flags = ComDiaPropPrint.Flags + cdlPDSelection
End If


ComDiaPropPrint.ShowPrinter
printer.Print ""
rtbPropDirections.SelPrint printer.hDC
printer.enddoc


End Sub

Thanks
Kumar
 
J

Jonathan West

It's not possible to convert this code directly because VBA doesn't support
the Printer object, and doesn't allow direct printing from VBA, only
printing of Word documents using the Printout methid.

What are you trying to achieve?
 
K

Kumar

My macros in Word produce a report which is displayed on a userform
Richtextbox control. I needed to print the report to the default
printer. I came across this vb code that does the printing, but didn't
know its vba equivalent.

Alternatively, I tried producing the report in a txt file and then
using shell command to print it. It prints, but it doesnot have the
beauty of a print from a richtext box. Like, How can I control fonts
and their formatting in notepad and more importantly, when the line is
too long, the print chops off the right end of the lines. Notepad
printout does not come good at all, unless there is a way that I can
make it look like richtextbox printout from vb.

Any other suggestions also welcome !!!

Thanks
Kumar
 
J

JGM

Hi Kumar,

Why don't you send the formatted report back to a "temporary" Word document
and print it from there?

For example:
'_______________________________________
Private Sub CommandButton1_Click()

'This codes assumes you already have a blank Word document open

Dim CurrentPara As Paragraph
Dim myRTFdoc As Document

Const RTFDocPathandName As String = _
"D:\Test\myrtf.rtf"

Set CurrentPara = Selection.Paragraphs(1)

RichTextBox1.SaveFile RTFDocPathandName

Set myRTFdoc = Documents.Open(FileName:=RTFDocPathandName,
Visible:=False)

CurrentPara.Range.FormattedText = myRTFdoc.Content.FormattedText

myRTFdoc.Close

'do the printing here

Set CurrentPara = Nothing
Set myRTFdoc = Nothing

End Sub
'_______________________________________

HTH
Cheers!
 
K

Kumar

Thanks JGM,

One question, maybe I am confused. But how do I do the printing here?
What will be code for printing?

Thanks
Kamur
 
J

JGM

Hi Kumar,

Look up the PrintOut method in the Word VBA Help, there are lots of examples
and they explain it way better that I can!

Good luck!
 
K

Kamur

Thanks JGM. I did look at the printout method....I will be trying it out
tomorrow.

Kamur
 

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