Sending Email attaching a Zip file

Terry P

Active Member
Joined
Nov 28, 2002
Messages
256
I am able to zip the active Workbook but I need to email the zipped file rather than the Workbook.
Any ideas please?
This message was edited by Terry P on 2002-12-07 10:22
 
Jack
It appears the usual vba will only send the current workbook or sheet as is. I don,t know much about API's but TsTom is coming up with something that I may be able to use.

Many thanks and warn you that I might come back again.
Terry
Using Excel XP
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hi Terry,

While TsTom is taking a look on Outlook / Outlook Express via MAPI there exist other alternatives as well.

I recently finished a work where the client preferred to use a total different approach.

<PRE>
<FONT color=blue>Sub </FONT>Send_Mail_ComMail()

<FONT color=blue>Dim </FONT>wbBok<FONT color=blue> As</FONT><FONT color=blue> Workbook</FONT>

<FONT color=blue>Dim </FONT>wsBlad<FONT color=blue> As</FONT> Worksheet

<FONT color=blue>Dim </FONT>rnOmrade<FONT color=blue> As</FONT> Range

<FONT color=blue>Dim </FONT>RetVal<FONT color=blue> As</FONT><FONT color=blue> Double</FONT>

<FONT color=blue>Dim </FONT>stTill<FONT color=blue> As</FONT><FONT color=blue> String</FONT>, stFran<FONT color=blue> As</FONT><FONT color=blue> String</FONT>, stComMailexe<FONT color=blue> As</FONT><FONT color=blue> String</FONT>

<FONT color=blue>Dim </FONT>stBodyText<FONT color=blue> As</FONT><FONT color=blue> String</FONT>, stHost<FONT color=blue> As</FONT><FONT color=blue> String</FONT>, stBilagaNamn<FONT color=blue> As</FONT><FONT color=blue> String</FONT>

<FONT color=blue>Dim </FONT>stMailVard<FONT color=blue> As</FONT><FONT color=blue> String</FONT>, stArende<FONT color=blue> As</FONT><FONT color=blue> String</FONT>



<FONT color=blue>Set </FONT>wbBok = ThisWorkbook

<FONT color=blue>Set </FONT>wsBlad = wbBok.ActiveSheet



<FONT color=#ff0000>'Mailsoftware
</FONT>
stComMailexe = "c:commail.exe"



<FONT color=#ff0000>'Mailhost
</FONT>
stMailVard = Range("F3").Value



<FONT color=#ff0000>'Senders e-mail
</FONT>
stFran = Range("F4").Value



<FONT color=#ff0000>'Recipient e-mail
</FONT>
stTill = Range("F5").Value



<FONT color=#ff0000>'Subject
</FONT>
stArende = Range("F6").Value



<FONT color=#ff0000>'Create attachment
</FONT>
stBilagaNamn = "c:Bilaga" &<FONT color=blue> Date</FONT> & ".xls"



<FONT color=blue>With </FONT>Application

.ScreenUpdating =<FONT color=blue> False</FONT>

.DisplayAlerts =<FONT color=blue> False</FONT>

<FONT color=blue>End With</FONT>



wsBlad.Copy

ActiveWorkbook.SaveAs stBilagaNamn

ActiveWindow.Close



<FONT color=#ff0000>'Create bodytext
</FONT>
Range("C11:H25").Copy



Workbooks.Add -4167

Selection.PasteSpecial xlValues

ActiveWorkbook.SaveAs Filename:="c:Bodytext.txt", FileFormat:=xlTextMSDOS

ActiveWindow.Close



<FONT color=#ff0000>'Create BAt-file
</FONT>
<FONT color=blue>Open </FONT>"XL.bat" <FONT color=blue>For </FONT><FONT color=blue>Output</FONT><FONT color=blue> As</FONT> #1

<FONT color=blue>Print </FONT>#1, stComMailexe & " " & "-host=" & stMailVard & " " & "-from=" & stFran & " " & "-to=" & stTill & " " & "-msg=c:bodytext.txt" & " " & "-subject=" & stArende & " " & "-attach=" & stBilagaNamn

<FONT color=blue>Close </FONT>#1



<FONT color=#ff0000>'Execute
</FONT>
RetVal = Shell("c:XL.bat", 0)



<FONT color=#ff0000>'Make a delay enablighs Shell to finish up
</FONT>
<FONT color=#ff0000>'and then kill some of files
</FONT>
Application.Wait Now + TimeValue("00:00:10")

Kill "c:Bilaga" &<FONT color=blue> Date</FONT> & ".xls"

Kill "c:Bodytext.txt"



<FONT color=blue>With </FONT>Application

.ScreenUpdating =<FONT color=blue> False</FONT>

.DisplayAlerts =<FONT color=blue> False</FONT>

<FONT color=blue>End With</FONT>



<FONT color=blue>End Sub</FONT>


</PRE>

Instead of creating the attachmentfile as above You replace it with Your zip-file and the path to the zipfile.

In order to have it to work You need to download a small software - Command Mail - which is a freeware and can be downloaded from following site:

http://www.xwebware.com/products/commail/index.html

Kind regards,
Dennis

Ps Propably some of the more regular posters may have objection with this solution - They are welcome :wink: Ds
 
Upvote 0
Thanks Dennis
I'll try it tommorow (It's a good excuse to get out of chores).
Terry
This message was edited by Terry P on 2002-12-07 17:42
 
Upvote 0
Dennis have given you one option and I'm sure Tom will come up with another.
Here is another option (NB: Not fully tested Xl2000 WinXp)

Notes:
1) Assumes OutLook is your mailer
2) Must be run on a workbook that is Open
- will close it after
3) Change constants to reflect where your files are/going.
4) Uses API to determine when the Winzip process is finished rather then a delayed time.

<PRE><FONT color=blue>Option Explicit</FONT>



Private Declare <FONT color=blue>Function </FONT>OpenProcess Lib "kernel32" ( _

<FONT color=blue>ByVal</FONT> dwDesiredAccess <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>, _

<FONT color=blue>ByVal</FONT> bInheritHandle <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>, _

<FONT color=blue>ByVal</FONT> dwProcessId <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>) <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>



Private Declare <FONT color=blue>Function </FONT>GetExitCodeProcess Lib "kernel32" ( _

<FONT color=blue>ByVal</FONT> lnghProcess <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>, _

lpExitCode <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>) <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>



<FONT color=#008000>'// If your going to be working with Systems that support security
</FONT>
<FONT color=#008000>'// settings eg NT, XP the access will be checked against any
</FONT>
<FONT color=#008000>'// security descriptor for the target process, so use this Const
</FONT>
<FONT color=#008000>'// Sets all possible access flags for the process object.
</FONT>
Private <FONT color=blue>Const </FONT>PROCESS_ALL_ACCESS = &H1F0FFF



<FONT color=#008000>'// Note spaces important
</FONT>
Private <FONT color=blue>Const </FONT>ZipExePath <FONT color=blue>As</FONT><FONT color=blue> String</FONT> = "C:Program filesWinzip"

Private <FONT color=blue>Const </FONT>ZipCom <FONT color=blue>As</FONT><FONT color=blue> String</FONT> = "Winzip32 -a "

Private <FONT color=blue>Const </FONT>strDest <FONT color=blue>As</FONT><FONT color=blue> String</FONT> = "C:WINDOWSTempMyTest.zip"



<FONT color=blue>Public </FONT><FONT color=blue>Function </FONT>ShlProc_IsRunning(ShellReturnValue <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>) <FONT color=blue>As</FONT> <FONT color=blue>Boolean</FONT>

<FONT color=blue>Dim </FONT>lnghProcess <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>

<FONT color=blue>Dim </FONT>lExitCode <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>



<FONT color=#008000>'//Get the process handle
</FONT>
lnghProcess = OpenProcess(PROCESS_ALL_ACCESS, 0&, ShellReturnValue)

<FONT color=blue>If </FONT>lnghProcess <> 0 Then

<FONT color=#008000> '// The GetExitCodeProcess function retrieves the
</FONT>
<FONT color=#008000> '// termination status of the specified process.
</FONT>
GetExitCodeProcess lnghProcess, lExitCode

<FONT color=blue>If </FONT>lExitCode <> 0 Then

<FONT color=#008000> '// Process still ALIVE!
</FONT>
ShlProc_IsRunning =<FONT color=blue> True</FONT>

<FONT color=blue>Else</FONT>

<FONT color=#008000> '// YES...finished @ last
</FONT>
ShlProc_IsRunning =<FONT color=blue> False</FONT>

<FONT color=blue>End If</FONT>

<FONT color=blue>End If</FONT>



<FONT color=blue>End Function</FONT>



<FONT color=blue>Sub </FONT>ShellZipAndEmailIt()

<FONT color=blue>Dim </FONT>ZipItPID <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>

<FONT color=blue>Dim </FONT>strSource <FONT color=blue>As</FONT><FONT color=blue> String</FONT>

<FONT color=blue>Dim </FONT>OLook <FONT color=blue>As</FONT><FONT color=blue> Object</FONT>

<FONT color=blue>Dim </FONT>Mitem <FONT color=blue>As</FONT><FONT color=blue> Object</FONT>

<FONT color=blue>Dim </FONT>OlAttachment <FONT color=blue>As</FONT><FONT color=blue> Object</FONT>



strSource = "C:WINDOWSTempMyTest.xls"



<FONT color=blue>If </FONT>ActiveWorkbook.Name = ThisWorkbook.Name Then

MsgBox "Cannot Zip and email this workbook" & vbCr & _

"select another workbook and run this again."

End

<FONT color=blue>End If</FONT>



<FONT color=blue>With </FONT>ActiveWorkbook

.Save<FONT color=blue>As</FONT> strSource

.Close<FONT color=blue> False</FONT>

<FONT color=blue>End With</FONT>

<FONT color=#008000>'//
</FONT>
<FONT color=#008000>'// When you Shell out to an Application the Return Value
</FONT>
<FONT color=#008000>'// is the Applications Task ID
</FONT>
<FONT color=#008000>'// in order to determine if it has Terminated we need to check
</FONT>
<FONT color=#008000>'// if there is an existing process object
</FONT>
<FONT color=#008000>'// > OpenProcess function opens an existing process object.
</FONT>
<FONT color=#008000>'//
</FONT>


<FONT color=blue>On Error</FONT> <FONT color=blue>Resume </FONT><FONT color=blue>Next</FONT>

<FONT color=#008000>'//
</FONT>
ZipItPID = Shell(ZipExePath & ZipCom & strDest & " " & strSource, vbNormalFocus)

<FONT color=blue>On Error</FONT> <FONT color=blue>GoTo</FONT> 0



<FONT color=blue>If </FONT>ZipItPID = 0<FONT color=blue> Then </FONT>MsgBox "NoGo!" & vbCr & "Check your Path": End



<FONT color=#008000>'// Ok, lets loop until the App process is terminated!
</FONT>
<FONT color=blue>Do </FONT>While ShlProc_IsRunning(ZipItPID) =<FONT color=blue> True</FONT>

DoEvents

<FONT color=blue>Loop</FONT>





<FONT color=blue>Set </FONT>OLook = CreateObject("Outlook.Application")

<FONT color=blue>Set </FONT>Mitem = OLook.CreateItem(0)

<FONT color=blue>Set </FONT>OlAttachment = Mitem.Attachments



OlAttachment.Add strDest, ol<FONT color=blue>ByVal</FONT>ue, 1, "Updated Excel<FONT color=blue> Workbook</FONT>"



<FONT color=blue>With </FONT>Mitem

.<FONT color=blue>To </FONT>= "ivanmoala@xtra.co.nz"

.Subject = "New<FONT color=blue> Workbook</FONT>"

.Body = "Here is the latest update"

.Send

<FONT color=blue>End With</FONT>



ErrorHandler:



<FONT color=blue>Set </FONT>OLook =<FONT color=blue> Nothing</FONT>

<FONT color=blue>Set </FONT>Mitem =<FONT color=blue> Nothing</FONT>

<FONT color=blue>Set </FONT>OlAttachment =<FONT color=blue> Nothing</FONT>



<FONT color=#008000>'// Cleanup
</FONT>
Kill strSource

Kill strDest



<FONT color=blue>End Sub</FONT>




</PRE>
 
Upvote 0
This has been a marvelleous response and I thank you all.
It's nearly 1 a.m here in dull old UK so I'm off to bed. I'll try all your suggestions tomorrow.

By way of explaination, It's my users whose life I'm trying to make as simple as possible as some are in their 70's.

Forgive me for being cheeky but I had recently posted my very first post which I never resolved but with brains like your there may be a solution:
It seems very simple but I can't get my head around it.

I want my users to enter dates in column A as d/mm/yy eg 2/10/02 which automatically converts to 2 Oct 02.
I am using the worksheet_change function so thought I could enter something like if activecolumn = B and column A <> Like #/##/## then messbox("Re-enter date") etc.
I've tried various ways including validation to no avail. Any hints?

My very best wishes
Terry
PS Ivan, hurry up with your wesite. I suppose it's people like me taking your time up!
 
Upvote 0
Hi Terry

If I understand you correctly then
Try this ??

<pre/>

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
If Not Target.Text Like "*/*/*" Then
With Application
.EnableEvents = False
.Undo
.EnableEvents = True
End With
MsgBox "Please reenter date as > d/mm/yy eg 2/10/02"
Else
Target.NumberFormat = "d mmm yy"
End If
End If
End Sub


</pre>
 
Upvote 0
Wow!
No need for me to post! If all else fails, I'll post a code example I use that automatically sends me email from work every day using the default mail client.(Uses Mapi)(Outlook Express) Just out of curiosity? Are your users required to zip up their file manually? Reason I ask is the same procedure I wrote about above uses Winzip's command line to automatically zip up many files and then sends the attachment to my mail account. Very easy to use and free if you have winzip.
Tom
 
Upvote 0
On 2002-12-07 23:25, TsTom wrote:
Wow!
No need for me to post! If all else fails, I'll post a code example I use that automatically sends me email from work every day using the default mail client.(Uses Mapi)(Outlook Express) Just out of curiosity? Are your users required to zip up their file manually? Reason I ask is the same procedure I wrote about above uses Winzip's command line to automatically zip up many files and then sends the attachment to my mail account. Very easy to use and free if you have winzip.
Tom

Hi Tom
Please post your code for O Express using MAPI as not everyone has outlook :(
I would be interested in this.
Also note: The code I supplied does zip the file and then emails it...can also automate
the extraction of the zip from upon delivery
But thats another post.
Thanks in advance
 
Upvote 0

Forum statistics

Threads
1,216,028
Messages
6,128,392
Members
449,445
Latest member
JJFabEngineering

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top