Consulting

Results 1 to 9 of 9

Thread: Solved: Email file with password HELP please

  1. #1
    VBAX Regular
    Joined
    Mar 2010
    Posts
    49
    Location

    Solved: Email file with password HELP please

    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:

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


    Please help

    Many thanks

  2. #2
    VBAX Regular
    Joined
    Mar 2010
    Posts
    49
    Location

    Some Modification 80% there

    Sorry new file attached work only once then vb error:
    Last edited by softman; 04-14-2010 at 02:51 AM.

  3. #3
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by softman
    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...
    Quote Originally Posted by softman
    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:

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

    Mark

  4. #4
    VBAX Regular
    Joined
    Mar 2010
    Posts
    49
    Location

    On the Spot

    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

  5. #5
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    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

  6. #6
    VBAX Regular
    Joined
    Mar 2010
    Posts
    49
    Location

    Nearly there

    Hi Mark,

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

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

    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

  7. #7
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by softman
    Hi Mark,

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

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

    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\"
    Quote Originally Posted by softman
    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

  8. #8
    VBAX Regular
    Joined
    Mar 2010
    Posts
    49
    Location

    Thumbs up Solved

    Hi Mark,

    Thanks a Million, works wonderfull!

    Regards

    PS. How do I ad the Solved?

  9. #9
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Thanks a Million, works wonderfull!

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

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •