|
|
We had a meeting some time ago in the company where I work, we were speaking in ways for a better use of the bandwidth of our connection. One of the points we talk about, was that every file sent as an attachment, should be a ZIP file. We all agree about this, but during the next week, we find out that not all of the employes were going to follow the rule (some because "they don't know how" even when you have teached them, some because they don't want to, etc). So I tought about a program that will take all the attachments from a message and create a ZIP file from them.
A simple search in google, bring me a lot of choices for this, but at the same time, I was reading the newest entry on Craig Boyds blog (wich by the way it's always great), and came to my hands the latest version of his compression library, so yesterday I tried to create a COM server in VFP to create a ZIP file from all the attachments in an email message using Microsoft Outlook.
This was a quick work, so I'm sure that anyone can make it better, I have only tried this with Outlook 2003, so please let me know any issues you may have.
These are the steps, needed to make it work:
1. First every office plug in based in COM, it's based in the following class IDTExtensibility2, what is this? well thanks to our Object browser, was really easy to find out it's the Microsoft Add In Designer, like this image shows

So if you ever need to write one, this is the way to go. Drag and drop the interface part into a an empty PRG, and it will create the basic structure to the class we are going to build. After this part, we need to create another class that we are going to bind to the Microsoft Outlook application model, for this part I read all the information in the VFP Help File.
This is the code, of the two libraries: Define Class Connect As session OLEPUBLIC Implements IDTExtensibility2 In {AC0714F2-3D04-11D1-AE7D-00A0C90F26F4}#1.0 oOutlook=Null oProxy=Null Procedure IDTExtensibility2_OnConnection(_Application As VARIANT, ConnectMode As VARIANT, AddInInst As VARIANT, custom As VARIANT) As VOID Local lcStr As String lcStr="Iniciando Servidor en: "+_Vfp.ServerName StrToFile(lcStr,"C:\LogNavas.Log") This.oOutlook=_Application This.oProxy=NewObject("OutlookEvents") If EventHandler(This.oOutlook,This.oProxy) Then lcStr=Chr(13)+Chr(10)+"BindEvent Hecho" StrToFile(lcStr,"C:\LogNavas.Log",1) Else lcStr=Chr(13)+Chr(10)+"No se pudo realizar el BindEvent" StrToFile(lcStr,"C:\LogNavas.Log",1) EndIf EndProc Procedure IDTExtensibility2_OnDisconnection(RemoveMode As VARIANT, custom As VARIANT) As VOID EndProc Procedure IDTExtensibility2_OnAddInsUpdate(custom As VARIANT) As VOID EndProc Procedure IDTExtensibility2_OnStartupComplete(custom As VARIANT) As VOID EndProc Procedure IDTExtensibility2_OnBeginShutdown(custom As VARIANT) As VOID This.oOutlook=Null This.oProxy=Null EndProc Function Error(nError As Integer, cMethod As String, nLine As Integer) ComReturnError(cMethod+' Error#='+Str(nError,5)+' línea='+Str(nline,6)+' '+Message(),_VFP.ServerName) EndFunc Procedure Destroy EndProc EndDefine
Define Class OutlookEvents As Session OLEPUBLIC Implements ApplicationEvents In Outlook.Application oFiles=Null cTempPath="" Procedure ApplicationEvents_ItemSend(ITEM AS VARIANT, CANCEL AS LOGICAL) AS VOID Local loItem As Object, lcFileName As String, lcPath As String, lnI As Integer, lnTotal As Integer, lcStr As String lcPath=JustPath(_Vfp.ServerName)+"\" Set Library To lcPath+"vfpcompression.fll" This.cTempPath=Addbs(Sys(2023)) lnTotal=m.Item.Attachments.Count If lnTotal>0 Then This.oFiles=CreateObject("Collection") If File(This.cTempPath+"Adjuntos.Zip") Then Delete File (This.cTempPath+"Adjuntos.Zip") lcStr=Chr(13)+Chr(10)+"Se elimino el archivo Adjuntos.ZIP" StrToFile(lcStr,"C:\LogNavas.Log",1) EndIf ZipOpen("Adjuntos.Zip",This.cTempPath,.F.) lcStr=Chr(13)+Chr(10)+"Se Creo el Archivo Adjuntos.ZIP" StrToFile(lcStr,"C:\LogNavas.Log",1) For lnI=lnTotal To 1 Step -1 lcFileName=This.cTempPath+m.Item.Attachments(lnI).FileName m.Item.Attachments.Item(lnI).SaveAsFile(lcFileName) ZipFile(lcFileName,.T.) lcStr=Chr(13)+Chr(10)+"Se Agregó el Archivo "+lcFileName StrToFile(lcStr,"C:\LogNavas.Log",1) m.Item.Attachments.Item(lnI).Delete This.oFiles.Add(lcFileName) EndFor ZipClose() m.Item.Attachments.Add(This.cTempPath+"Adjuntos.Zip") lcStr=Chr(13)+Chr(10)+"Se Adjunto el Archivo Adjuntos.ZIP" StrToFile(lcStr,"C:\LogNavas.Log",1) For lnI=1 To lnTotal Delete File (This.oFiles.Item(lnI)) lcStr=Chr(13)+Chr(10)+"Se Eliminó el Archivo "+This.oFiles.Item(lnI) StrToFile(lcStr,"C:\LogNavas.Log",1) EndFor EndIf This.oFiles=Null EndProc Procedure ApplicationEvents_NewMail() AS VOID EndProc Procedure ApplicationEvents_Reminder(ITEM AS VARIANT) AS VOID EndProc Procedure ApplicationEvents_OptionsPagesAdd(PAGES AS VARIANT) AS VOID EndProc Procedure ApplicationEvents_Startup() AS VOID EndProc Procedure ApplicationEvents_Quit() AS VOID If File(This.cTempPath+"Adjuntos.Zip") Then Delete File (This.cTempPath+"Adjuntos.Zip") EndIf Local lcStr As String lcStr=Chr(13)+Chr(10)+"Se Finalizó la Sesión" StrToFile(lcStr,"C:\LogNavas.Log",1) EndProc Function Error(nError As Integer, cMethod As String, nLine As Integer) ComReturnError(cMethod+' Error#='+Str(nError,5)+' línea='+Str(nline,6)+' '+Message(),_VFP.ServerName) EndFunc Procedure Destroy EndProcEndDefine
An important part of the code is the EventHandler(This.oOutlook,This.oProxy) function call, think of it like BindEvent() but for COM objects.
The last step, is to create a key in the registry:
[HKEY_CURRENT_USER\Software\Microsoft\Office\Outlook\AddIns\outlookzip.Connect] "LoadBehavior"=dword:00000003 "Description"="To create ZIP files in all attachments" "FriendlyName"="Outlook ZIP"
This is an image of the registry key:
 To check if everything is working as expected, in Outllook, go to Tools Menu, Options, Others tab, Advanced options, Com plug in and be sure that Outlook Zip is checked.
Take a look at this image, if you have any doubt:
You will find all the code in the file attached to this post.
More information, and samples in another languages, in:
http://msdn2.microsoft.com/en-us/library/Aa140126(office.10).aspx http://www.outlookcode.com/article.aspx?ID=36
Comment Notification
If you would like to receive an email when updates are made to this post, please register here
Subscribe to this post's comments using
|
|