An ASP page first creates a MS-Word (doc) document using the values
received from a HTML Form & then mails this doc file as an attachment
for which I am using CDO.MESSAGE but the line
cdoMessage.AddAttachment Server.MapPath(strFile)
generates the following error:
The process cannot access the file because it is being used by another
process.
which points to the AddAttachment line (shown above).
How do I overcome this error?
You'll have a Word application object still holding the file open. As the
link that Bob has posted in a later thread indicates, this not a good
idea.
Anthony, if I am not mistaken, what Bob has suggested is very much
different to what I want to do here. What Bob has suggested that is
for opening a Word file using ASP but here I want to first create the
Word document (which I have taken care of) & then mail it as an
attachment. At no point do I want to open that Word document that ASP
creates.
Please post the code that creates the Word document.
Anyway, how do I ensure that the Word object is not holding the file
open?
My advice is don't use Office objects in ASP.
Here is the code that creates the Word document Anthony:
<%
Dim strParty,strNewFile,strFolderPath
strParty=Request.Form(strParty)
strNewFile="LETTERS\Letter" & Replace(strParty," ","") & ".doc"
strFolderPath="LETTERS"
Dim objFSO,objFolder,objFile
Set objFSO=Server.CreateObject("SCRIPTING.FILESYSTEMOBJECT")
Set objFolder=objFSO.GetFolder(Server.MapPath(strFolderPath))
Const ForAppending=8
Const ForWriting=2
If Not(objFSO.FileExists(Server.MapPath(strNewFile))) Then
Set objFile=objFSO.CreateTextFile(Server.MapPath(strNewFile))
Else
Set
objFile=objFSO.OpenTextFile(Server.MapPath(strNewFile),ForWriting)
End If
<<<<<<<<
The above can be reduced to:-
Set objFile=objFSO.OpenTextFile(Server.MapPath(strNewFile),ForWriting, True)
There is no need to test for FileExist.
objFile.WriteLine("............")
objFile.WriteLine("............")
objFile.WriteLine("............")
<<<<<<<<
Where is objFile.Close ?
I'm no expert in office file formats but it surprises me that you can write
a word document using a text stream object.
'e-mail code using CDO.MESSAGE to send
'the DOC file as an attachment comes here
'deleting the DOC document after 5 hours
For Each objFile In objFolder.Files
If(InStr(objFile.Name,".doc")>0) Then
If(DateDiff("n",objFile.DateLastModified,Now())>300) Then
objFile.Delete
End If
End If
Next
Set objFile=Nothing
Set objFSO=Nothing
<<<<<<<<
Then how does one send Office documents as attachments in ASP?
You're not using office objects so don't worry about it.