locked
[VBScript] Creating, Modifying and repeating filetxt.WriteLine RRS feed

  • Question

  • Hi there,

    how can I create a txt file, read it, let VBScript write a line in the file and save it. After it's done, It must be able to run again, and this time just add a line, instead of replacing it. Code included below.

    Sub Auto_Ref_Email()
        Dim msg As Outlook.MailItem
        Set msg = Application.CreateItem(olMailItem)
        Dim filesys, filetxt, getname, path
        Ref_Custom = MsgBox("Would you like to enter a custom Ref_ID?", vbYesNo, "Desicion Time")
        If Ref_Custom = vbYes Then
            Ref_ID = InputBox("Please Enter Custom Ref ID", "Input Required")
            confirm_ref_id = MsgBox("You have entered [" & Ref_ID & "] as a Ref ID. Is this okay?", vbYesNo, "Action Required")
            Do Until confirm_ref_id = vbYes
                Ref_ID = InputBox("Please Enter Custom Ref ID", "Input Required")
                confirm_ref_id = MsgBox("You have entered [" & Ref_ID & "] as a Ref ID. Is this okay?", vbYesNo, "Action Required")
            Loop
            Send_Email = InputBox("Please Enter Email Address")
            confirm_send_email = MsgBox("You have entered [" & Send_Email & "] as an Email Address. Is this okay?", vbYesNo, "Action Required")
            Do Until confirm_send_email = vbYes
                Send_Email = InputBox("Please Enter Email Address")
                confirm_send_email = MsgBox("You have entered [" & Send_Email & "] as an Email Address. Is this okay?", vbYesNo, "Action Required")
            Loop
            msg.To = Send_Email
            msg.Subject = "Automatic Reply: Reference ID - " & Ref_ID
            msg.HTMLBody = "Thank you for contacting Pandora Information Systems. Your communication is valuable to us! We will reply shortly. Your automated reference number is: " & Ref_ID & ". Thank you for your kind support."
            msg.Display
            msg.Send
            Set msg = Nothing
            '   Create Log of Reference Numbers
            Set filesys = CreateObject("Scripting.FileSystemObject")
            Set filetxt = filesys.CreateTextFile("c:\EmailResponder.ref", True)
            path = filesys.GetAbsolutePathName("c:\EmailResponder.ref")
            getname = filesys.GetFileName(path)
            filetxt.WriteLine ("Reference: " & Ref_ID_Parse & "; Email: " & Send_Email)
            filetxt.WriteLine ("-------------------------------------------------------------------------")
            filetxt.Close
        Else
            Ref_ID_part1 = Int((99999 * Rnd) + 1)
            Ref_ID_part2 = Int((999 * Rnd) + 1)
            Ref_ID_part3 = Year(Date)
            '   Combining all three into one variable
            Ref_ID_Parse = Ref_ID_part1 & "/" & Ref_ID_part2 & "/" & Ref_ID_part3
            final_ref_id = MsgBox("Random Ref ID is: [" & Ref_ID_Parse & "]", vbInformation, "Information")
            Send_Email = InputBox("Please Enter Email Address")
            confirm_send_email = MsgBox("You have entered [" & Send_Email & "] as an Email Address. Is this okay?", vbYesNo, "Action Required")
            Do Until confirm_send_email = vbYes
                Send_Email = InputBox("Please Enter Email Address")
                confirm_send_email = MsgBox("You have entered [" & Send_Email & "] as an Email Address. Is this okay?", vbYesNo, "Action Required")
            Loop
            msg.To = Send_Email
            msg.Subject = "Automatic Reply: Reference ID - " & Ref_ID_Parse
            msg.HTMLBody = "Thank you for contacting Pandora Information Systems. Your communication is valuable to us! We will reply shortly. Your automated reference number is: " & Ref_ID_Parse & ". Thank you for your kind support."
            msg.Display
            msg.Send
            Set msg = Nothing
            '   Create Log of Reference Numbers
            Set filesys = CreateObject("Scripting.FileSystemObject")
            Set filetxt = filesys.CreateTextFile("c:\EmailResponder.ref", True)
            path = filesys.GetAbsolutePathName("c:\EmailResponder.ref")
            getname = filesys.GetFileName(path)
            filetxt.WriteLine ("Reference: " & Ref_ID_Parse & "; Email: " & Send_Email)
            filetxt.WriteLine ("------------------------------------------------------------------------")
            filetxt.Close
        End If
    End Sub
    

    Tuesday, July 10, 2012 7:55 PM

Answers

  • Ok - you are trying to work with a very hard to manage design.  A database qqould have been easier.  That said.

    1. OPen file and read to end. 
    2. Get last line number. 
    3. CLose fiel
    4. OPen file in 'appemd' mode and add lines as needed.

    That is all.  No need to edit anything.  Just get the last line and continue.  It just requires  two opens.


    ¯\_(ツ)_/¯

    • Marked as answer by Johan Brink Tuesday, July 10, 2012 10:43 PM
    Tuesday, July 10, 2012 8:58 PM

All replies

  • Hi,

    You're using CreateTextFile instead of OpenTextFile.

    Bill

    Tuesday, July 10, 2012 8:08 PM
  • To read or write you need to close and reopen the file.  The FileSystemObject has no way of repositining in a file.  It is forward only.  You can open and write to teh end or open and read from teh beginning to teh end.  There are no other things you can do.  To edit a file you would open and read thie input and write to a file while searching the input for items to cnage.

    Basically what you have there is all you ae going to get.

    Why?  What is not working?


    ¯\_(ツ)_/¯

    Tuesday, July 10, 2012 8:10 PM
  • You've posted to the VB.Net forum.  Moving to Offical Scripting Guys forum...


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, July 10, 2012 8:42 PM
  • Solution, perhaps? I tried to do what AbqBill said, but that just makes more headaches than before. Let me explain what the script is doing and what it should do.

    This script is created to send a custom or randomly generated reference ID (Ref_ID) email to a user specified email address. It should log all ref IDs and co-joining email addresses for future use in a file know as EmailResponder.ref (*.ref has been associated with notepad).

    The contents of EmailResponder.ref should look as follow:

    (Ln. 1) Reference: xxxxxx/xxx/2012; Email: xxxx@xxxx.xx.xx

    (Ln. 2)=======================================

    AND THEN WHEN THE SCRIPT IS BEING RUN AGAIN, IT SHOULD ADD (Ln. 3 and 4), NOT replace (Ln. 1 and 2) *being frustrated over here*

    Tuesday, July 10, 2012 8:49 PM
  • Ok - you are trying to work with a very hard to manage design.  A database qqould have been easier.  That said.

    1. OPen file and read to end. 
    2. Get last line number. 
    3. CLose fiel
    4. OPen file in 'appemd' mode and add lines as needed.

    That is all.  No need to edit anything.  Just get the last line and continue.  It just requires  two opens.


    ¯\_(ツ)_/¯

    • Marked as answer by Johan Brink Tuesday, July 10, 2012 10:43 PM
    Tuesday, July 10, 2012 8:58 PM
  • By the way.  Your code is VBAand not VBScript so Bill will return this thread to the VB forum.

    The FSO is used frequently in VBA and VB becuse VB has no comprehensive file management methods.

    An easier way to do this would be to keep the last number in a separate file which would be opened and retrived and updated on each message.

    Access data bases are very easy to work with and can do the number generating automatically.  Access can protect agasinst number generation failure due to all kinds of issues.  Text files cannot.


    ¯\_(ツ)_/¯

    Tuesday, July 10, 2012 9:03 PM
  • There's loads of info in the Hey, Scripting Guy! Blog.

    http://blogs.technet.com/b/heyscriptingguy/archive/tags/text+files/

    The VB stuff is the older ones.


    Blog: http://scriptimus.wordpress.com/

    Tuesday, July 10, 2012 9:16 PM
  • The code as posted always create a new log evey time it is run.  You need to open teh file and not create it.

    Start here: http://msdn.microsoft.com/en-us/library/aa265347(v=vs.60).aspx


    ¯\_(ツ)_/¯

    Tuesday, July 10, 2012 9:18 PM
  • You're using CreateTextFile instead of OpenTextFile. It would be good to familiarize yourself with these methods of the FileSystemObject object so you know which method is appropriate for what you want to do. Click on the links to read the documentation.

    Bill

    Tuesday, July 10, 2012 9:48 PM
  • Thanks you guys!

    the KB's helped a lot!

    It works!

    Tuesday, July 10, 2012 10:44 PM
  • Or not... Error received: Run-time error '5': invalid procedure call or argument.  New code is as follow: (Note, I have very limited knowledge of scripting and database integration... Don't judge me! Still learning!) ALSO note that the error is being called for Lines 44 and 69, which is [Set f = fs.OpenTextFile("c:\EmailResponder.ref", ForAppending, TristateFalse)]. Help????????

    '==========================================================================
    '
    ' VBScript Source File -- Created with Microsoft Visual Basic for Applications
    '
    ' NAME: Auto_Ref_Email
    '
    ' AUTHOR: Johan Brink, Pandora Information Systems
    ' DATE  : 10/07/2012
    '
    ' COMMENT: No_Reply Response email including Reference number for follow-up.
    '
    '   Namespace: http://msdn.microsoft.com/en-us/library/ms872853(EXCHG.65).aspx
    '
    '==========================================================================
    
    Sub Auto_Ref_Email()
        Const ForReading = 1, ForWriting = 2, ForAppending = 3, TristateFalse = 0, File_name = "C:\EmailResponder.ref"
        Dim fs, f
        Dim msg As Outlook.MailItem
        Set msg = Application.CreateItem(olMailItem)
        Dim filesys, filetxt, getname, path
        Ref_Custom = MsgBox("Would you like to enter a custom Ref_ID?", vbYesNo, "Desicion Time")
        If Ref_Custom = vbYes Then
            Ref_ID = InputBox("Please Enter Custom Ref ID", "Input Required")
            confirm_ref_id = MsgBox("You have entered [" & Ref_ID & "] as a Ref ID. Is this okay?", vbYesNo, "Action Required")
            Do Until confirm_ref_id = vbYes
                Ref_ID = InputBox("Please Enter Custom Ref ID", "Input Required")
                confirm_ref_id = MsgBox("You have entered [" & Ref_ID & "] as a Ref ID. Is this okay?", vbYesNo, "Action Required")
            Loop
            Send_Email = InputBox("Please Enter Email Address")
            confirm_send_email = MsgBox("You have entered [" & Send_Email & "] as an Email Address. Is this okay?", vbYesNo, "Action Required")
            Do Until confirm_send_email = vbYes
                Send_Email = InputBox("Please Enter Email Address")
                confirm_send_email = MsgBox("You have entered [" & Send_Email & "] as an Email Address. Is this okay?", vbYesNo, "Action Required")
            Loop
            msg.To = Send_Email
            msg.Subject = "Automatic Reply: Reference ID - " & Ref_ID
            msg.HTMLBody = "Thank you for contacting Pandora Information Systems. Your communication is valuable to us! We will reply shortly. Your automated reference number is: " & Ref_ID & ". Thank you for your kind support."
            msg.Display
            msg.Send
            Set msg = Nothing
            '   Create Log of Reference Numbers
            Set fs = CreateObject("Scripting.FileSystemObject")
            Set f = fs.OpenTextFile("c:\EmailResponder.ref", ForAppending, TristateFalse)
            f.Write "Reference: " & Ref_ID_Parse & "; Email: " & Send_Email & "; Date: " & Date
            f.Write " /& NEXT-"
            f.Close
        Else
            Ref_ID_part1 = Int((99999 * Rnd) + 1)
            Ref_ID_part2 = Int((999 * Rnd) + 1)
            Ref_ID_part3 = Year(Date)
            '   Combining all three into one variable
            Ref_ID_Parse = Ref_ID_part1 & "/" & Ref_ID_part2 & "/" & Ref_ID_part3
            final_ref_id = MsgBox("Random Ref ID is: [" & Ref_ID_Parse & "]", vbInformation, "Information")
            Send_Email = InputBox("Please Enter Email Address")
            confirm_send_email = MsgBox("You have entered [" & Send_Email & "] as an Email Address. Is this okay?", vbYesNo, "Action Required")
            Do Until confirm_send_email = vbYes
                Send_Email = InputBox("Please Enter Email Address")
                confirm_send_email = MsgBox("You have entered [" & Send_Email & "] as an Email Address. Is this okay?", vbYesNo, "Action Required")
            Loop
            msg.To = Send_Email
            msg.Subject = "Automatic Reply: Reference ID - " & Ref_ID_Parse
            msg.HTMLBody = "Thank you for contacting Pandora Information Systems. Your communication is valuable to us! We will reply shortly. Your automated reference number is: " & Ref_ID_Parse & ". Thank you for your kind support."
            msg.Display
            msg.Send
            Set msg = Nothing
            '   Create Log of Reference Numbers
            Set fs = CreateObject("Scripting.FileSystemObject")
            Set f = fs.OpenTextFile("c:\EmailResponder.ref", ForAppending, TristateFalse)
            f.Write "Reference: " & Ref_ID_Parse & "; Email: " & Send_Email & "; Date: " & Date
            f.Write " /& NEXT-"
            f.Close
        End If
    End Sub
    


    Tuesday, July 10, 2012 11:09 PM
  • Yes- it is clear that you are not a coder or scripter.

    You are asking for a solution.  We can give you assistance if you are a professional looking to become adept at scripting.

    Unfortualetly the code you have posted is not VBScript.  It is VBA.  You will need to post in an Office/Outlook VBA forume.


    ¯\_(ツ)_/¯

    Wednesday, July 11, 2012 12:38 AM