|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
word automation vb.neta 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) 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. You need a WordDoc.Quit after your WordDoc.Close.
_________________________________ The Grim Reaper Show quoteHide quote "Steve" <s.ar***@comcast.net> wrote in message news:eOB0SXmEHHA.2356@TK2MSFTNGP03.phx.gbl... > 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) > > > > 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 Show quoteHide quote "The Grim Reaper" <grim_rea***@REMOVEbtopenworld.com> wrote in message news:-NidnWSQ689l__bYRVnytQ@bt.com... > You need a WordDoc.Quit after your WordDoc.Close. > _________________________________ > The Grim Reaper > > "Steve" <s.ar***@comcast.net> wrote in message > news:eOB0SXmEHHA.2356@TK2MSFTNGP03.phx.gbl... > > 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) > > > > > > > > > > Could you save a separate copy as a temp file and send that
in the e-mail and then delete it? Robin S. -------------------------- Show quoteHide quote "Steve" <s.ar***@comcast.net> wrote in message news:%23jnNTInEHHA.4256@TK2MSFTNGP04.phx.gbl... > 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 > > "The Grim Reaper" <grim_rea***@REMOVEbtopenworld.com> wrote in message > news:-NidnWSQ689l__bYRVnytQ@bt.com... >> You need a WordDoc.Quit after your WordDoc.Close. >> _________________________________ >> The Grim Reaper >> >> "Steve" <s.ar***@comcast.net> wrote in message >> news:eOB0SXmEHHA.2356@TK2MSFTNGP03.phx.gbl... >> > 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) >> > >> > >> > >> > >> >> > > 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 Show quoteHide quote "RobinS" <RobinS@NoSpam.yah.none> wrote in message news:WbOdnXVnj6oQ4PbYnZ2dnUVZ_r-dnZ2d@comcast.com... > Could you save a separate copy as a temp file and send that > in the e-mail and then delete it? > > Robin S. > -------------------------- > "Steve" <s.ar***@comcast.net> wrote in message > news:%23jnNTInEHHA.4256@TK2MSFTNGP04.phx.gbl... > > 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 > > > > "The Grim Reaper" <grim_rea***@REMOVEbtopenworld.com> wrote in message > > news:-NidnWSQ689l__bYRVnytQ@bt.com... > >> You need a WordDoc.Quit after your WordDoc.Close. > >> _________________________________ > >> The Grim Reaper > >> > >> "Steve" <s.ar***@comcast.net> wrote in message > >> news:eOB0SXmEHHA.2356@TK2MSFTNGP03.phx.gbl... > >> > 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) > >> > > >> > > >> > > >> > > >> > >> > > > > > > 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. ------------------------------------- Show quoteHide quote "Steve" <s.ar***@comcast.net> wrote in message news:eLmoRLoEHHA.3520@TK2MSFTNGP04.phx.gbl... > 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 > > "RobinS" <RobinS@NoSpam.yah.none> wrote in message > news:WbOdnXVnj6oQ4PbYnZ2dnUVZ_r-dnZ2d@comcast.com... >> Could you save a separate copy as a temp file and send that >> in the e-mail and then delete it? >> >> Robin S. >> -------------------------- >> "Steve" <s.ar***@comcast.net> wrote in message >> news:%23jnNTInEHHA.4256@TK2MSFTNGP04.phx.gbl... >> > 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 >> > >> > "The Grim Reaper" <grim_rea***@REMOVEbtopenworld.com> wrote in message >> > news:-NidnWSQ689l__bYRVnytQ@bt.com... >> >> You need a WordDoc.Quit after your WordDoc.Close. >> >> _________________________________ >> >> The Grim Reaper >> >> >> >> "Steve" <s.ar***@comcast.net> wrote in message >> >> news:eOB0SXmEHHA.2356@TK2MSFTNGP03.phx.gbl... >> >> > 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) >> >> > >> >> > >> >> > >> >> > >> >> >> >> >> > >> > >> >> > > > So this is what I did: I created the files in my I do this with temporary files. Typically I generate a hundred or so during > 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. an average running session, which are all in a user data directory. This directory is cleaned during program startup. 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. ------------------------------- Show quoteHide quote "Robinson" <b**@bbb.com> wrote in message news:lqWdnaW6x-GqZPbYnZ2dnUVZ8tidnZ2d@giganews.com... > >> 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. > > > 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() Show quoteHide quote "RobinS" <RobinS@NoSpam.yah.none> wrote in message news:Nd6dneRc9M2bGfHYnZ2dnUVZ_vadnZ2d@comcast.com... > 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. > ------------------------------- > "Robinson" <b**@bbb.com> wrote in message > news:lqWdnaW6x-GqZPbYnZ2dnUVZ8tidnZ2d@giganews.com... > > > >> 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. > > > > > > > > Woohoo! Thanks for letting us know.
Congrats. Robin S. -------------------------------------- Show quoteHide quote "Steve" <s.ar***@comcast.net> wrote in message news:ua28By$EHHA.5000@TK2MSFTNGP03.phx.gbl... >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() > > > > > "RobinS" <RobinS@NoSpam.yah.none> wrote in message > news:Nd6dneRc9M2bGfHYnZ2dnUVZ_vadnZ2d@comcast.com... >> 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. >> ------------------------------- >> "Robinson" <b**@bbb.com> wrote in message >> news:lqWdnaW6x-GqZPbYnZ2dnUVZ8tidnZ2d@giganews.com... >> > >> >> 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. >> > >> > >> > >> >> > > Steve wrote:
> ' Release the references. Setting the objects to Nothing does not release them. For COM objects> > WordMailMerge = Nothing > WordDoc = Nothing > WordApp = Nothing you need to call Marshal.ReleaseComObject.
Arranging window screens on multi-monitor Desktop
Re: Help needed in using FSO's, TextStreams, etc. --- Code Review and Advice requested listbox's SelectedIndexChanged keep fire Array Question, Out of Bounds better way to program than this? Display Outlook Recipient Window from vb.net sqlDataReader does not return correct sorted data Errors when running from LAN server. using file system in web site Form level focus monitor |
|||||||||||||||||||||||