Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 45
Default Create directory on hard drive?

I need to export some data to a specific folder on the harddrive - via VBA!

Is it possible to create a directory on the harddrive via VBA if you know
the path and the foldername(s)?

TIA,


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Create directory on hard drive?

sPath = Application.DefaultFilePath & "\"
sFldr = "TestFolder"
MkDir sPath & sFldr

It will error if the folder already exists or do not have rights to the path

Regards,
Peter T

"Charlotte E." wrote in message
...
I need to export some data to a specific folder on the harddrive - via VBA!

Is it possible to create a directory on the harddrive via VBA if you know
the path and the foldername(s)?

TIA,




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Create directory on hard drive?

Charlotte,

Try this. Note the directory structure is built in stages and before each
stage there is a check to see if the directory exists.

Sub makedir()
If Dir("C:\yyy") < "" Then
Resume Next
Else
MkDir "C:\yyy"
End If
If Dir("C:\yyy\zzz") < "" Then
Resume Next
Else
MkDir "C:\yyy\zzzz"
End If
End Sub

Mike

"Charlotte E." wrote:

I need to export some data to a specific folder on the harddrive - via VBA!

Is it possible to create a directory on the harddrive via VBA if you know
the path and the foldername(s)?

TIA,



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Create directory on hard drive?

After my signature is some code that I have posted in the past to the
compiled VB newsgroups, but which will work fine in Excel VBA as well. Note
that it is presented as a Function which means you can check if it worked
okay or not using a structure like this...

If MakeDirectoryPath("c:\dir1\dir2\dir3\dir4") Then
MsgBox "Directory path created successfully"
Else
MsgBox "An error occurred while trying to create that directory"
End If

allowing you to take any steps necessary for either case. Or, if you want to
omit the check, you can call it directly as if it were a subroutine (you can
do this for any function) using either this...

MakeDirectoryPath "c:\dir1\dir2\dir3\dir4"

or this...

Call MakeDirectoryPath("c:\dir1\dir2\dir3\dir4")

Rick

Here is a subroutine (VB6) that will create of all the sub-directories (that
do not already exist) for a specified path argument. The error checking was
made up off the top of my head, but I think it covers the problems that one
might encounter. Note that the path must start with a drive letter, followed
by a colon, followed by a backslash, followed by the directories path.

Function MakeDirectoryPath(ByVal Path As String) As Boolean
Dim X As Long
Dim NewPath As String
Dim Parts() As String
On Error GoTo OOPS
If Path Like "[a-zA-Z]:\*" And InStr(Path, "\\") = 0 Then
If Len(Dir$(Left$(Path, 3))) = 0 Then Exit Function
Parts = Split(Path, "\")
Parts(0) = Parts(0) & "\"
NewPath = Parts(0)
For X = 0 To UBound(Parts)
If Len(Dir$(NewPath, vbDirectory)) = 0 Then MkDir NewPath
If Right$(NewPath, 1) < "\" Then NewPath = NewPath & "\"
If X < UBound(Parts) Then NewPath = NewPath & Parts(X + 1)
Next
MakeDirectoryPath = True
End If
OOPS:
End Function



"Charlotte E." wrote in message
...
I need to export some data to a specific folder on the harddrive - via VBA!

Is it possible to create a directory on the harddrive via VBA if you know
the path and the foldername(s)?

TIA,



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
HARD DRIVE DENISE New Users to Excel 7 March 11th 09 08:06 PM
Links to mapped drive change to refer to local hard drive SueD Links and Linking in Excel 1 May 8th 08 11:42 AM
Can I save to hard drive AND my flash drive at the same time? Gizelle Excel Discussion (Misc queries) 3 July 24th 06 08:27 PM
Save to hard drive and backup to thumb drive. sungen99[_34_] Excel Programming 22 January 27th 06 01:17 PM
Erase Hard Drive Mark C[_4_] Excel Programming 2 February 27th 04 09:24 AM


All times are GMT +1. The time now is 11:31 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"