S
Steve
I've been building an application that will merge fields in a text file with
a word template, save the resulting word file out to the user's hard drive,
and then email the file as an attachment.
The problem I'm having is that I can't delete the word file I saved at the
end of the process due to the file being locked by the email process. It
appears to take longer than the code takes to complete due to virus checking
software that intercepts the mail before releasing it (at least that's what
I've narrowed it down to).
I don't want to leave the file around after the code has completed. And I
don't think I want to hold up the completion of the code process until the
email gets sent in case the email process takes longer on some machines than
others.
I'm not sure if there is a better way to handle the attachment in the code
so I don't have to save a file to the user's desktop. Or if I can use a
deattach process that I can trigger from code to delete the file later. Or
something else.
I've attached a snippet of the code below:
Thanks
Steve
------------
WordDoc.SaveAs(sSaveFileName)
Do While Not WordDoc.Saved
Application.DoEvents()
Loop
WordDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
' Email the Saved Merge Document.
Dim Fromaddress As New MailAddress("fromaddress")
Dim Toaddress As New MailAddress("toaddress")
Dim myMail As New MailMessage(Fromaddress, ToAddress)
myMail.Subject = "test"
Dim AttachmentFile As Attachment = New Attachment(sSaveFileName)
myMail.Attachments.Add(AttachmentFile)
myMail.Priority = MailPriority.High
Dim client As New SmtpClient
client.Host = "hostaddress"
client.Send(myMail)
' Release the references.
WordMailMerge = Nothing
WordDoc = Nothing
WordApp = Nothing
' Delete the temporary files.
System.IO.File.Delete(sSaveFileName)
a word template, save the resulting word file out to the user's hard drive,
and then email the file as an attachment.
The problem I'm having is that I can't delete the word file I saved at the
end of the process due to the file being locked by the email process. It
appears to take longer than the code takes to complete due to virus checking
software that intercepts the mail before releasing it (at least that's what
I've narrowed it down to).
I don't want to leave the file around after the code has completed. And I
don't think I want to hold up the completion of the code process until the
email gets sent in case the email process takes longer on some machines than
others.
I'm not sure if there is a better way to handle the attachment in the code
so I don't have to save a file to the user's desktop. Or if I can use a
deattach process that I can trigger from code to delete the file later. Or
something else.
I've attached a snippet of the code below:
Thanks
Steve
------------
WordDoc.SaveAs(sSaveFileName)
Do While Not WordDoc.Saved
Application.DoEvents()
Loop
WordDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
' Email the Saved Merge Document.
Dim Fromaddress As New MailAddress("fromaddress")
Dim Toaddress As New MailAddress("toaddress")
Dim myMail As New MailMessage(Fromaddress, ToAddress)
myMail.Subject = "test"
Dim AttachmentFile As Attachment = New Attachment(sSaveFileName)
myMail.Attachments.Add(AttachmentFile)
myMail.Priority = MailPriority.High
Dim client As New SmtpClient
client.Host = "hostaddress"
client.Send(myMail)
' Release the references.
WordMailMerge = Nothing
WordDoc = Nothing
WordApp = Nothing
' Delete the temporary files.
System.IO.File.Delete(sSaveFileName)