Micro Focus QTP (UFT) Forums
Cannot attach the text file to email - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: VB Scripting/Descriptive Programming (https://www.learnqtp.com/forums/Forum-VB-Scripting-Descriptive-Programming)
+--- Thread: Cannot attach the text file to email (/Thread-Cannot-attach-the-text-file-to-email)



Cannot attach the text file to email - arpan - 07-17-2013

Hi Guys,

I'm having trouble trying to attach a file to an email. Following is the script and error message i'm getting when i run it. I close all the processes and restarted the machine and still getting this error.
Code:
Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("C:\test.txt", 2, True)

f.WriteLine("Testing Script) & vbcrlf

strSMTPFrom = "abc@xyz.com"
strSMTPTo = "abc@xyz.com"
strSMTPRelay = "mail.xyz.com"
strTextBody = "Testing."
'strSubject = "Testing"
strAttachment = "C:\test.txt"
Set oMessage = CreateObject("CDO.Message")
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSMTPRelay
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
oMessage.Configuration.Fields.Update

oMessage.Subject = strSubject
oMessage.From = strSMTPFrom
oMessage.To = strSMTPTo
oMessage.TextBody = strTextBody
oMessage.AddAttachment strAttachment <--- Getting Error
oMessage.Send

Error Message:

The process cannot access the file because it is being used by another process.

Code:
Line (25): "oMessage.AddAttachment strAttachment".

Any help will be appreciated.

Thanks
Arpan


RE: Cannot attach the text file to email - ssvali - 07-18-2013

Try this...

Save the text file and close it and then send the mail with attachment


RE: Cannot attach the text file to email - arpan - 07-19-2013

Thanks ssvali. It worked. Really appreciate your help.