PC Review


Reply
Thread Tools Rate Thread

Creating a directory from within Excel VBA

 
 
Jo Gjessing
Guest
Posts: n/a
 
      31st Mar 2008
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
 
Reply With Quote
 
 
 
 
Mike H
Guest
Posts: n/a
 
      31st Mar 2008
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

 
Reply With Quote
 
Joel
Guest
Posts: n/a
 
      31st Mar 2008
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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating an excel macro to open and save sheets to a different directory sem Microsoft Excel Programming 0 29th Sep 2005 07:36 PM
Creating a directory =?Utf-8?B?Q2hhcmxlcyBUYW0=?= Microsoft Access Form Coding 4 20th Apr 2005 01:37 PM
Creating a macro that lists directory names within a directory.... =?Utf-8?B?QW5keQ==?= Microsoft Excel Programming 4 28th Nov 2004 06:13 AM
creating directory Phil Microsoft Access 1 22nd Sep 2003 10:02 PM
Re: Creating directory bug Rick Microsoft Windows 2000 1 8th Aug 2003 08:25 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:39 PM.