word automation and emailing attachments in vb.net

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)
 
S

Steve

Just another note...

Just an add on note...Here's the code I have so far

WordApp.ActiveWindow.View.Zoom.Percentage = 75
WordApp.ActiveWindow.WindowState =
Word.WdWindowState.wdWindowStateNormal
WordApp.ActiveWindow.Height = Me.Height - 300
WordApp.ActiveWindow.Width = Me.Width - 275
WordApp.ActiveWindow.Top = Me.Top + ((Me.Height -
WordApp.ActiveWindow.Height) / 2)
WordApp.ActiveWindow.Left = Me.Left + ((Me.Width -
WordApp.ActiveWindow.Width) / 2)

Where me. is the form initiating the word automation. Me.top = 113.
Me.height = 767. Me.Left = 242. Me.width = 796. So based on my
calculations, ActiveWindow.height = 467. ActiveWindow.width = 521 making it
smaller than the form it displays on top of. ActiveWindow.Top would then
be equal to 263. And ActiveWindow.Left would then be equal to 380. Which
should center the activewindow in the middle of the form. But instead it
displays it to the right and below.

Not sure what I'm doing wrong.

Thanks

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top