PDA

View Full Version : Need help with MkDir form Cell



softman
04-29-2010, 01:16 AM
I would like to have the same MkDir action in the VBA code for may Sub Proc2 as how it works in Sub Proc 1.

It must still use the Range("B15") for the directory and the file name. The only diffrerance is the file name get the date added as the VBA in Proc1.

Currently in Proc 2 the directory have to exist before it can work.
Now I want to change it to make a directory like C:\Dir1\dir name=(RANGE.B15)\filename=(Range.B15).xls

Proc1: (working fine)

Sub Proc1() 'Save filename as value of B15 plus the current date
Dim strFilename, strDirname, strPathname, strDefpath As String
On Error Resume Next ' If directory exist goto next line
strDirname = Range("B15").Value ' New directory name
strFilename = Range("B15").Value 'New file name
strDefpath = "C:\Dir1\" 'Default path name
If IsEmpty(strDirname) Then Exit Sub
If IsEmpty(strFilename) Then Exit Sub
MkDir strDefpath & strDirname
strPathname = strDefpath & strDirname & "\" & strFilename 'create total string
ActiveWorkbook.SaveAs Filename:=strPathname & " " & Format$(Date, "dd-mm-yyyy"), _
FileFormat:=xlNormal, Password:="password123", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
End Sub

Proc 2:
The file is creating correctly but I do not get it to create the directory of the Range("B15") as well.

Sub Proc2()
'Working in 2000-2010
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim OutApp As Object
Dim OutMail As Object
Dim sh As Worksheet
Dim TheActiveWindow As Window
Dim TempWindow As Window
Const PWD As String = "password123"
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sourcewb = ActiveWorkbook
'Copy the sheets to a new workbook
'We add a temporary Window to avoid the Copy problem
'if there is a List or Table in one of the sheets and
'if the sheets are grouped
With Sourcewb
Set TheActiveWindow = ActiveWindow
Set TempWindow = .NewWindow
.Sheets(Array("Audits", "Summary", "Audit Complete", "Chart", "Pixtures")).Copy
End With
'Close temporary Window
TempWindow.Close
Set Destwb = ActiveWorkbook
'Determine the Excel version and file extension/format
With Destwb
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007-2010
'We exit the sub when your answer is NO in the security dialog that you only
'see when you copy a sheet from a xlsm file with macro's disabled.
If Sourcewb.Name = .Name Then
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
MsgBox "Your answer is NO in the security dialog"
Exit Sub
Else
Select Case Sourcewb.FileFormat
Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
Case 52:
If .HasVBProject Then
FileExtStr = ".xlsm": FileFormatNum = 52
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
Case 56: FileExtStr = ".xls": FileFormatNum = 56
Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
End Select
End If
End If
End With
' 'Change all cells in the worksheets to values if you want
' For Each sh In Destwb.Worksheets
' sh.Select
' With sh.UsedRange
' .Cells.Copy
' .Cells.PasteSpecial xlPasteValues
' .Cells(1).Select
' End With
' Application.CutCopyMode = False
' Destwb.Worksheets(1).Select
' Next sh
'Save the new workbook/Mail it
TempFilePath = "C:\Dir1\" & Range("B15").Value
TempFileName = Format(Now, "dd-mm-yyyy")

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With Destwb
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum, Password:=PWD
On Error Resume Next
With OutMail
.To = mail@someone.com
.CC = "mail@someone.com
.BCC = ""
.Subject = "Report Feedback"
.Body = "Please find the report attached"
.Attachments.Add Destwb.FullName
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Display 'or use .Send
End With
On Error GoTo 0
.Close SaveChanges:=False
End With
'Delete the file you have send
'Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub

Many thanks

xld
04-29-2010, 01:47 AM
Break the MkDir out to a separate sub, and call that in Proc2 in the same way



Sub Proc1() 'Save filename as value of B15 plus the current date
Dim strFilename As String, strDirname As String, strPathname As String, strDefpath As String
strDefpath = "C:\Dir1\" 'Default path name
strDirname = Range("B15").Value
If IsEmpty(strDirname) Then Exit Sub
strFilename = Range("B15").Value 'New file name
If IsEmpty(strFilename) Then Exit Sub
Call CreateDirectory(strDefpath, strDirname)
strPathname = strDefpath & strDirname & "\" & strFilename 'create total string
ActiveWorkbook.SaveAs Filename:=strPathname & " " & Format$(Date, "dd-mm-yyyy"), _
FileFormat:=xlNormal, Password:="password123", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
End Sub

Private Sub CreateDirectory(path As String, cellValue As String)
On Error Resume Next
MkDir path & cellValue
End Sub


BTW, best to use date format as yyyy-mm-dd rather than dd-mm-yyyy, for sort order.

softman
04-29-2010, 02:04 AM
Hi James,

Thanks James, I am not a pro to VBA and can only understand some of it but is this solution for Proc 1 or Proc 2 because the way it needs to work in the complete Workbook I have to have Proc 1 and Proc 2 othewise Proc 2 is not working (emailing correctly).

So, the part of Proc 1 have the MkDir also in it.

Sorry if I sound deee.

Kind regards

xld
04-29-2010, 02:19 AM
My name is not James, James Thurber is an American humourist that I was quoting :)

I did look at Proc2, but I baled out as I quickly got lost in it and could not see where a directory would need to be created. Can you tell me where that would be?

softman
04-29-2010, 02:41 AM
Sorry (James :)) xld,

For me it look like it should be just before
'Save the new workbook/Mail it
TempFilePath = "C:\Dir1\" & Range("B15").Value
TempFileName = Format(Now, "dd-mm-yyyy")


Kind regards

xld
04-29-2010, 03:27 AM
Okay, try this. Change the code to



'Save the new workbook/Mail it
TempFilePath = "C:\Dir1\"
TempDirName = Range("B15").Value
Call CreateDirectory(TempFilePath, TempDirName)
TempFilePath =TempFilePath & TempDirName
TempFileName = Format(Now, "dd-mm-yyyy")

softman
04-29-2010, 03:53 AM
Hi XLD,

Getting a VBA error at :
Call CreateDirectory(TempFilePath, TempDirName)

Kind regards

xld
04-29-2010, 04:29 AM
Did you create that little extra procedure I gave you in my previous post?

softman
04-29-2010, 04:35 AM
Hi XLd,

Was that not for Proc 1, beacuse I have changed your last settings on Proc 2?

Kind regards

xld
04-29-2010, 05:05 AM
It was for both.

softman
04-29-2010, 05:30 AM
Hi xld,

No luck, I think the froblem is with Proc 1 and not much Proc 2 but will let you know

Thanks