Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Getting vbscript to wait until a file is present

Options
  • 15-10-2011 9:02pm
    #1
    Registered Users Posts: 9


    Hi I hope somebody can help me with this. I am trying to write an application that sends and receives service calls from a pc to a mobile phone.

    i am using a program called mobile data studio to do most of the work as i am new to this and not a programmer so please excuse my ignorance.

    basically the program generates a webpage as its report for a customer and this is mailed to the customer by the system which i have working :)

    The problem is that the systems does not wait until the file is generated before it tries to send it as an attachment and i get an error:

    CDO.Message1

    The system cannot find the file specified.

    Position: 58.0


    this is the code:

    objmessage.Addattachment sFile


    onece 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.

    This is all the 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\"& theSession("ORN")&"."&"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" & " " & theSession("rdate")
    objMessage.From = """Service Department"" <user@mydomain>"
    objMessage.To = sEmail
    objMessage.TextBody = "Hi " & theSession("sname") & ","
    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

    Any help would be appreciated.


    Regards,

    Darren


Comments

  • Moderators, Politics Moderators Posts: 39,600 Mod ✭✭✭✭Seth Brundle


    could you add in a loop that runs every say 30 seconds and checks if the file is there?


  • Closed Accounts Posts: 577 ✭✭✭Galtee


    darren.w wrote: »
    Hi I hope somebody can help me with this. I am trying to write an application that sends and receives service calls from a pc to a mobile phone.

    i am using a program called mobile data studio to do most of the work as i am new to this and not a programmer so please excuse my ignorance.

    basically the program generates a webpage as its report for a customer and this is mailed to the customer by the system which i have working :)

    The problem is that the systems does not wait until the file is generated before it tries to send it as an attachment and i get an error:

    CDO.Message1

    The system cannot find the file specified.

    Position: 58.0

    this is the code:

    objmessage.Addattachment sFile


    onece 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.

    This is all the 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\"& theSession("ORN")&"."&"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" & " " & theSession("rdate")
    objMessage.From = """Service Department"" <user@mydomain>"
    objMessage.To = sEmail
    objMessage.TextBody = "Hi " & theSession("sname") & ","
    objmessage.Addattachment sFile
    Set objfax = CreateObject("WScript.Shell")
    objfax.Run sFile
    ///////////////////////
    // Insert Code Here
    //////////////////////

    //////////////////////

    '==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

    Any help would be appreciated.


    Regards,

    Darren

    I really don't know anything about VBScript but I butchered the below from some Microsoft samples

    // this is your code segment
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    Dim numTries
    numTries = 0
    Do While ((Not fso.filexists(sFile)) and (numTries < 5)
    numTries = numTries + 1 // Increment numtries to ensure we exit loop
    Sleep(10000) // Sleep for 10 seconds
    Loop
    // Check to see if file actually exists or if we hit numtries
    // If still no file then exit;
    if (Not fso.filexists(sFile))
    Exit sub
    End if

    I think you could also check the return value of objFax if you're sure that the file will always be created eventually and so the following may work too?
    Do while (objfax = 0)
    Sleep(5000) // Sleep for 5 secs before checking again
    Loop

    Hope this helps a little.


  • Registered Users Posts: 9 darren.w


    Thanks lads for the replies. Galtee i tried placing your script where you indicated but when i run the script with the application it just ignores the entire script and creates the file.

    i replaced the // with ' to comment and tried again but the same result.

    I think i need a way to tell the application (Mobile data studio) to do it's bit and then come back and read this script. if no script exists in the data script window the file is still produced by the application (Mobile data studio).

    Mobile data studio has a default data pathway (see screenshot)

    ' Set the return value to true to indicate that normal
    ' processing should continue
    OnIncomingSession = True

    i have set this to false and as far as i can tell it make no difference.


    Thanks again.

    Darren


  • Registered Users Posts: 2,781 ✭✭✭amen


    just so I am clear you have users reporting/requesting service calls which you take and enter on a pc.

    You then have a custom VBScript application running on the PC which calls a 3rd Party Application (Mobile Data Studio) which
    A: Send the service request to a mobile phone
    B: Generates a html report.

    If that's the case I would suggest putting the call to the email inside a while loop and check for the file every 5 seconds. If you get the file email (and exit the loop so you don't send the file multiple times) if not try again.

    After 30 seconds I would exit the loop if no match found.

    You should look at Windows Scripting Host (a bit nicer then using VBScript).

    How are you logging the fact that you sent/did not send the email to the customer ?


  • Registered Users Posts: 9 darren.w


    Hi amen,

    Thanks for the reply.

    All the work is done through the application (Mobile Data Studio)http://www.creativitycorp.com/mds/skills/page12

    The call is created in the application and sent to the mobile (customer name, address, phone, problem etc...)

    Once the call is completed the call is sent back to the pc and stored in the applications database for further processing ie the html file is created in this case.

    "If that's the case I would suggest putting the call to the email inside a while loop and check for the file every 5 seconds. If you get the file email (and exit the loop so you don't send the file multiple times) if not try again.

    After 30 seconds I would exit the loop if no match found."


    I would but have no idea how to do this. i have tried this however:

    ' Get the email address from the session
    sEmail = theSession ( "EmailAddress" )

    'Get the file name from the session
    sFile = "C:\htm\"& theSession("ORN")&"."&"htm"

    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    Do While Not fso.FileExists(sFile)
    Loop
    Set fso = Nothing


    Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.

    but it just causes the app to crash because the file is not created until this script completes.

    "You should look at Windows Scripting Host (a bit nicer then using VBScript)."

    Mobile data Studio requires VBScript for its data window however i think it can read in other languages not sure how to do this either i'm afraid trying to teach myself (old dog new tricks)

    Thanks again for the reply.

    Regards,


    Darren


  • Advertisement
  • Registered Users Posts: 2,781 ✭✭✭amen


    been a while but try



    [PHP]
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    Dim whileCounter
    whileCounter =0
    Do While whileCounter <6

    For i = 0 To 1000
    Next
    if fso.FileExists(sFile)
    (
    Call function to send mail
    whileCounter = 7 so we can exit the while
    )
    else
    (
    whileCounter = whileCounter+1
    )
    Loop
    Set fso = Nothing
    [/PHP]


  • Registered Users Posts: 9 darren.w


    Hi amen,

    Thank for the reply. i tried placing your code into the data window but when i run the processing pathway it just ignores the script completely and creates the file. Argh!

    I think i need to have a function after the

    " ' Process incoming sessions from Pocket PCs
    Function OnIncomingSession (theSession) " that processes the mail something like this:

    'When Idle check for the file
    Function OnIdle (theSession)

    Dim checkforfile
    set checkforfile=CreateObject("Scripting.FileSystemObject")
    If checkforfile.FileExists("c:\htm\" &theSession ("ORN" &"." &"html") Then

    ' Check if the user indicated a confirmation was desired
    ElseIf theSession("SendEmail") = "Yes" Then

    sendobjMessage theSession

    Else 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" )
    '
    rest of mail code
    '
    '
    '
    objMessage.Send

    End Sub




    Not sure how to add this code or if i need to do more (sub or loop maybe.)

    'Check if the file exists and then move it to processed mail folder

    dim filesys
    set filesys=CreateObject("Scripting.FileSystemObject")
    If filesys.FileExists("c:\htm\" &theSession ("ORN" &"." &"html") Then
    filesys.MoveFile "c:\htm\" &theSession ("ORN" &"." &"html", "c:\htm\processed" &theSession ("ORN" &"." &"html"

    End If

    Anyway thanks again,

    Darren


  • Registered Users Posts: 9 darren.w


    Hey guys, i have been looking at WMI to see how i could use it for this but for this issue i still need to figure out a way to get the original script to run again once it sees the file and also to stop it creating an error when it doesn't.

    I need a way to stop the send mail script from executing for a few seconds (as if it was finished) and start again once the file has been created.

    Current Steps:

    1.Service report is sent from the phone back to mobile data studio for processing.
    2. mobile data studio process the call in the based on the script in the data window (send mail code)
    3. this fails because the file has not been created so the error appears.
    4. Once the error is cleared default processing continues and the file is created "C:\htm\ & theSession ("ORN") &"." & "htm"
    5. if i go into the database an right click > select send to> data processing pathway the script runs again and the mail with attachment is sent out.

    Required steps:

    1.Service report is sent from the phone back to mobile data studio for processing.

    2. Send mail script needs to be bypassed and let default processing continue.
    3. once this happens send mail script needs to start and finish executing.

    How can i tell the send mail script to stop or wait for a short period of time before starts while tricking the default pathway into thinking it has run and it needs to created the file.

    this seemed like a good idea at the time - not so sure now :)

    Cheers,

    Darren


Advertisement