Click to See Complete Forum and Search --> : choosing a file


skined
February 15th, 2000, 06:06 AM
i`m developing macros in Visual Basic for word2000.
i need some method of allowing the user to move around a directory structure
and choose a file, which i then need to know the full path of.
trouble is, the obvious controls which are available to the full version of
VB such as drivebox,dirbox,filebox aren`t available in VB for word2000.
can anyone provide some advice as to how to acomplish this?
cheers in advance

Lothar Haensler
February 15th, 2000, 06:28 AM
if VB is installed on your machine you can add additional ActiveX controls to the Toolbox in Word VB editor.
Add a MS Common Dialog control to your toolbox and call the ShowOpen method of that control.
The .FileName property will tell you the complete path of the file chosen by the user.

skined
February 15th, 2000, 06:41 AM
unfortunately the full version of VB isn`t available where i work. attempting to add the common dialog control to the toolbox and placing it onto a
form leads to VBA telling me that the control isn`t licensed.
so i cant go down that avenue :(
any other suggestions?

Lothar Haensler
February 15th, 2000, 07:25 AM
I don't know enough about Word VBA.
If it supports VB's Declare Syntax for declaring (and later calling) DLLs, you can use the GetOpenFileName API call to achieve the exact same result without any OCX.

Kyle Burns
February 15th, 2000, 10:09 AM
The Common Dialog Control is unnecessary in your application. You can acheive the same functionality, while not as easily, with the Windows API. The following code to accomplish what you need is taken primarily from the API section of Ken Getz & Mike Gilbert's book "VBA Developers Handbook" (Sybex)

option Explicit

'constants for dialog

public Const OFN_READONLY as Long = &H1
public Const OFN_OVERWRITEPROMPT as Long = &H2
public Const OFN_HIDEREADONLY as Long = &H4
public Const OFN_NOCHANGEDIR as Long = &H8
public Const OFN_SHOWHELP as Long = &H10
public Const OFN_ENABLEHOOK as Long = &H20
public Const OFN_ENABLETEMPLATE as Long = &H40
public Const OFN_ENABLETEMPLATEHANDLE as Long = &H80
public Const OFN_NOVALIDATE as Long = &H100
public Const OFN_ALLOWMULTISELECT as Long = &H200
public Const OFN_EXTENSIONDIFFERENT as Long = &H400
public Const OFN_PATHMUSTEXIST as Long = &H800
public Const OFN_FILEMUSTEXIST as Long = &H1000
public Const OFN_CREATEPROMPT as Long = &H2000
public Const OFN_SHAREAWARE as Long = &H4000
public Const OFN_NOREADONLYRETURN as Long = &H8000
public Const OFN_NOTESTFILECREATE as Long = &H10000
public Const OFN_NONETWORKBUTTON as Long = &H20000
public Const OFN_NOLONGNAMES as Long = &H40000
public Const OFN_EXPLORER as Long = &H80000
public Const OFN_NODEREFERENCELINKS as Long = &H100000
public Const OFN_LONGNAMES as Long = &H200000
public Const OFN_ENABLEINCLUDENOTIFY as Long = &H400000
public Const OFN_ENABLESIZING as Long = &H800000

Const OFN_OPENEXISTING as Long = OFN_PATHMUSTEXIST _
Or OFN_FILEMUSTEXIST Or OFN_HIDEREADONLY

Const OFN_SAVENEW as Long = OFN_PATHMUSTEXIST _
Or OFN_OVERWRITEPROMPT Or OFN_HIDEREADONLY

Const OFN_SAVENEWPATH as Long = OFN_OVERWRITEPROMPT Or OFN_HIDEREADONLY


Type OPENFILENAME
lngStructSize as Long
hwndOwner as Long
hInstance as Long
strFilter as string
strCustomFilter as string
intMaxCustFilter as Long
intFilterIndex as Long
strFile as string
intMaxFile as Long
strFileTitle as string
intMaxFileTitle as Long
strInitialDir as string
strTitle as string
lngFlags as Long
intFileOffset as Integer
intFileExtension as Integer
strDefExt as string
lngCustData as Long
lngfnHook as Long
strTemplateName as string
End Type

Declare Function GetOpenFileName Lib "comdlg32.dll" _
Alias "GetOpenFileNameA" (ofn as OPENFILENAME) as Boolean

Declare Function GetSaveFileName Lib "comdlg32.dll" _
Alias "GetSaveFileNameA" (ofn as OPENFILENAME) as Boolean

Declare Function GetActiveWindow Lib "User32" () as Long



public Function dhFileDialog(optional strInitDir as string, _
optional strFilter as string = "All files (*.*)" & vbNullChar & _
"*.*" & vbNullChar & vbNullChar, _
optional intFilterIndex as Integer = 1, _
optional strDefaultExt as string, _
optional strFileName as string, _
optional strDialogTitle as string = "Open File", _
optional hwnd as Long = -1, _
optional fOpenFile as Boolean = true, _
optional byref lngFlags as Long = OFN_OPENEXISTING) as Variant

Dim ofn as OPENFILENAME
Dim strFileTitle as string
Dim fResult as Boolean

If strInitDir = "" then strInitDir = CurDir

If hwnd = -1 then hwnd = GetActiveWindow()

strFileName = strFileName & string(255 - len(strFileName), 0)
strFileTitle = string(255, 0)

With ofn
.lngStructSize = len(ofn)
.hwndOwner = hwnd
.strFilter = strFilter
.intFilterIndex = intFilterIndex
.strFile = strFileName
.intMaxFile = len(strFileName)
.strFileTitle = strFileTitle
.intMaxFileTitle = len(strFileTitle)
.strTitle = strDialogTitle
.lngFlags = lngFlags
.strDefExt = strDefaultExt
.strInitialDir = strInitDir
.hInstance = 0
.strCustomFilter = string(255, 0)
.intMaxCustFilter = 255
.lngfnHook = 0
End With

If fOpenFile then
fResult = GetOpenFileName(ofn)
else
fResult = GetSaveFileName(ofn)
End If

If fResult then
lngFlags = ofn.lngFlags

dhFileDialog = dhTrimNull(ofn.strFile)
else
dhFileDialog = null
End If
End Function

Function dhTrimNull(byval Expression as string)
Dim iPos as Integer
iPos = InStr(Expression, vbNullChar)
Select Case iPos
Case 0
dhTrimNull = Expression
Case 1
dhTrimNull = vbNullString
Case is > 1
dhTrimNull = Left$(Expression, iPos - 1)
End Select
End Function



Hope that helps!