Open File Common Control Dialog Replacement for VBA

Using the OpenFile common control dialog adds a look of consistency to your application. The OpenFile dialog is part of the comdlg32.dll library ofWindows API routines and is easily accessed. The following example illustrates using these routines to request a drawing file to open.

Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias

"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long Private Type OPENFILENAME lStructSize As Long hwndOwner As Long hInstance As Long lpstrFilter As String lpstrCustomFilter As String nMaxCustFilter As Long nFilterIndex As Long lpstrFile As String nMaxFile As Long lpstrFileTitle As String nMaxFileTitle As Long lpstrInitialDir As String lpstrTitle As String flags As Long nFileOffset As Integer nFileExtension As Integer lpstrDefExt As String lCustData As Long lpfnHook As Long lpTemplateName As String End Type

Public Function ShowOpen(Filter As String, _

InitialDir As String, _ DialogTitle As String) As String

Dim OFName As OPENFILENAME

'Set the structure size OFName.lStructSize = Len(OFName) 'Set the owner window OFName.hwndOwner = 0 'Set the filter OFName.lpstrFilter = Filter 'Set the maximum number of chars OFName.nMaxFile = 255 'Create a buffer OFName.lpstrFile = Space(254)

'Create a buffer

OFName.lpstrFileTitle = Space$(254)

'Set the maximum number of chars

OFName.nMaxFileTitle = 255

'Set the initial directory

OFName.lpstrInitialDir = InitialDir

'Set the dialog title

OFName.lpstrTitle = DialogTitle

'no extra flags

OFName.flags = 0

'Show the 'Open File' dialog

If GetOpenFileName(OFName) Then

ShowOpen = Trim(OFName.lpstrFile)

Else ShowOpen = "" End If End Function

The following sample code illustrates using the ShowOpen routine, which returns the selected file name as a string.

Dim Filter As String Dim InitialDir As String Dim DialogTitle As String Dim OutputStr As String

Filter = "Drawing Files (*.dwg)" + Chr$(0) + "*.dwg" + Chr$(0) + _

"All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0) InitialDir = "C:\Program Files\AutoCAD 2006\Sample" DialogTitle = "Open a DWG file"

OutputStr = ShowOpen(Filter, InitialDir, DialogTitle) MsgBox OutputStr

The Filter parameter is a string that details which file types, by extension, to display when the OpenFile dialog box appears. The InitialDir parameter specifies which directory to display by default. To give a name to your OpenFile dialog box, use the DialogTitle parameter.

With each of these parameters defined, executing this code results in the OpenFile dialog box shown in Figure 18-2.

Look in: |o Sample

©Line weights.dwg ©ilfl^P^n.dwq ©Oil Module.dwg

©ffeiiSS'eenirig' and FIII:.Kattei"ns.cfwij ;©SPCA Site Plan.dwq ^Stadium North Elevation.dwg ©Stadium Plan.dwg ■©Stadium South Elevaton.dwq jfljil

tSsth floor.dwg:

Ecolorwh.dwg Ë3JI. p.dwg 0HOÔÎ Model.dwg ©Hummer Elevation.dwg tSsth floor.dwg:

Ecolorwh.dwg Ë3JI. p.dwg 0HOÔÎ Model.dwg ©Hummer Elevation.dwg pan*

©8th floor furrirKjTr^'.dwg ©8th floor hvac [>ig ©8th floor li._r L" g. d wq E):JJ floor plan.dwg ©8th floor plumbing.dwg ©8th floor r.dwq lEPTalsel Detail Plarr.y&g: ©Taisei Door Window Sheetdwg ©Talsel Interior EIV .dwg ©TrueType.dwg

Future- l.dwg ©Welding FIxttMfitodel.dwgi. ©Wllhome.dwg

©Line weights.dwg ©ilfl^P^n.dwq ©Oil Module.dwg

©ffeiiSS'eenirig' and FIII:.Kattei"ns.cfwij ;©SPCA Site Plan.dwq ^Stadium North Elevation.dwg ©Stadium Plan.dwg ■©Stadium South Elevaton.dwq

Figure 18-2. An OpenFile dialog box example

Was this article helpful?

0 -1

Post a comment