954,184 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

What is the problem with this line of my code?

Here is VB6 Code:

Attachement = objMail.Attachments.Add("D:\message.doc.pgp")


I am trying to automatically attach a file with outlook email......objMail.Attachments.Add method is unable to read the file "message.doc.pgp" very first time when the code execute.....On second n later executions it start recognizing the file n work fine but y not the first time.

I create the file "message.doc.pgp" by using shell command as follow.

Shell "command.com /c pgp -e " & "d:\message.doc " & "Imran Khalid"


....when this command execute....a file "D:\message.doc.pgp" is created automatically which i later wants to sends as an email attachment....but not recognized during first time code execution as mentioned above.

Thanx a lot

ikhalid
Newbie Poster
3 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

Since VB's Shell statement starts the outside program and then passes control to the next line of code right away, the outside program probably doesn't have a chance to finish running before you try to attach the file to the email message. You'll get the error because the outside program is still writing the file when your code tries to attach it.

Luckily, the fix for this is really easy. All you have to do is put the following code after your Shell statement, and then tweak the value of the variable "waitSeconds" so that you're waiting long enough for the outside program to finish, but not so long that it looks like your program isn't doing the job:

waitSeconds = 5        ' Wait for 5 seconds before trying to use the file
startTime = Timer
Do Until Timer - waitSeconds >= startTime
    DoEvents           ' Don't want the user to think it's frozen!
Loop


That should fix the problem! Good luck!

- Sen

sendoshin
Light Poster
39 posts since Sep 2006
Reputation Points: 16
Solved Threads: 1
 

Load of thanx

ikhalid
Newbie Poster
3 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You