vba to secure zip attachments

Post number 3 has been selected as the best answer.

shrydvd

New Member
Outlook version
Outlook 2019 64-bit
Email Account
IMAP
I am hoping someone can pull me out of this . . . I am trying to identify emails that have attachments then zip those attachments together with a password. I end up with different errors but can't seem to get it. One of the main errors is with PKZip in the command line saying "Nothing to do". I am sure it has something to do with how I am trying to tell it which files to zip.

Anyway, here is what I have. It is a mixed bag of pieces I have gotten elsewhere and trying to put them together.

If I am way off base and need to scrap it & start over, please tell me that as well. Also, there is likely extraneous junk in the code below simply because I keep trying different things.

Thank you for any guidance you can provide.
Code:
Sub ZipdAttach()

    Dim objMail As Outlook.MailItem
    Dim objAttachments As Outlook.Attachments
    Dim objAttachment As Outlook.Attachment
    Dim objFileSystem As Object
    Dim objShell As Object
    Dim varTempFolder As Variant
    Dim varZipFile As Variant
    Dim myDir As String
    Dim dest As Variant
    Dim srce As Variant
    Dim strCommand As String
    Dim val As String
    'Save the attachments to Temporary folder
    Set objFileSystem = CreateObject("Scripting.FileSystemObject")
    varTempFolder = objFileSystem.GetSpecialFolder(2).Path & "\Temp " & Format(Now, "dd-mm-yyyy- hh-mm-ss-")
    MkDir (varTempFolder)
    varTempFolder = varTempFolder & "\"
 
    Set objMail = Outlook.Application.ActiveInspector.CurrentItem
    Set objAttachments = objMail.Attachments
    For Each objAttachment In objAttachments
        objAttachment.SaveAsFile (varTempFolder & objAttachment.FileName)
    Next
 
   'Create a new zip file
    varZipFile = InputBox("Specify a name for the new zip file", "Name Zip File", objMail.Subject)
    varZipFile = varTempFolder & varZipFile
 
    'Copy all the saved attachments to the new zip file
     Set objShell = CreateObject("Shell.Application")
    
'_______________________________________________


val = InputBox("Give me an 8 character password.")
myDir = "C:\Program Files\PKWARE\PKZIPC"
dest = varZipFile
srce = varTempFolder
strCommand = "pkzipc.exe -add -passphrase=" & val & " " & dest & " " & srce
Call Shell("cmd.exe /k cd /d " & myDir & " & " & strCommand, 1)

End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
>> Also, there is likely extraneous junk in the code below simply because I keep trying different things.
LOL. A lot of my code has unnecessary junk in it for the same reason.

Try this change:
srce = varTempFolder & *.*

From the way i read the documentation, it should add the folder contents when you use just the folder, but designating all files in the folder works here.
 
Last edited:

shrydvd

New Member
Outlook version
Outlook 2019 64-bit
Email Account
IMAP
I'm sorry I didn't get back with you to say thank you. When I tried your answer, it worked great & I got carried away with the rest of the project. Anyway, while I am sure my code is "less than elegant" to say the least, this works for me if it will help someone else.
Thank you again to Diane for her guidance that made it all finally work!


Code:
Sub ZipAttach()
    Dim objMail As Outlook.MailItem
    Dim objAttachments As Outlook.Attachments
    Dim objAttachment As Outlook.attachment
    Dim objFileSystem As Object
    Dim varTempFolder As String
    Dim varzipfile As String
    Dim val As String
    Dim n As String
    Dim srce As String
    Dim dest As String
'    Dim sender As Outlook.AddressEntry
 
    n = (Format(Now, "dd-mm-yyyy- hh-mm-ss-")) 'the folder name where attachments are saved & converted
EnterVal:
    val = InputBox("Create password of at least 8 characters.")
            If StrPtr(val) = 0 Then 'Cancel was pressed
            ' Handle what to do if cancel pressed
            Exit Sub
            ElseIf Len(strM) < 8 Then 'Not enough characters entered
            MsgBox "You must enter at least 8 characters."
            GoTo EnterVal
            End If

       'Save the attachments to Temporary folder
    Set objFileSystem = CreateObject("Scripting.FileSystemObject")
    varTempFolder = objFileSystem.GetSpecialFolder(2).Path & "\Temp " & n
    MkDir (varTempFolder)
    varTempFolder = varTempFolder & "\"
 
    Set objMail = Outlook.Application.ActiveInspector.CurrentItem
    Set objAttachments = objMail.Attachments
    For Each objAttachment In objAttachments
        objAttachment.SaveAsFile (varTempFolder & objAttachment.FileName)
    Next
    dest = "protectedfiles.zip"
    srce = """" & varTempFolder & "*.*" & """"
    Cmd2 = "CD " & varTempFolder
    cmd3 = "pkzipc.exe -add -passphrase=" & val & " " & "protectedfiles.zip" & " " & srce
    
    Connector = " & "
    Commands = "cmd.exe /c " & Cmd2 & Connector & cmd3
    Debug.Print Commands
    pid = Shell(Commands, vbNormalFocus)
    
Set objAttachments = objMail.Attachments
     While objAttachments.Count > 0
           objAttachments.Item(1).Delete
     Wend

varzipfile = varTempFolder & "protectedfiles.zip"
Pause (2)
objMail.Attachments.Add varzipfile
'DelFolder (varTempFolder)
'Get the address of the person you are sending to
        Dim olObj As Object
        Set olObj = Application.ActiveInspector.CurrentItem
        olObj.Recipients.Item(1).Resolve
        TheMail = olObj.Recipients.Item(1).Address
        Set olObj = Nothing
'Now, email yourself the password
    Dim s As String
    Dim obApp As Object
    Dim NewMail As MailItem
    Set obApp = Outlook.Application
    Set NewMail = obApp.CreateItem(olMailItem)

User = CreateObject("Outlook.Application").GetNamespace("MAPI").CurrentUser.Address
    'You can change the concrete info as per your needs
    With NewMail
         .Subject = TheMail & " Password Sent " & n
         .To = User
         .Body = "Password used was " & """" & val & """"
         '.Attachments.Add ("C:\Attachments\Test File.docx")
         '.Importance = olImportanceHigh
         .Send
    End With
 
    Set obApp = Nothing
    Set NewMail = Nothing
End Sub
Public Sub Pause(Seconds As Single)
    Dim TimeEnd As Single
    TimeEnd = Timer + Seconds
    While Timer < TimeEnd
        DoEvents
    Wend
End Sub
I will eventually clean it up as much as my novice self can do. I will then replace this with the cleaned up version.
 

Lewis-H

Senior Member
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
I suggest you change the program a little and test command in the command prompt. Once you have that working try it in VBA. I added quotes around program exe since it has space.
Sub Zip_Test1()

Dim source As String, Mstr As String, target As String, password As String
Dim file As Variant
Dim cmd As String

Application.DisplayAlerts = False
Mstr = "C:\Users\Desktop\Test"

For Each file In CreateObject("Scripting.FileSystemObject").GetFolder(Mstr).Files
source = Chr$(34) & "C:\Users\Desktop\Test\" & file.Name & Chr$(34)

target = "C:\Users\Desktop\Zipped\"
password = Chr$(34) & "Abcd1234" & Chr$(34)
cmd = """" & "C:\Program Files\PKWARE\PKZIPW\pkzipw.exe" & """" & " -add -pass=" & password & " " & target & " " & source
Debug.Print cmd '' Copy command in Immediate window and paste in Command Prompt.

Shell (cmd)

Exit Sub ''Exit for testing
Next
Application.DisplayAlerts = True
End Sub

Hope this helps!
Regards,
Lewis
 
Thread starter Similar threads Forum Replies Date
M Outlook 2013 reminder email by using Outlook vba Outlook VBA and Custom Forms 0
D.Moore VBA script fail after Office 365 update Using Outlook 8
R Limiting length of saved attachment in VBA Outlook VBA and Custom Forms 2
S Skype for business meeting vba code Outlook VBA and Custom Forms 1
C How to use VBA to show only items x days old or more Outlook VBA and Custom Forms 1
B VBA to convert email to task, insert text of email in task notes, and attach copy of original email Outlook VBA and Custom Forms 4
D Outlook VBA error extracting property data from GetRules collection Outlook VBA and Custom Forms 10
S Reference Custom Fields with VBA Outlook VBA and Custom Forms 2
PGSystemTester VBA To Change AppointmentItem.BusyStatus From MeetingItem Before Send Using Outlook 0
A VBA macro for 15 second loop in send and received just for 1 specific mailbox Outlook VBA and Custom Forms 1
O Email not leaving Outbox when using Excel VBA to sync Outlook account Outlook VBA and Custom Forms 4
G VBA Macro Calendar Printing Assistant 3
R Help Revising VBA macro to delete email over different time span Outlook VBA and Custom Forms 0
B VBA to Collapse Task Folder Groups Outlook VBA and Custom Forms 1
R Expand VBA Permanent Delete Code Outlook VBA and Custom Forms 6
M Adding Subject to this Link-Saving VBA Outlook VBA and Custom Forms 5
N VBA to delete duplicates by message-id on common pst for 2 or more emails Outlook VBA and Custom Forms 0
S Change VBA script to send HTML email instead of text Outlook VBA and Custom Forms 3
M VBA to auto forward message with new subject and body text Outlook VBA and Custom Forms 8
A Custom VBA to sort emails into folders Outlook VBA and Custom Forms 0
L Moving emails with similar subject and find the timings between the emails using outlook VBA macro Outlook VBA and Custom Forms 1
B Outlook Business Contact Manager with SQL to Excel, User Defined Fields in BCM don't sync in SQL. Can I use VBA code to copy 1 field to another? BCM (Business Contact Manager) 0
R VBA for copying sent email to current folder under a shared mailbox Outlook VBA and Custom Forms 17
A Edit subject - and change conversationTopic - using VBA and redemption Outlook VBA and Custom Forms 2
N How can I increase/faster outlook VBA Macro Speed ? Using Outlook 2
N Outlook Email Rule execution through shortcut keys (VBA codes) Using Outlook 1
A VBA Code in Outlook disappears after first use Outlook VBA and Custom Forms 1
B Clear Offline Items (Mail Folder) via VBA Outlook VBA and Custom Forms 1
dweller Outlook 2010 Rule Ignores VBA Script Outlook VBA and Custom Forms 2
D.Moore Folder view settings by VBA macro Outlook VBA and Custom Forms 57
F VBA to ensure a code is entered in Subject title Outlook VBA and Custom Forms 1
B Vba to monitor time to respond to emails using a shared mailbox Outlook VBA and Custom Forms 5
N VBA Script to Open highlighted e-mail and Edit Message Outlook VBA and Custom Forms 5
G Outlook VBA and Google Calendar ("Events") Outlook VBA and Custom Forms 1
B Looking to get the Recipient email address (or even the "friendly name") from an email I am replying to using VBA Outlook VBA and Custom Forms 4
J VBA Outlook : Subject line : Cut and Paste name to heading , number to very end of the body of Email Outlook VBA and Custom Forms 1
M VBA to send reminder email if no response Using Outlook 13
D Using a VBA Custom Form to Send Reoccurring Email Upon Task Completion Outlook VBA and Custom Forms 4
U Outbox Message Stuck after reading some MailItem Properties with VBA Outlook VBA and Custom Forms 1
B Advanced Search in MS Outlook by VBA and SQL Outlook VBA and Custom Forms 2
N Open & Save VBAProject.Otm using VBA Code Outlook VBA and Custom Forms 1
M Help sending email but removing signature via VBA Outlook VBA and Custom Forms 5
K VBA BeforeItemMove event create rule to always move to its folder. Outlook VBA and Custom Forms 4
D Remove text in subject using VBA Outlook VBA and Custom Forms 4
R VBA Code to permanently delete selected email Outlook VBA and Custom Forms 10
R VBA | Chosing path to save file Outlook VBA and Custom Forms 1
K Outlook Archive to PST Files by Date Range VBA Script? Outlook VBA and Custom Forms 1
P How to export voting results using VBA? Outlook VBA and Custom Forms 2
J Help Please!!! Outlook 2016 - VBA Macro for replying with attachment in meeting invite Outlook VBA and Custom Forms 9
Peter H Williams Enable script containing VBA Outlook VBA and Custom Forms 12
Similar threads


















































Top