VB6/VBA - El control CommonDialog.

Septiembre 2017


He dejado todas las constantes disponibles, a pesar de que todas estas no son utilizadas en las funciones propuestas, estas permitirán eventuales búsquedas de otros posibles datos.

Las funciones posibles con Commond Dialog

  • El cuadro de dialogo > Abrir un archivo
  • El cuadro de dialogo > Guardar como
  • El cuadro de dialogo > Imprimir
  • El cuadro de dialogo > Seleccionar la fuente

Previamente


Un form > Name = Dialog
Un control CommonDialog > Name = CMDialog1

botón1 > Caption = Abrir Archivo
Código en CommandX_Click() > Ret = cmd_Abre() >ver los rem para mas detalles.

botón2 > Caption = Guardar como
Código en Clic Ret = cmd_Abre()

botón3 > Caption = Imprimir
Código en CommandX_Click() > Ret = cmd_Print()

botón4 > Caption = Police
Código en CommandX_Click() > Ret = cmd_Police()

En un módulo estandar


Option Explicit   

Public Filtro1 As String   
Public Filtro2 As String   
Public Filtro3 As String   
Public Filtro4 As String   
Public Filtro5 As String   
Public Filtro6 As String   
Public Filtro7 As String   

Public Const DLG_FILE_OPEN = 1   
Public Const DLG_FILE_SAVE = 2   
Public Const DLG_COLOR = 3   
Public Const DLG_FONT = 4   
Public Const DLG_Print = 5   
Public Const DLG_HELP = 6   

'File Open/Save Dialog Flags   
Public Const OFN_READONLY = &H1&   
Public Const OFN_OVERWRITEPROMPT = &H2&   
Public Const OFN_HIDEREADONLY = &H4&   
Public Const OFN_NOCHANGEDIR = &H8&   
Public Const OFN_SHOWHELP = &H10&   
Public Const OFN_NOVALIDATE = &H100&   
Public Const OFN_ALLOWMULTISELECT = &H200&   
Public Const OFN_EXTENSIONDIFFERENT = &H400&   
Public Const OFN_PATHMUSTEXIST = &H800&   
Public Const OFN_FILEMUSTEXIST = &H1000&   
Public Const OFN_CREATEPROMPT = &H2000&   
Public Const OFN_SHAREAWARE = &H4000&   
Public Const OFN_NOREADONLYRETURN = &H8000&   

'Color Dialog Flags   
Public Const CC_RGBINIT = &H1&   
Public Const CC_FULLOPEN = &H2&   
Public Const CC_PREVENTFULLOPEN = &H4&   
Public Const CC_SHOWHELP = &H8&   

'Fonts Dialog Flags   
Public Const CF_SCREENFONTS = &H1&   
Public Const CF_PRINTERFONTS = &H2&   
Public Const CF_BOTH = &H3&   
Public Const CF_SHOWHELP = &H4&   
Public Const CF_INITTOLOGFONTSTRUCT = &H40&   
Public Const CF_USESTYLE = &H80&   
Public Const CF_EFFECTS = &H100&   
Public Const CF_APPLY = &H200&   
Public Const CF_ANSIONLY = &H400&   
Public Const CF_NOVECTORFONTS = &H800&   
Public Const CF_NOSIMULATIONS = &H1000&   
Public Const CF_LIMITSIZE = &H2000&   
Public Const CF_FIXEDPITCHONLY = &H4000&   
Public Const CF_WYSIWYG = &H8000&         'must also have CF_SCREENFONTS & CF_PRINTERFONTS   
Public Const CF_FORCEFONTEXIST = &H10000   
Public Const CF_SCALABLEONLY = &H20000   
Public Const CF_TTONLY = &H40000   
Public Const CF_NOFACESEL = &H80000   
Public Const CF_NOSTYLESEL = &H100000   
Public Const CF_NOSIZESEL = &H200000   

'Printer Dialog Flags   
Public Const PD_ALLPAGES = &H0&   
Public Const PD_SELECTION = &H1&   
Public Const PD_PAGENUMS = &H2&   
Public Const PD_NOSELECTION = &H4&   
Public Const PD_NOPAGENUMS = &H8&   
Public Const PD_COLLATE = &H10&   
Public Const PD_PRINTTOFILE = &H20&   
Public Const PD_PRINTSETUP = &H40&   
Public Const PD_NOWARNING = &H80&   
Public Const PD_RETURNDC = &H100&   
Public Const PD_RETURNIC = &H200&   
Public Const PD_RETURNDEFAULT = &H400&   
Public Const PD_SHOWHELP = &H800&   
Public Const PD_USEDEVMODECOPIES = &H40000   
Public Const PD_DISABLEPRINTTOFILE = &H80000   
Public Const PD_HIDEPRINTTOFILE = &H100000   

'Help Constants   
Public Const HELP_CONTEXT = &H1           'Display topic in ulTopic   
Public Const HELP_QUIT = &H2              'Terminate help   
Public Const HELP_INDEX = &H3             'Display index   
Public Const HELP_CONTENTS = &H3   
Public Const HELP_HELPONHELP = &H4        'Display help on using help   
Public Const HELP_SETINDEX = &H5          'Set the current Index for multi index help   
Public Const HELP_SETCONTENTS = &H5   
Public Const HELP_CONTEXTPOPUP = &H8   
Public Const HELP_FORCEFILE = &H9   
Public Const HELP_KEY = &H101             'Display topic for keyword in offabData   
Public Const HELP_COMMAND = &H102   
Public Const HELP_PARTIALKEY = &H105      'call the search engine in winhelp   

Function agrega(a$) As String   
    If a$ <> "" Then a$ = a$ + "|"   
    agrega = a$   
End Function   

'Lineas de llamado...   
'NomArchivo = cmd_abre()   
'Si 1 filtro de más >> NomArchivo = cmd_abre("txt") < por ej.   
'Si varios filtros, inicializar los filtros Filtro1 a 4   
'la rutina agrega el filtro todos (*.*)   
Function cmd_Abre(Optional Filt1 As String) As String   
Dim CTRL$, a$, b$, F1$, F2$, F3$, F4$   
CTRL$ = Chr$(13) + Chr$(10)   
    If Filt1 <> "" Then   
        a$ = " Archivo (*." & Filt1 & ") | *." & Filt1   
    End If   
    If Filtro1 <> "" Then   
        a$ = agrega(a$)   
        a$ = a$ + " Archivo (*." & Filtro1 & ") | *." & Filtro1   
        Filtro1 = ""   
    End If   
    If Filtro2 <> "" Then   
        a$ = agrega(a$)   
        a$ = a$ + " Archivo (*." & Filtro2 & ") | *." & Filtro2   
        Filtro2 = ""   
    End If   
    If Filtro3 <> "" Then   
        a$ = agrega(a$)   
        a$ = a$ + " Archivo (*." & Filtro3 & ") | *." & Filtro3   
        Filtro3 = ""   
    End If   
    If Filtro4 <> "" Then   
        a$ = agrega(a$)   
        a$ = a$ + " Archivo (*." & Filtro4 & ") | *." & Filtro4   
        Filtro4 = ""   
    End If   
        a$ = agrega(a$)   
        a$ = a$ + " Todos (*.*) | *.*"   
    Dialog.CMDialog1.Filter = a$   
    Dialog.CMDialog1.FilterIndex = 1   
    Dialog.CMDialog1.FLAGS = CF_EFFECTS Or OFN_HIDEREADONLY Or CF_ANSIONLY   
    Dialog.CMDialog1.Action = DLG_FILE_OPEN   
    cmd_Abrir = Dialog.CMDialog1.FileName   
    Unload Dialog   
End Function   

Function cmd_Police()   
    Dialog.CMDialog1.DialogTitle = "Elección de fuente"   
    Dialog.CMDialog1.FLAGS = CF_WYSIWYG + CF_BOTH + CF_SCALABLEONLY   
    Dialog.CMDialog1.Action = DLG_FONT   
    Filtro1 = Dialog.CMDialog1.FontName   
    Filtro4 = Dialog.CMDialog1.FontSize   
    Filtro5 = Dialog.CMDialog1.FontBold   
    Filtro6 = Dialog.CMDialog1.FontItalic   
    cmd_Police = Dialog.CMDialog1.FontName   
End Function   

Function cmd_Print()   
    Dialog.CMDialog1.FLAGS = PD_ALLPAGES   
    Dialog.CMDialog1.Min = 1   
    Dialog.CMDialog1.Max = 100   
    Dialog.CMDialog1.FromPage = 1   
    Dialog.CMDialog1.ToPage = 100   
    Dialog.CMDialog1.Action = DLG_Print   
    Unload Dialog   
End Function   

'Filt1 = extensión de los archivos a buscar   
'ex: TXT ou EXE   
'la rutina agrega el filtro todos (*.*)   
Function cmd_SaveAs(Filt1 As String) As String   
    Filtro1 = "Archivo (*." & Filt1 & ") | *." & Filt1   
    Filtro2 = "Todos (*.*) | *.*"   
    Dialog.CMDialog1.Filter = Filtro1 + "|" + Filtro2   
    Dialog.CMDialog1.FilterIndex = 1   
    Dialog.CMDialog1.FLAGS = OFN_HIDEREADONLY   
    Dialog.CMDialog1.Action = DLG_FILE_SAVE   
    cmd_SaveAs = Dialog.CMDialog1.FileName   
    Unload Dialog   

End Function

Consulta también

Publicado por Carlos-vialfa. Última actualización: 9 de marzo de 2010 a las 23:07 por Carlos-vialfa.
El documento «VB6/VBA - El control CommonDialog.» se encuentra disponible bajo una licencia Creative Commons. Puedes copiarlo o modificarlo libremente. No olvides citar a CCM (es.ccm.net) como tu fuente de información.