Results 1 to 12 of 12
  1. #1
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using/Creating a File Open dialog but not opening (VBA Word 2000)

    I've seen a lot of posts about the FileOpen dialog box and maybe I just don't understand it enough to use it for my needs. I have a macro that currently installs a .dot file as a global template, but the path is assumed to be the same directory as the file that is running the macro. Sometimes that might be true and sometimes not.

    I'd like to be able to display a File Open dialog box where the user can navigate to the place where the .dot file is that they want to install and provide the path and file name as output. The code I have can take that file name and path from there.

    Keep in mind this is VBA, not VB. I saw some good examples for VB, but could not figure it out for use in VBA.

    Thanks for your help!!
    Troy

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Using/Creating a File Open dialog but not opening (VBA Word 2000)

    Troy,

    As a starting point, you can use the FileCopy dialog to return the directory (and then concatenate that with the .dot name, since you already know the name):

    <pre>Public Sub ReturnPath()

    Dim strFileDir As String

    With Dialogs(wdDialogCopyFile)
    .Display
    strFileDir = .Directory
    End With

    MsgBox strFileDir


    End Sub</pre>

    But I think there is an even better dialog you can use that gets created via an API call - think it has been posted on this forum - will have to have a look for it.

    Gary

  3. #3
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: BrowseForFolder function

    See <post#=28049>post 28049</post#> for code for the SHBrowseForFolder function.

  4. #4
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using/Creating a File Open dialog but not opening (VBA Word 2000)

    Gary,
    That BrowseFolder thing looks great, EXCEPT I obviously did not make one thing clear (I have a bad habit of not quite telling "the rest of the story" <img src=/S/blush.gif border=0 alt=blush width=15 height=15> . I want to make this "install" program generic to install one of several global template files. Thus, I will NOT know the name of the dot file. I'll need to be able to select the file and get both the full path and the file name.

    Sorry for the monkey wrench!! Thanks again for your help!!
    Troy

  5. #5
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Using/Creating a File Open dialog but not opening (VBA Word 2000)

    Hi Troy,

    The BrowseForFolder code can be adjusted to allow the inclusion if files. Adapt the code already referred to by Gary, by replacing bInfo.ulFlags = &H1 with bInfo.ulFlags = &H4000 in the Function GetDirectory, which you might want to rename, and perhaps change the title to Select a File.

    Hope it helps,

    Andrew C

  6. #6
    New Lounger
    Join Date
    Aug 2002
    Location
    Geelong, Victoria, Australia
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using/Creating a File Open dialog but not opening (VBA Word 2000)

    Hyperlinks and "pre" tags added- Mod

    Have look at using the Wordbasic FileNameInfo() command as described here:

    http://www.mvps.org/word/FAQs/MacrosVBA/Wo...sicCommands.htm

    Specifically:

    "One case where FileNameInfo$ is very useful is to get the pathname of a file which has just been selected by the user in the FileOpen dialog. The following code returns the full pathname of a file selected by the user. "

    <pre>With Dialogs(wdDialogFileOpen)
    If .Display Then
    MsgBox WordBasic.FilenameInfo$(.Name, 1)
    Else
    MsgBox "No file selected"
    End If
    End With
    </pre>


  7. #7
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using/Creating a File Open dialog but not opening (VBA Word 2000)

    <P ID="edit" class=small>Edited by TroyWells on 04-Aug-02 13:02.</P>Andrew and Gary,

    I think this is very close to what I need. However, I know nothing about API lingo and need just a few more things:

    1. How can I change the title of the dialog box to "Select a file"?

    2. Can I filter the dialog box to only show .dot files?

    EDITED: Nevermind #3 below: I found I could use the WordBasic.FilenameInfo$ command mentioned in the string from interfact to retrieve this information.
    3. While I need the full path and file name as one output, I also need the file name by itself for a separate line in my code (uninstalling and deleting the previous version of the template if it exists). How can I get just the file name as a separate variable?

    Thanks again!!
    Troy

  8. #8
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Using/Creating a File Open dialog but not opening (VBA Word 2000)

    Troy,

    In the Function GetDirectory posted by Brooke, the Optional parameter is the title you want to appear on the dialog box, so if you were to call the function like this :

    GetDirectory ("Select a File"),

    it should do what you want.

    Unfortunately with SHBrowseForFolder I do not think you can apply a filter to restrict selection to specified files. There is another API, GetOpenFileName , which might be better suited to your needs. The code to use it (below) looks cumbersome because it requires a User Defined Type as a parameter. I am also using two other API calls, GetActiveWindow to provide the main function with a Window Handle (though you could manage by just using zero) and GetFileTitle to parse the filename from the full path in the Function GetFileNameFromPath.

    Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" ( _
    pOpenfilename As OpenFileName) As Long

    Private Declare Function GetActiveWindow Lib "user32" () As Long

    Private Declare Function GetFileTitle Lib "comdlg32.dll" Alias "GetFileTitleA" ( _
    ByVal lpszFile As String, _
    ByVal lpszTitle As String, _
    ByVal cbBuf As Integer) As Integer

    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

    Sub BrowseForFile()
    Dim FName As OpenFileName
    FName.lStructSize = Len(FName)
    FName.hwndOwner = GetActiveWindow()
    FName.lpstrFilter = "Word Template Files (*.dot)" + Chr$(0) + "*.dot" _
    + Chr$(0) + "All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
    FName.lpstrFile = Space$(254)
    FName.nMaxFile = 255
    FName.lpstrFileTitle = Space$(254)
    FName.nMaxFileTitle = 255
    FName.lpstrInitialDir = "C:" 'Set the initial directory to browse from
    FName.lpstrTitle = "Select Template to Install"
    FName.flags = 0
    If GetOpenFileName(FName) Then
    MsgBox "Template FullName: " & Trim(FName.lpstrFile)
    MsgBox "Template FileName: " & GetFileNameFromPath(Trim(FName.lpstrFile))
    Else
    MsgBox "Cancel was pressed"
    End If
    End Sub

    Function GetFileNameFromPath(strPath As String) As String
    Dim strPathBuffer As String
    strPathBuffer = String(255, 0)
    GetFileTitle strPath, strPathBuffer, Len(strPathBuffer)
    GetFileNameFromPath = Left(strPathBuffer, InStr(1, strPathBuffer, Chr$(0)) - 1)
    End Function

    Andrew

  9. #9
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using/Creating a File Open dialog but not opening (VBA Word 2000)

    Andrew,
    That code does everything I wanted and then some. Thanks so much for your help.

    Hats off to you!! <img src=/S/bravo.gif border=0 alt=bravo width=16 height=30>
    Troy

  10. #10
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using/Creating a File Open dialog but not opening (VBA Word 2000)

    Andrew,

    You gave the following code for separating a file name from a full path:

    <pre>Function GetFileNameFromPath(strPath As String) As String
    Dim strPathBuffer As String
    strPathBuffer = String(255, 0)
    GetFileTitle strPath, strPathBuffer, Len(strPathBuffer)
    GetFileNameFromPath = Left(strPathBuffer, InStr(1, strPathBuffer, Chr$(0)) - 1)
    End Function</pre>


    That has worked great on all Windows 2000 machines I've tried. However, I did a demo today of some code that used that function on some NT 4 machines (still with Word 2000). When the function was finished, the output was the file name minus the three digit file extension (filename instead of filename.dot).

    Any idea why it worked that way?

    Thanks!!
    Troy

  11. #11
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Using/Creating a File Open dialog but not opening (VBA Word 2000)

    Troy,

    Could it be that Windows File Explorer is configured to Hide extensions for known file types on the NT systems ?

    The GetFileTitle seems to take this setting into account when returning the file name.

    If this does not suit, try an alternate FileSystemObject method : <pre> Function fsoGetFileNameFromPath(strPath As String) As String
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    fsoGetFileNameFromPath = fso.GetFileName(strPath)
    End Function </pre>


    Andrew

  12. #12
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using/Creating a File Open dialog but not opening (VBA Word 2000)

    Regarding

    "Could it be that Windows File Explorer is configured to Hide extensions for known file types on the NT systems"

    That was exactly the case. However, I found that even if I changed this setting, I still had problems. Perhaps you have to change the setting and reboot? I had no idea that was a limitation of the previous function.

    Anyway, the other function you gave works like a charm with either setting.

    Thanks for your speedy response!!
    Troy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •