CFileDialog code generator

To often I have found my self in a situation where i needed to save or open a file through the standard common dialogs. The MFC implementation of the common dialog is quite elegant, but it involves alot of parameters, which are often hard to remember.

This macro will take you through a few steps, in a wizard like mode, and create the right code for you.

FileDialogHandler


Sub FileDialogHandler()
'DESCRIPTION: Automatically inserts the statements needed for a file-open/file-save dialog

    dim strExt
    
    writeln " {// BLOCK - inserted by CFileDialog macro"

    ' Ask for the type of dialog
    dim bSaveDialog
    bSaveDialog = 1
    bAnswer = MsgBox("Do you want to make a save dialog (Yes) or an open dialog (No)", vbYesNo)

    ActiveDocument.Selection.StartOfLine dsFirstText
    ActiveDocument.Selection = " CFileDialog dlg("
    if bAnswer = vbYes then
    ActiveDocument.Selection = "FALSE"
    else
    ActiveDocument.Selection = "TRUE"
    bSaveDialog = 0
    end if

    ' The extension of the file
    strExt = InputBox("What is the extension of the filetype?", "Extension", "txt")
    ' The name of the file
    strName = InputBox("What is the name of the filetype?", "Name", "Text File")

    ' Only apply a default extension if it is a save-as dialog
    if bAnswer = vbYes then
        ActiveDocument.Selection = ",""*." & strExt & """"
    else
        ActiveDocument.Selection = ", """""
    end if 

    ActiveDocument.Selection = ", "

    ' Only  if it is a save-as dialog should a default filename be provided
    dim strDefaultName
    if bAnswer = vbYes then
    strDefaultName = "Untitled." & strExt
        strDefName = InputBox("What is the default filename?", "Default Filename", strDefaultName)
        ActiveDocument.Selection = """" & strDefaultName & """, "
    else
        ActiveDocument.Selection = """"", "
    end if 

    dim flags
    dim filebuffer
    dim bMultiSelectDlg
    bMultiSelectDlg = 0
    filebuffer = "none"
    if bSaveDialog = 0 then
        ' Spxrg om dialog typen
        bAnswer = MsgBox("Do you want a multiple selection dialog?", vbYesNo)

        if bAnswer = vbYes then
            bMultiSelectDlg = 1
            flags = "|OFN_ALLOWMULTISELECT"
            filebuffer = InputBox("How big should the selection buffer be?", "Multiple Selection", "10240")
        else
            flags = ""
        end if
    end if

    ActiveDocument.Selection.NewLine

    ActiveDocument.Selection.StartOfLine dsFirstText
    ActiveDocument.Selection = " OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT" & flags & ", "
    ActiveDocument.Selection.NewLine

    ActiveDocument.Selection.StartOfLine dsFirstText
    ActiveDocument.Selection = " """ & strName & " (*." & strExt & ")|*." & strExt & "|All Files (*.*)|*.*||"", this);"
    ActiveDocument.Selection.NewLine
    ActiveDocument.Selection.NewLine

    if filebuffer <> "none" then
        ActiveDocument.Selection.StartOfLine dsFirstText
        ActiveDocument.Selection = " char cbBuffer[" & filebuffer & "];"
        ActiveDocument.Selection.NewLine

        ActiveDocument.Selection.StartOfLine dsFirstText
        ActiveDocument.Selection = " dlg.m_ofn.nMaxFile = " & filebuffer & ";"
        ActiveDocument.Selection.NewLine

        ActiveDocument.Selection.StartOfLine dsFirstText
        ActiveDocument.Selection = " dlg.m_ofn.lpstrFile = cbBuffer;"
        ActiveDocument.Selection.NewLine

    end if

    ' If you want to use a special caption for your dialog
    dim caption
    if bSaveDialog = 1 then
        szDefCaption = "Save As"
    else
        szDefCaption = "Open"
    end if
    caption = InputBox("What should the caption of the dialog be? (cancel = default)", "Dialog Caption", szDefCaption)
    if caption <> "" then
        ActiveDocument.Selection.StartOfLine dsFirstText
        ActiveDocument.Selection = " dlg.m_ofn.lpstrTitle = " & caption & ";"
        ActiveDocument.Selection.NewLine        
    end if

    ' Initial directory to look in
    if bSaveDialog = 1 then
        szDefCaption = "Save As"
    else
        szDefCaption = "Open"
    end if
    caption = InputBox("Initial directory to search for files (cancel = default)? If you're using a string in stead of a variable put the string in quotation marks", "Dialog Caption", "")
    if caption <> "" then
        ActiveDocument.Selection.StartOfLine dsFirstText
        ActiveDocument.Selection = " dlg.m_ofn.lpstrInitialDir = " & caption & ";"
        ActiveDocument.Selection.NewLine        
    end if

    ActiveDocument.Selection.NewLine

    ActiveDocument.Selection.StartOfLine dsFirstText
    ActiveDocument.Selection = " if (dlg.DoModal() == IDOK)"
    ActiveDocument.Selection.NewLine

    ActiveDocument.Selection.StartOfLine dsFirstText
    ActiveDocument.Selection = " {"
    ActiveDocument.Selection.NewLine

    ActiveDocument.Selection.StartOfLine dsFirstText
    ActiveDocument.Selection = " // Insert your code here..."
    ActiveDocument.Selection.NewLine

    if bMultiSelectDlg = 1 then 
        ActiveDocument.Selection.StartOfLine dsFirstText
        ActiveDocument.Selection = " // Insert your code here..."
        ActiveDocument.Selection.NewLine

        ActiveDocument.Selection.StartOfLine dsFirstText
        ActiveDocument.Selection = " POSITION pos;"
        ActiveDocument.Selection.NewLine
        ActiveDocument.Selection.StartOfLine dsFirstText
        ActiveDocument.Selection = " for (pos = dlg.GetStartPosition() ; pos != NULL ; )"
        ActiveDocument.Selection.NewLine
        ActiveDocument.Selection.StartOfLine dsFirstText
        ActiveDocument.Selection = " {"
        ActiveDocument.Selection.NewLine
        ActiveDocument.Selection.StartOfLine dsFirstText
        ActiveDocument.Selection = " // Use dlg.GetNextPathName(pos); to extract filename (including path)"
        ActiveDocument.Selection.NewLine
        ActiveDocument.Selection.StartOfLine dsFirstText
        ActiveDocument.Selection = " {"
        ActiveDocument.Selection.NewLine
    else
        ActiveDocument.Selection.StartOfLine dsFirstText
        ActiveDocument.Selection = " // Use dlg.GetPathName() to extract filename (including path);"
        ActiveDocument.Selection.NewLine
    end if

    ActiveDocument.Selection.StartOfLine dsFirstText
    ActiveDocument.Selection = " }"
    ActiveDocument.Selection.NewLine

    ActiveDocument.Selection.StartOfLine dsFirstText
    ActiveDocument.Selection = " } // End block"
    ActiveDocument.Selection.NewLine

    ActiveDocument.Selection.StartOfLine dsFirstText

'End Recording
End Sub

A note about WriteLn

Because of compatibility reasons (I dont want people to have to copy one macro to use another), I have not used the function below to write text. However it is pretty handy, and will make your code look alot better. It does the same as WriteLn from pascal.
This function is not meant to be called by the user, but only from other scripts.


sub writeln(line)
    ActiveDocument.Selection.StartOfLine dsFirstText
    ActiveDocument.Selection = line
    ActiveDocument.Selection.NewLine
end sub



Comments

  • Excellent Macro

    Posted by Legacy on 04/06/2000 12:00am

    Originally posted by: Jaz

    This is a wonderful macro. Thank you for your work.

    Reply
  • Small update

    Posted by Legacy on 05/17/1999 12:00am

    Originally posted by: Yehuda Hahn

    The code as published puts the dialog title without surrounding quotes. To change this behavior, change the line that reads:

    ActiveDocument.Selection = " dlg.m_ofn.lpstrTitle = " & caption & ";"

    to
    ActiveDocument.Selection = " dlg.m_ofn.lpstrTitle = """ & caption & """;"


    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • CentreCorp is a fully integrated and diversified property management and real estate service company, specializing in the "shopping center" segment, and is one of the premier retail service providers in North America. Company executives travel a great deal, carrying a number of traveling laptops with critical current business data, and no easy way to back up to the network outside the office. Read this case study to learn how CentreCorp implemented a suite of business continuity services that included …

Most Popular Programming Stories

More for Developers

RSS Feeds