PDA

View Full Version : Solved: Email file with password HELP please



softman
04-14-2010, 01:40 AM
I have the following sampe workbook (see attached) that select some worksheets and email the new workbook to a given email. What I would like to be added to this is that it should save a copy to a given directory with password protection and email that save file with the password protection.

I do not now how to incorporate the following code:

ActiveWorkbook.SaveAs Filename:= _
"C:\Documents and Settings\Jahar\My Documents\test.xls", FileFormat:=xlNormal _
, Password:="test", WriteResPassword:="test", ReadOnlyRecommended:=False _
, CreateBackup:=False



Please help

Many thanks

softman
04-14-2010, 02:29 AM
Sorry new file attached work only once then vb error:

GTO
04-14-2010, 03:09 AM
I have the following sampe workbook (see attached) that select some worksheets and email the new workbook to a given email. What I would like to be added to this is that it should save a copy to a given directory with password protection and email that save file with the password protection...


I have managed to save attached file with a password,
Now only need to get it in a given directory....?

Hi softman,

I caught your modified post #2 with the attachment included. I think we can improve upon that, but could we clarify what exactly is we want?

I see that you want to change the way the code example works, in that rather than creating (and sending) a temp wb, then deleting said wb - we would rather save the wb permanently (with a password to open), and send the passworded wb.

Now, you mention that you got it to work once, but it fails upon the second try. (This is because you have the fullname hard-coded)

To get around this, how do you want to handle naming the file? Do you want to offer the user the ability to name the file, or, do you want the macro to still name the new file, but:


overwrite a previously saved file with the same date
add to the filename, such as the current time (seconds) so that it doesn't find an existing file w/the same nameHope that makes sense,

Mark

softman
04-14-2010, 04:23 AM
Hi Mark,

Thanks for your replay.

You are on the spot, the steps this must do is as follow:

1. The file name must still be safe as currently set (Sheet1:B15) + date + time
2. No temt dir but to a permanent folder (c:\test)

hope this helps.

Regards
Christo

GTO
04-14-2010, 04:47 AM
Hi Christo,

See if this makes sense. My added coments are like:
'// comments //


Sub EmailSheets()
'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 = "MyPassword"

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("Sheet1", "Sheet2")).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
'// We'll skip saving to the Temp folder. I just used ThisWorkbook's path, change //
'// change to suit //
' TempFilePath = Environ$("temp") & "\"
TempFilePath = ThisWorkbook.Path & "\"

'// To reasonably make a new filename ea time, we could add minutes and seconds//
TempFileName = Range("B15").Value & Sourcewb.Name & " " & Format(Now, "dd-mm-yyyy (NnSs)")

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With Destwb
'// Add the Password argument here. See Const at top. //
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum, Password:=PWD

On Error Resume Next
With OutMail
.To = "yourmail@here.com"
.CC = "yourmail2@here.com"
.BCC = ""
.Subject = "This is the Subject line"
.Body = "Hi there"
.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

'// Ditch killing the file here.//
'Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub

softman
04-14-2010, 05:06 AM
Hi Mark,

This is working 100% but I just have 2 questions please?

Q1: I will I change the path:
'// We'll skip saving to the Temp folder. I just used ThisWorkbook's path, change //
'// change to suit //
' TempFilePath = Environ$("temp") & "\"
TempFilePath = ThisWorkbook.Path & "\"


TempFilePath = C:\Temp & "\" ????

Q2: It currently save the document to B15 and the old filename like:
Filename_heresample_macro.xls 14-04-2010 (5811).xls

How do I get rid of the sample_macro.xls name in the file?

Many thanks

GTO
04-14-2010, 06:52 AM
Hi Mark,

This is working 100% but I just have 2 questions please?

Q1: I will I change the path:
'// We'll skip saving to the Temp folder. I just used ThisWorkbook's path, change //
'// change to suit //
' TempFilePath = Environ$("temp") & "\"
TempFilePath = ThisWorkbook.Path & "\"


TempFilePath = C:\Temp & "\" ????

I'm not exactly sure if I'm understanding, but whatever folder you want the wb saved to, it should look like:


TempFilePath = "C:\Documents and Settings\Jahar\My Documents\"



Q2: It currently save the document to B15 and the old filename like:
Filename_heresample_macro.xls 14-04-2010 (5811).xls

How do I get rid of the sample_macro.xls name in the file?

Many thanks

TempFileName = Range("B15").Value & Sourcewb.Name & " " & Format(Now, "dd-mm-yyyy (NnSs)")

Try deleting the part in red.

Hope that helps,

Mark

softman
04-14-2010, 07:07 AM
Hi Mark,

Thanks a Million, works wonderfull!

Regards

PS. How do I ad the Solved?

GTO
04-14-2010, 09:48 AM
Thanks a Million, works wonderfull!

You are most welcome and I am glad we were able to help:thumb

How do I ad the Solved?

Atop your first post, look under Thread Tools. The Solved option is displayed only to the originator of the thread. Thank you for asking as well, as this saved others time checking to see if particular threads are solved.

A good day to you,

Mark