word automation 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

susiedba

sorry bud it is as simple as Word Automation

but of course; Microsoft doesn't support vb6 anymore so you're shit out
of luck

I would build a time machine and go back to the 90s and never bother
with VB.NOT

-Susie, DBA
 
B

BK

Disregard "Susie", he/she/it suffers from split personalities and none
of them are real programmers....

I think you want to look at the SmtpClient.SendCompleted documentation,
that's what you are missing.
 
T

The Grim Reaper

You need a WordDoc.Quit after your WordDoc.Close.
_________________________________
The Grim Reaper
 
S

Steve

But this will close the word application which the user running the program
will want to keep open for printing the active document. I am trying to
blindly send an email (legit) during the process.

Steve
 
R

RobinS

Could you save a separate copy as a temp file and send that
in the e-mail and then delete it?

Robin S.
--------------------------
 
S

Steve

Actually, I discovered it's locked by vshost.exe - which appears to only be
shut down when the host application is shut down. I guess VB2005 uses
vshost.exe for performance improvement/debugging.

Anyway, not sure what to do for now. Note: I'm developing the application
as I type this.

Any ideas from anyone?

Robin:

How would I create a temp copy of the file?

Steve
 
R

RobinS

The only reason I'd create a second (temp) copy is
because you said you want to leave Word open for the
user to print the information in the Word document.
But if Word is open, the file is still locked.

The problem is, if you leave it open, and don't
save it (so it's nowhere), you can't e-mail it.
And if you save it, you can't delete it because
you want to leave it open for your user. And
when he gets around to closing Word, your application
has already released control of it, and you can't go
back and delete it.

I used to create CSV files and then open them
in Excel using OLE Automation. The user could then
save the file as an Excel file to wherever he
wanted to.

My program let go of the file as soon as it
showed it to the user in Excel. But I still had
the pesky CSV files, and couldn't delete them
if the user still had them open.

So this is what I did: I created the files in my
application directory and named them with a specific
pattern, like Temp_yymmddhhmmss.csv.

Then every time they started up the application,
it would delete any files it found in the application
folder with the pattern Temp_*.csv.

Okay, it's tacky, but it worked, and I'm open to
other suggestions. Since they ran the app all the
time, it was always "self-cleaning". The other nice
thing about it was if they didn't save the Excel
file, they could go open the CSV before they
started up the app again and get their data.

So you could save the Word document in the same fashion
to somewhere (the windows temp folder? the application
folder?) and e-mail it and leave it open. And when
your app starts up, go delete any that you find. Be
sure to catch the exception of the file being locked,
in case they still have it open. If you put it
in the windows temp folder, I'd name it something
really specific, like MyAppName_Temp_yymmddhhmmss.doc,
so you don't accidentally delete something else
when you go back to delete them.

You can access the name of the Windows temp folder
using this (VB2005):

Dim tempFolder as String = New Path.GetTempPath

You could get a temporary file name like this, too,
but then you couldn't delete them yourself (also VB2005):

Dim tempFileName as String = New Path.GetTempFileName

If they don't run your app more than once, I don't
know that I would do this. The temp files don't get
deleted automatically. If you *do* do this, and save
them to the Windows temp folder, and they hardly ever
run your app, I'd save them with GetTempFileName name.

By the way, when you double-click on an attachment
in Outlook and open it, it saves it to the Windows
temp folder and then opens it. It must have some
kind of clean-up, or maybe not.

I hope that helps. Other ideas are welcome!

Robin S.
-------------------------------------
 
R

Robinson

So this is what I did: I created the files in my
application directory and named them with a specific
pattern, like Temp_yymmddhhmmss.csv.

Then every time they started up the application,
it would delete any files it found in the application
folder with the pattern Temp_*.csv.

I do this with temporary files. Typically I generate a hundred or so during
an average running session, which are all in a user data directory. This
directory is cleaned during program startup.
 
C

Chris Dunaway

Steve said:
' Release the references.

WordMailMerge = Nothing
WordDoc = Nothing
WordApp = Nothing

Setting the objects to Nothing does not release them. For COM objects
you need to call Marshal.ReleaseComObject.
 
R

RobinS

Thanks -- that makes me feel better. I just couldn't think
of another way. I was afraid I was going to get a lot of
negative feedback for the method I chose!

Robin S.
-------------------------------
 
S

Steve

I figured out a solution to my problem. After client.send, I issue a
mymail.dispose. It releases the hold on the file which allows me to delete
it.

Steve

-----

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
myMail.Dispose()
 
R

RobinS

Woohoo! Thanks for letting us know.
Congrats.
Robin S.
--------------------------------------
 

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