Library tutorials & articles
Common Dialog Control
Using the Print Dialog
You can use the print dialog to allow the user to select printer settings, which your application can then use to print. Below are the most common parameters you will need to set before displaying the Print Dialog Box.
| Parameter | Changes... |
| DialogTitle | the Title displayed in the Dialog Box |
| Printer Default | whether to use the default printer |
| Flags | custom settings such as print selection only |
| CancelError | whether an error occurs when the dialog box is cancelled. |
The following code displays a print dialog, with page numbers disabled, and then prints the text in RichTextBox1. It also returns the selected printer
CommonDialog1.PrinterDefault = True
CommonDialog1.CancelError = True
' Set flags - no page numbers, return the selected printer
CommonDialog1.Flags = cdlPDReturnDC + cdlPDNoPageNums
If RichTextBox1.SelLength = 0 Then
CommonDialog1.Flags = CommonDialog1.Flags + cdlPDAllPages
Else
CommonDialog1.Flags = CommonDialog1.Flags + cdlPDSelection
End If
' Enables error handling to catch cancel error
On Error Resume Next
' display the print dialog box
CommonDialog1.ShowPrinter
If Err Then
' This code runs if the dialog was cancelled
Msgbox "Dialog Cancelled"
Exit Sub
End If
' Prints the contents of RichTextBox
RichTextBox1.SelPrint (Printer.hDC)
Related articles
Related discussion
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
-
passing and reading parameters from using Shell
by jigartoliya (0 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
listbox scrollbar
by Dennijr (10 replies)
-
Can you describe Above simple VB6 code?
by pramodmca09 (0 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
As far as I know. This is a licensed control from Microsoft, which requires a license file (*.lpk) file generated using the LPK Tool from Microsoft.
When I run the above code, it shows an error as *object cannot be found 'CommonDialog1' *.
As a result, I slightly modified the code as:
Aftewards, it shows an error as: Class is not licensed for use.
Any Idea why its happening. Thanks.
!--removed tag-->Dear Sir
Thanks for you help and I like you way that you got it, but my questation is Can I use Common Dialog without use File Type? because I need only open folder without select file "Only Folder Path"
Using the FileName (path) and FileTitle (filename) properties of the dialog control,
Dim FilePathOnly As String
FilePathOnly = Left(FileName, Len(FileName) - Len(FileTitle))
'or
FilePathOnly = Left(FileName, InStrRev(FileName, "\"))
'or if you use a scripting runtime
Dim fso As FileSystemObject
FilePathOnly = fso.GetPath(FileName)
OpenFileWName = TreeView1.SelectedItem
If OpenFileWName = OpenPath Then
MsgBox "You can not select a directory. Please select a template file."
Else
frmMain.RichTextBox1.LoadFile OpenPath + OpenWFileName
'Unload Me
End If
And it should be as simple as that
Any idea on my problem? I posted it on here as darkwolf
is there a way of getting just the path of the file this way, without the file name in it?
E.G. if i open
C:\Windows\Wini.ini
can i get the program to read just
C:\Windows
from that?
I have decided to do it a different way though. I am now using a Tree view to list all files in the directory. It allows a much better interface for what I am trying to accomplish.
I do have a new problem though. The main function of the form (frmNewTemplate) is to open a file an put the contents into a rich textbox on the main form (frmMain) but it doesn't seem to be working. If I put a RichTextBox on the frmNewTemplate it is able to open the file and put it into the RichTextBox correctly but when I change it to be placed into the frmMain it doesn't update the RichTextBox.
Do have any ideas on how to pass the information from frmNewTemplate to frmMain?
I have included the code below. Again it works until the part of updating frmMain.
OpenPath = App.Path & "\templates\"
OpenFileWName = TreeView1.SelectedItem
If OpenFileWName = OpenPath Then
MsgBox "You can not select a directory. Please select a template file."
Else
Open OpenPath & OpenFileWName For Input As #1
Do Until EOF(1)
Line Input #1, lineoftext$
If frmMain.RichTextBox1.Text = "" Then
alltext$ = lineoftext$
Else
alltext$ = alltext$ & vbCrLf & lineoftext$
End If
frmMain.RichTextBox1.Text = alltext$
Loop
Close #1
'Unload Me
End If
Thank you in advance for any help that you or anyone else may provide.
Joel Strellner
I think you may need to check the file selected to ensure that it was chosen from the path that your trying to restrict to.
Thank you,
Joel Strellner
This thread is for discussions of Common Dialog Control.