|
-
May 28th, 2004, 10:08 AM
#1
Thread Starter
Junior Member
Select directory
Can anyone help me with VBA in Excel please.
I want the user to be able to select a directory where files are to be saved, or add a new directory which is then created. I have looked at the Common Dialog Control but it is not suitable as it selects a file. Is there anything similar for directories or can someone suggest another method.
As I am a novice, sample code would be much appreciated.
Thanks
-
May 29th, 2004, 12:54 PM
#2
New Member
With microsoft sysinfo control you get dirlistbox and drivelistbox controls.
With those you can handle drives and folders.
Even for novices the best way to learn is to get tips and break by yourself
-
May 29th, 2004, 04:54 PM
#3
Here is an example that I'm sure you will find useful.
You can only see directories and not any files.
VB Code:
Private Type BrowseInfo
hWndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type
Const BIF_RETURNONLYFSDIRS = 1
Const MAX_PATH = 260
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
Private Sub Form_Load()
'KPD-Team 1998
'URL: [url]http://www.allapi.net/[/url]
Dim iNull As Integer, lpIDList As Long, lResult As Long
Dim sPath As String, udtBI As BrowseInfo
With udtBI
'Set the owner window
.hWndOwner = Me.hWnd
'lstrcat appends the two strings and returns the memory address
.lpszTitle = lstrcat("C:\", "")
'Return only if the user selected a directory
.ulFlags = BIF_RETURNONLYFSDIRS
End With
'Show the 'Browse for folder' dialog
lpIDList = SHBrowseForFolder(udtBI)
If lpIDList Then
sPath = String$(MAX_PATH, 0)
'Get the path from the IDList
SHGetPathFromIDList lpIDList, sPath
'free the block of memory
CoTaskMemFree lpIDList
iNull = InStr(sPath, vbNullChar)
If iNull Then
sPath = Left$(sPath, iNull - 1)
End If
End If
MsgBox sPath
End Sub
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jun 1st, 2004, 05:24 AM
#4
Thread Starter
Junior Member
Thanks Jamo,
microsoft sysinfo control sounds like the controls I have come across in VB and they are fairly easy to use, but I can't find them in Excel VB.
RobDog888,
Thanks for the code, can't say I can understand it, but pasted it into my Excel workbook in ThisWorkbook and when I tried to run it I got 'Compile error - method or data member not found' where you set the owner window on the .hwnd.
Created a form and put it there and still get same message. Is there a reference I need to tick in Tools, References?
Sorry, this really is above me at the mo.
Thanks for your help guys.
-
Jun 1st, 2004, 05:40 AM
#5
The owner shouldn't be an issue, as most API's that ask for it (and don't directly affect a form) are happy with it not being set.
You can either set it to a hWnd if you have one (eg: Me.hWnd, or Form1.hWnd), or you can set it to 0.
-
Jun 1st, 2004, 10:11 AM
#6
Sorry for that. I forgot to change it for Excel. The line
".hWndOwner = Me.hWnd" should be ".hWndOwner = Application.Hwnd".
HTH
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jun 9th, 2004, 07:41 AM
#7
Thread Starter
Junior Member
Thanx RobDog888,
Changed code as suggested and it works fantastic.
Sorry too so long to get back, due to workload.
Thanks for all your advice.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|