Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Save file in a new folder, but create folder only if folder doesn't already exist?


I am trying to generate reports and save the files in a folder, but I
want to create the folder if it does not already exist. If it does
exist, I want it to just save the file there. Right now I am just
setting it to make a directory, which was fine, but if I have other
reports going to that same folder and the folder already exists, I get
a "Path/File Access error" Is there a little line of code I can put in
that first checks to see if the folder exists?

Thanks,

~J

MyDate = Trim(VBA.Format(Now(), "MM-DD-YY"))
Application.DisplayAlerts = False
MkDir "G:\Reports\Friday Reports\" & MyDate
ActiveWorkbook.SaveAs filename:="G:\Reports\Friday Reports\" & _
MyDate & "\" & filename


--
nbaj2k
------------------------------------------------------------------------
nbaj2k's Profile: http://www.excelforum.com/member.php...o&userid=36480
View this thread: http://www.excelforum.com/showthread...hreadid=570801

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Save file in a new folder, but create folder only if folder doesn't already exist?

Hi nbaj2k

Use this function
See the test macro how you can save the activeworkbook in the dir C:\MyDir

Function DirectoryExist(sstr As String)
'Tom Oglivy
Dim lngAttr As Long
DirectoryExist = False
If Dir(sstr, vbDirectory) < "" Then
lngAttr = GetAttr(sstr)
If lngAttr And vbDirectory Then _
DirectoryExist = True
End If
End Function

Sub Test()
Dim dirstr As String
Dim wb As Workbook

Set wb = ActiveWorkbook

dirstr = "C:\MyDir"
If Not DirectoryExist(dirstr) Then
MkDir dirstr
wb.SaveAs dirstr & "\ron.xls"
Else
wb.SaveAs dirstr & "\ron.xls"
End If
End Sub


--
Regards Ron de Bruin
http://www.rondebruin.nl



"nbaj2k" wrote in message
...

I am trying to generate reports and save the files in a folder, but I
want to create the folder if it does not already exist. If it does
exist, I want it to just save the file there. Right now I am just
setting it to make a directory, which was fine, but if I have other
reports going to that same folder and the folder already exists, I get
a "Path/File Access error" Is there a little line of code I can put in
that first checks to see if the folder exists?

Thanks,

~J

MyDate = Trim(VBA.Format(Now(), "MM-DD-YY"))
Application.DisplayAlerts = False
MkDir "G:\Reports\Friday Reports\" & MyDate
ActiveWorkbook.SaveAs filename:="G:\Reports\Friday Reports\" & _
MyDate & "\" & filename


--
nbaj2k
------------------------------------------------------------------------
nbaj2k's Profile: http://www.excelforum.com/member.php...o&userid=36480
View this thread: http://www.excelforum.com/showthread...hreadid=570801



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default Save file in a new folder, but create folder only if folder doesn'

Hello,
Another way is to trap the error in an error routine.
The error number if the directory already exists is 75
Add you sub to include:

Sub makefolder()
On Error GoTo errorTrap:
MyDate = Trim(VBA.Format(Now(), "MM-DD-YY"))
Application.DisplayAlerts = False
MkDir "C:\Reports\Friday Reports\" & MyDate
ActiveWorkbook.SaveAs Filename:="c:\AAAA & " \ " & Filename"
Exit Sub
errorTrap:
If Err.Number = 75 Then
Resume Next
End If

MsgBox Err.Number & " " & Err.Description
End Sub



"nbaj2k" wrote:


I am trying to generate reports and save the files in a folder, but I
want to create the folder if it does not already exist. If it does
exist, I want it to just save the file there. Right now I am just
setting it to make a directory, which was fine, but if I have other
reports going to that same folder and the folder already exists, I get
a "Path/File Access error" Is there a little line of code I can put in
that first checks to see if the folder exists?

Thanks,

~J

MyDate = Trim(VBA.Format(Now(), "MM-DD-YY"))
Application.DisplayAlerts = False
MkDir "G:\Reports\Friday Reports\" & MyDate
ActiveWorkbook.SaveAs filename:="G:\Reports\Friday Reports\" & _
MyDate & "\" & filename


--
nbaj2k
------------------------------------------------------------------------
nbaj2k's Profile: http://www.excelforum.com/member.php...o&userid=36480
View this thread: http://www.excelforum.com/showthread...hreadid=570801


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Save file in a new folder, but create folder only if folder doesn't already exist?


I got this code, my now I'm confused how I would also incorporate int
this how to have it check to see if the file already exists. If i
does I wanted it to ask if it should overwrite or not.

This code was posted in a reply to my other post, but now I'm confuse
how I would put them both together. Is there an Else If statement
would add to it or something?

Thanks,

~J


Public Sub Tester()
Dim FName As String
Const myPath As String = "G:\Reports\First\"
Dim res As VbMsgBoxResult

FName = Range("A1")

If Dir(myPath & FName) < "" Then
res = MsgBox(Prompt:="The file already exists. " _
& "Do you wish to overwrite it?", _
Buttons:=vbYesNo)
End If

If res = vbYes Then
ChDir myPath
ActiveWorkbook.SaveAs filename:=myPath & FName, _
FileFormat:=xlNormal, _
Password:="", _
WriteResPassword:="", _
ReadOnlyRecommended:=No, _
CreateBackup:=False
Else
'do nothing?
End If
End Su

--
nbaj2
-----------------------------------------------------------------------
nbaj2k's Profile: http://www.excelforum.com/member.php...fo&userid=3648
View this thread: http://www.excelforum.com/showthread.php?threadid=57080

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default Save file in a new folder, but create folder only if folder do

Hello,
Are you replying to Chas or Ron?.

If it is Chas.
You can call the "makeFolder" subroutine but you will have to pass the
appropriate variables.
Or you can insert the line of code after your line where you assign the
filename.
(Incidentally, but I'm sure you figured it out already, in the snippet I
sent you. I was MkDir with your variable names and Saving as C:\Chasdev etc.
Obviously this is wrong.

What you will be doing now is
Attemting to make the new directory.
If there is an error it will be taken care of by the error trap

Once that is done then you can check to see if the file exisits already

But (I'm not sure about this but why not turn DisplayAlerts on and the
system will ask you if you want to overwrite the file).
If you need alerts off just turn them off straight after you have saved the
file.

Chas

"nbaj2k" wrote:


I got this code, my now I'm confused how I would also incorporate into
this how to have it check to see if the file already exists. If it
does I wanted it to ask if it should overwrite or not.

This code was posted in a reply to my other post, but now I'm confused
how I would put them both together. Is there an Else If statement I
would add to it or something?

Thanks,

~J


Public Sub Tester()
Dim FName As String
Const myPath As String = "G:\Reports\First\"
Dim res As VbMsgBoxResult

FName = Range("A1")

If Dir(myPath & FName) < "" Then
res = MsgBox(Prompt:="The file already exists. " _
& "Do you wish to overwrite it?", _
Buttons:=vbYesNo)
End If

If res = vbYes Then
ChDir myPath
ActiveWorkbook.SaveAs filename:=myPath & FName, _
FileFormat:=xlNormal, _
Password:="", _
WriteResPassword:="", _
ReadOnlyRecommended:=No, _
CreateBackup:=False
Else
'do nothing?
End If
End Sub


--
nbaj2k
------------------------------------------------------------------------
nbaj2k's Profile: http://www.excelforum.com/member.php...o&userid=36480
View this thread: http://www.excelforum.com/showthread...hreadid=570801




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Save file in a new folder, but create folder only if folder doesn't already exist?


I had edited my last post, looks like you responded before I edited i
or something, sorry about that.

I was trying to do it the way that I typed it above. Is it possible t
do that way? Is there an error number for file already existing instea
of directory? I could do an error trap that way.

Thanks,

~

--
nbaj2
-----------------------------------------------------------------------
nbaj2k's Profile: http://www.excelforum.com/member.php...fo&userid=3648
View this thread: http://www.excelforum.com/showthread.php?threadid=57080

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Save file in a new folder, but create folder only if folder doesn't already exist?


Thanks, I just figured it out, greatly appreciate all the help :)

~J


--
nbaj2k
------------------------------------------------------------------------
nbaj2k's Profile: http://www.excelforum.com/member.php...o&userid=36480
View this thread: http://www.excelforum.com/showthread...hreadid=570801

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
Need code to save file to new folder, erase from old folder Ron M. Excel Discussion (Misc queries) 1 February 24th 06 06:02 PM
open file from folder save in new folder tim64[_3_] Excel Programming 20 June 17th 05 07:58 PM
"Save As" folder -- can I default this to the same folder as origi Mike Excel Discussion (Misc queries) 1 June 11th 05 12:06 AM
Create Folder If It Doesn't Exist Neutron1871 Excel Programming 4 May 6th 05 01:42 AM
Create Folder and Text File in folder Todd Huttentsine Excel Programming 2 April 29th 04 03:41 PM


All times are GMT +1. The time now is 07:07 AM.

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"