Computing.Net > Forums > Office Software > macro to save as

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

macro to save as

Reply to Message Icon

Name: nu2it
Date: November 3, 2009 at 03:22:23 Pacific
OS: Windows XP
Product: Microsoft Excel 2007 home and student
Subcategory: Microsoft Office
Tags: save as, macro
Comment:

I would like to have a macro to run a save as
command using the contents of an excel cell as
the name. I have generated this cell by combining
3 cells. Icing on the cake if it could generate an
e-mail message.



Sponsored Link
Ads by Google

Response Number 1
Name: Humar
Date: November 3, 2009 at 05:13:37 Pacific
Reply:

Hi,

Here is macro code for saving the workbook using a generated name in Cell A4 on sheet1 of the active workbook:

Option Explicit

Private Sub TestSave()
Dim strName As String
strName = ActiveWorkbook.Worksheets("Sheet1").Range("A4").Text
ActiveWorkbook.SaveCopyAs Filename:=strName
End Sub

I included the intermediate step of creating the filename as a string so that you can single step through the macro and see the filename that will be used, or use it in an e-mail, but you can just use:

Filename:=ActiveWorkbook.Worksheets("Sheet1").Range("A4").Text

Regards


0

Response Number 2
Name: Humar
Date: November 3, 2009 at 05:56:57 Pacific
Reply:

Hi,

Here it is with email.

There are several e-mail settings that you will have to enter yourself, including the name of the SMTP server. You will likely have these settings already in your email program.

There is a *****RISK*****
This code includes your e-mail address and password.

I suggest that:
a. This code is in your Personal.xls file and NOT the workbook that you are saving, so that this information doesn't get sent on to others with the workbook
b. You use an Input box to request your password, rather than hardcode it in the Macro.
c. You might setup an e-mail just for this use, with a username and password separate from your own account.

Option Explicit

Private Sub TestSave()

Dim strName As String
Dim objEMail As Object
On Error GoTo ErrHnd

'get filename
strName = ActiveWorkbook.Worksheets("Sheet1").Range("A4").Text
'save a copy of the active workbook with the new filename
ActiveWorkbook.SaveCopyAs Filename:=ActiveWorkbook.Worksheets("Sheet1").Range("A4").Text

'create an e-mail object
Set objEMail = CreateObject("CDO.Message")
'create the e-mail fields
objEMail.From = "my name"
objEMail.To = "the e-mail address complete with @"
objEMail.Subject = "We've saved an Excel File"
objEMail.Textbody = strName
'
'setup the SMTP server
objEMail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name of Remote SMTP Server
objEMail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp. your email provider"
'Server port = 25 (typical - but check yours)
objEMail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEMail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your email name including @"
objEMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your password"
'update the fields
objEMail.Configuration.Fields.Update
'send the message
objEMail.Send
Set objEMail = Nothing
Exit Sub
'error handler
ErrHnd:
Set objEMail = Nothing
Err.Clear
End Sub

Note: Underscore _ at the ends of some lines is the line continuation character and these are all one long line.

I am not an expert on e-mail, all I can say is that this works for me.

Others may have better or more secure methods.

Regards


0
Reply to Message Icon

Related Posts

See More


reinstall Office Excel adjust row heights


Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: macro to save as

Excel Macro to Save As www.computing.net/answers/office/excel-macro-to-save-as/7469.html

Macro to save w/ other name www.computing.net/answers/office/macro-to-save-w-other-name/7251.html

Excel Macro to Save Consecutive Files www.computing.net/answers/office/excel-macro-to-save-consecutive-files/9490.html