title: Getting vbscript to wait until a file is present
body: <p>I'm trying to write an application that sends and receives service calls from a pc to a mobile phone. </p>
  
  <p>I'm using a program called mobile data studio to do most of the work. </p>
  
  <p>Basically the program generates a web-page as its report for a customer and this is mailed to the customer by the system which i have working </p>
  
  <p>The problem is that the system does not wait until the file is generated before it tries to send it as an attachment and i get an error:</p>
  
  <p>CDO.Message1</p>
  
  <blockquote>
    <p>The system cannot find the file specified.</p>
    
    <p>Position: 58.0</p>
  </blockquote>
  
  <p>this is the code:</p>
  
  <pre><code>objmessage.Addattachment sFile
  </code></pre>
  
  <p>Once I click OK on the error the file is then created and if I run the script again it process the mail and the attachment and opens the file if fax is set to "yes" also.</p>
  
  <p>This is all the code:</p>
  
  <pre><code>' Process incoming sessions from Pocket PCs
  
  Function OnIncomingSession (theSession)             
  ' Check if the user indicated a confirmation was desired
  If theSession("SendEmail") = "Yes" Then    
   sendobjMessage theSession     
      ElseIf theSession("SendFax") = "Yes" Then      
   sendobjfax theSession               
   End If
  
     ' Set the return value to true to indicate that normal
  ' processing should continue
  OnIncomingSession = True 
  
  End Function
  
  Sub sendobjMessage (theSession)
  ' Get the email address from the session
   sEmail = theSession ( "EmailAddress" )
  
   'Get the file name from the session
   sFile = "C:\htm\"&amp; theSession("ORN")&amp;"."&amp;"htm"   
  
  Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory. 
  Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network). 
  
  Const cdoAnonymous = 0 'Do not authenticate
  Const cdoBasic = 1 'basic (clear-text) authentication
  Const cdoNTLM = 2 'NTLM
  
  Set objMessage = CreateObject("CDO.Message") 
  objMessage.Subject = "Our Company  - Service Report" &amp; " " &amp; theSession("rdate")  
  objMessage.From = """Service Department"" &lt;user@mydomain&gt;" 
  objMessage.To = sEmail
  objMessage.TextBody = "Hi " &amp; theSession("sname") &amp; ","
  objmessage.Addattachment sFile
  
  Set objfax = CreateObject("WScript.Shell")
  objfax.Run sFile 
  
  '==This section provides the configuration information for the remote SMTP server.
  
  objMessage.Configuration.Fields.Item _
  ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
  
  'Name or IP of Remote SMTP Server
  objMessage.Configuration.Fields.Item _
  ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.mydomain.com"
  
  'Type of authentication, NONE, Basic (Base64 encoded), NTLM
  objMessage.Configuration.Fields.Item _
  ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
  
  'Your UserID on the SMTP server
  objMessage.Configuration.Fields.Item _
  ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "user@mydomain"
  
  'Your password on the SMTP server
  objMessage.Configuration.Fields.Item _
  ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
  
  'Server port (typically 25)
  objMessage.Configuration.Fields.Item _
  ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
  
  'Use SSL for the connection (False or True)
  objMessage.Configuration.Fields.Item _
  ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
  
  'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
  objMessage.Configuration.Fields.Item _
  ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
  
  objMessage.Configuration.Fields.Update
  
  '==End remote SMTP server configuration section==
  
  objMessage.Send 
  
  End Sub 
  </code></pre>