Help VBA Saving and Emailing file

adammark

New Member
Joined
Mar 11, 2009
Messages
4
I am extremely new with VBA, and would be highly appreciative of help.
I have the script below working.

I would like to modify it a bit

ChDir "C:\file1\files\CURRENT YEAR\CURRENT MONTH\"
directory = "C:\file1\files\CURRENT YEAR\CURRENT MONTH\""

I want to have the CURRENT YEAR and MONTH populate automatically based on thir current days


I want to have the email sent to recipients based on a list on Sheet1 cells A1:A10

And I need to add body text to the email HTML prefeable, or txt maybe read from a txt file on my hard drive.


Any and all help is great.

Thanks!!!



Sub sendfile()
Sheets(ActiveSheet.Name).Copy

ChDir "C:\file1\files\CURRENT YEAR\CURRENT MONTH\"
directory = "C:\file1\files\CURRENT YEAR\CURRENT MONTH\""


filenamesave = "My File " & FormatDateTime(Date, vbLongDate)
ActiveWorkbook.SaveAs Filename:=directory & filenamesave & ".xls", FileFormat _
:=xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:= _
False, CreateBackup:=False

If MsgBox("Are you sure this file is ready to send?", vbOKCancel, _
"Are you sure...") = vbCancel Then Exit Sub


ActiveWorkbook.SendMail Recipients:="abc@abcd.com", Subject:=filenamesave
MsgBox "File Sent.", vbOKOnly

End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Thanks, it kinda works, it looks for the right path, I should have specified that I would like to the directories to be created if they havent already been.
 
Upvote 0
use dir with vbdirectory to find if it exists, if not use mkdir to make new directory, but you have to make each folder in turn, mkdir will not make the entire tree in one go
so if the directory does not exist you need to check if the parent directory exists etc
Code:
directoryexists = true
mydir = "C:\file1\files\" & year(now) & "\" & month(now) 
if len(dir(mydir, vbdirectory)) > 0 then    'directory or file found to match
      If not GetAttr(mydir) = vbDirectory Then directoryexists = false  'is file,not directory
   else directoryexists = false
end if
if directoryexists = false then mkdir mydir  ' assumes parent directory already exists
possibly best to make into a function, then can call it whenever you want
 
Last edited:
Upvote 0
Thank You so much. i will try tonight to get it to work. I'm not sure if I know how to make the command into a directory

everythings before my year and month will always exist. I will need to create month and year on the fly if need be

is there a way to have the month dir create its name in full aka March instead of 3?
 
Upvote 0
mydir = "C:\file1\files\" & year(now) & "\" & format(month(now), "mmmm")

if the year directory does not exist you will need to create that before creating the month directory
</pre>
 
Upvote 0
Regarding the format in which the file is saved i.e.
Code:
ActiveWorkbook.SaveAs Filename:= _
Path & SaveName _
, FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
Are there alternatives to xlNormal?
I would like to save the file as values only so that readers cannot see the formulas, but I would also like to preserve the formats (i.e. layout, page breaks, etc.)

Any advice on this? Thanks a lot!
 
Upvote 0
Although it's not the neatest code, I tried this and it seems to work:

Code:
ActiveWorkbook.SaveAs Filename:= _
Path & SaveName _
, FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
ActiveWorkbook.Sheets("Report").Range("A:M").Copy
ActiveWorkbook.Sheets("Report").Range("A:M").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveWorkbook.Save
 
Upvote 0

Forum statistics

Threads
1,224,896
Messages
6,181,622
Members
453,058
Latest member
rmd0725

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top