Need to locate a folder in vba

Started by Rich_T, October 04, 2013, 07:10:29 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Rich_T

In my Visio 2010 application, I ask the user to navigate to a directory that contains a set of images to be included in the diagram.

I tried to use the object Application.FileDialog(msoFileDialogFolderPicker) but it gives me an error that
Object Doesn't Support this property or Method.

How does one select a folder in Visio?


JuneTheSecond

Hi!
Try application.DoCmd visFileOpen.
Best Regards,

Junichi Yoda
http://june.minibird.jp/

Rich_T

JuneTheSecond - That command does spawn a dialog box and opens a but isn't what I'm looking for. Thanks for your help though! 

The program isn't trying to Open the Files yet, its just asking the use to identify where the files are located to be open later.
So I need a function that would return the  string that contains the name of the directory.

Later in my program I import a large numbe of images with filenames generated from shape data fields and name of the directory.

Any other ideas?

JuneTheSecond

#3
Then I think you can manipulate folders with Scripting.FileSystemObject Object.
http://msdn.microsoft.com/en-us/library/aa242706(v=vs.60).aspx

For example,
several weeks ago, 
I made a vba macro
that imports all images in a certain folder.


Sub SampleImportJPG()
    Dim f As Object
    Dim flname As String, fname As String ' File name with expansion or without
    Dim fdname As String  ' Folder name.
    Dim fulname As String   'Full name with folder name.
    Dim ftype As String   ' File type to check if the file is jpg file.
    Dim shp As Visio.Shape
       
    fdname = "C:\Users\Yoda Junichi\Desktop\MyFolder"
   
    With CreateObject("Scripting.FileSystemObject")
        For Each f In .GetFolder(fdname).Files

            flname = f.Name
            ftype = Right(flname, 3)
            fname = Left(flname, Len(flname) - 4)
            fulname = fdname + "\" + flname

            If ftype = "JPG" Or ftype = "jpg" Then
                ActivePage.Import fulname
                Set shp = ActiveWindow.Selection(1)
                shp.Cells("LockAspect").FormulaU = "1"
                shp.Text = fname
            End If
        Next f
    End With
End Sub
Best Regards,

Junichi Yoda
http://june.minibird.jp/

Rich_T

JuneTheSecond,
     
Thanks again for your help.  It's a little closer to what I'm looking for but not yet it.  In your sample code, you set the path with the command:

fdname = "C:\Users\Yoda Junichi\Desktop\MyFolder"

I'm looking for a way to let the user navigate using File Explorer to a folder and then set a string to the folder name the user  navigated to. 

Thanks again for your help....

JuneTheSecond

I am sorry I cannot image other way more than to use Excel common dialog or to make add-in using VB or C# in Visual Studio express.
Best Regards,

Junichi Yoda
http://june.minibird.jp/

Jumpy

As JuneTheSecond said: You either have to use Excel's dialogs or you try WinAPI (?) functions for this. The following is untested (first google hit) but I used sth. similar already:


Function selectOutputFolder(lastPath As String) As String
    Const BIF_NEWDIALOGSTYLE = &H00000040
    Dim objShell As Variant
    Dim objFolder As Variant
    Dim objFolderItem As Variant
    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.BrowseForFolder(0, "Choose a directory", BIF_NEWDIALOGSTYLE, lastPath)
    If Not (objFolder Is Nothing) Then
        Set objFolderItem = objFolder.Self
        selectOutputFolder = objFolderItem.Path
    End If
End Function

AndyW

Live life with an open mind

Rich_T

Jumpy - That solution worked great!  Thanks you very much for your help.   There was good flexiblity in what was displayed during the navigation process.

Thank you AndyW and JuneTheSecond for your suggestions as well.