Hello Nanimadhu,
This macro should be a good starting point. It will let you browse for the Inventory folder. Once you have selected the folder, only the PDF and MSG files are copied to a Zip folder. The email subject message shows the count of each of the file types in the attached zipped folder. You will need to change the mail address to the one you want to use as "myboss@work.com" is not a real address. I have heavily commented code to make it easier to understand. Try this out and let me know if this is close to what you wanted.
Sub EmailInventory()
Dim cnt As Long
Dim FileCnt(1) As Long
Dim FileFilter As Variant
Dim FolderPath As Variant
Dim Item As Variant
Dim MailAddress As String
Dim MsgToSend As String
Dim n As Variant
Dim nByte As Variant
Dim oFiles As Object
Dim oFolder As Object
Dim olApp As Object
Dim oShell As Object
Dim ZipFile As Variant
Dim ZipFolder As Variant
Dim ZipHeader() As Byte
' Written: February 03, 2014
' Author: Leith Ross
' Change this email address to whom the email will be sent.
MailAddress = "myboss@work.com"
' Browse for the inventory folder.
Set oShell = CreateObject("Shell.Application")
Set FolderPath = oShell.BrowseForFolder(0, "Browse for Folder", &H50, 17)
' Check if "Cancel" button was clicked.
If FolderPath Is Nothing Then Exit Sub
' Name of the Zip folder to hold files to email.
ZipFolder = Environ("TEMP") & "\Inventory"
' Create the folder if it does not exist.
If Dir(ZipFolder, vbDirectory) = "" Then MkDir ZipFolder
' Remove any previously saved files in the folder.
For Each Item In oShell.Namespace(ZipFolder).Items
Kill Item.Path
Next Item
' Zip file path and name.
ZipFile = Environ("TEMP") & "\Inventory_" & Hex(Timer) & ".zip"
' Create an empty Zip file using the EOCD (End Of Central Record) signature. Minimum Zip file size is 22 bytes.
ReDim ZipHeader(21)
ZipHeader(0) = 80: ZipHeader(1) = 75: ZipHeader(2) = 5: ZipHeader(3) = 6
Open ZipFile For Binary Access Write As #1
Put #1, , ZipHeader
Close #1
' Open the selected folder.
Set oFolder = oShell.Namespace(FolderPath)
Set oFiles = oFolder.Items
' Add these files to the Zip folder.
For Each FileFilter In Array("*.pdf", "*.msg")
oFiles.Filter 64, FileFilter
FileCnt(cnt) = oFiles.Count: cnt = cnt + 1
For n = 0 To oFiles.Count - 1
oShell.Namespace(ZipFolder).CopyHere oFiles.Item(n), 84
Next n
Next FileFilter
' Zip all the files in the folder.
If Application.Sum(FileCnt) > 0 Then
oShell.Namespace(ZipFile).CopyHere ZipFolder
End If
MsgToSend = "Inventory File Counts are as follows:" _
& vbCrLf & "PDF files = " & FileCnt(0) & vbCrLf _
& "MSG files = " & FileCnt(1)
' Create the email and send it.
Set olApp = CreateObject("Outlook.Application")
olApp.Session.GetDefaultFolder (6)
With olApp.CreateItem(0)
.To = MailAddress
.Subject = "Inventory Totals"
.Body = MsgToSend
.Attachments.Add ZipFile, 1
.Send
End With
' Delete the zip folder and files.
Kill ZipFile
End Sub
Bookmarks