ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Creating a directory from within Excel VBA (https://www.excelbanter.com/excel-programming/408570-creating-directory-within-excel-vba.html)

Jo Gjessing

Creating a directory from within Excel VBA
 
Hi all,

I'm creating a little VBA script within Ms Excel, wanting the script to
change directory before writing a file, using the ChangeFileOpenDirectory
method. It works fine. But what if the directory does not exist? Then I want
the script to create it. Can you please tell me how I make it do so?

Thank you very much in advance.

Jo

Mike H

Creating a directory from within Excel VBA
 
One way

Sub stantial()
On Error GoTo ErrNotExist
ChDir ("C:\Mydir")
Exit Sub
ErrNotExist:
MkDir "c:\Mydir"
End Sub

Mike

"Jo Gjessing" wrote:

Hi all,

I'm creating a little VBA script within Ms Excel, wanting the script to
change directory before writing a file, using the ChangeFileOpenDirectory
method. It works fine. But what if the directory does not exist? Then I want
the script to create it. Can you please tell me how I make it do so?

Thank you very much in advance.

Jo


joel

Creating a directory from within Excel VBA
 
You could also try this

Sub selectfolder()
Dim objShell As Object, objFolder As Object

Set objShell = CreateObject("Shell.Application")
On Error Resume Next
Set objFolder = objShell.BrowseForFolder(&H0&, "Select Folder ", &H1&)
If Not objFolder Is Nothing Then
Set oFolderItem = objFolder.Items.Item
MyPath = oFolderItem.Path
ChDir (MyPath)

End If

End Sub

"Mike H" wrote:

One way

Sub stantial()
On Error GoTo ErrNotExist
ChDir ("C:\Mydir")
Exit Sub
ErrNotExist:
MkDir "c:\Mydir"
End Sub

Mike

"Jo Gjessing" wrote:

Hi all,

I'm creating a little VBA script within Ms Excel, wanting the script to
change directory before writing a file, using the ChangeFileOpenDirectory
method. It works fine. But what if the directory does not exist? Then I want
the script to create it. Can you please tell me how I make it do so?

Thank you very much in advance.

Jo



All times are GMT +1. The time now is 10:37 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com