Hi, Hoping someone can help out on here. I am currently setting up a web page for work and have come across a problem. This is the script so far (writen on Notepad).
Sub B2_onClick on error resume next dim date1 dim option1 dim completelogon dim pagenumber dim username
Set objNet = CreateObject("WScript.NetWork") username = objNet.Username pagenumber="Department ID" date1 = FormatDateTime(Now(), vbGeneralDate) option1 = document.form_Staffsurveylogon.menu.value completelogon = pagenumber & " ; " & option1 & " ; " & date1 strFileName = "K:\Network Enterprise\Planning & Resource\Project Management\Staff Survey 2009\" & username & ".txt" Set fso = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.CreateTextFile(strFileName) Set f = fso.OpenTextFile("K:\Network Enterprise\Planning & Resource\Project Management\Staff Survey 2009\" & username & ".txt") If not (f.AtEndOfStream) then ReadAllTextFile = f.ReadAll End if f.Close Set f = fso.OpenTextFile("K:\Network Enterprise\Planning & Resource\Project Management\Staff Survey 2009\" & username & ".txt", 2, True) f.Write(ReadAllTextFile) f.WriteLine completelogon window.location.href = "Page_1.htm" End sub
What I am after is when running this script I need the code to see if a txt file (by this user, as this creates a text folder for each user as its run) already exists and if so a messagebox would appear saying this and then stops them proceeding any further through the web pages. This would mean only one run through by each person. Thats the idea...... Any help would be greatly appreciated as I've been trying so many different things and nothing seems to work!!!!!
Thank you... That did not seem to work though... It somehow stopped the script from doing anything ( I have no idea why!!!) I must confess to being a complete novice on vbs so please excuse my ignorance.
Could you please suggest a code that will search for the txt file (titled by the username) and if it exists to then state a message which when clicked would close the window (without requesting permission from the user). This would stop them from proceeding any further( which is my aim).
I am not sure if I fully understand what you are needing but maybe this will help. I cleaned yours up a bit.
Hope this helps get you started.
Sub B2_onClick on error resume next dim date1 dim option1 dim completelogon dim pagenumber dim username dim fso, f dim ReadAllTextFile dim strFileName dim objNet const ForWriting = 2 const ForReadOnly = 1
Set objNet = CreateObject("WScript.NetWork") username = objNet.Username pagenumber="Department ID" date1 = FormatDateTime(Now(), vbGeneralDate) option1 = document.form_Staffsurveylogon.menu.value completelogon = pagenumber & " ; " & option1 & " ; " & date1 strFileName = "K:\Network Enterprise\Planning & Resource\Project Management\Staff Survey " & _ "2009\" & username & ".txt" Set fso = CreateObject("Scripting.FileSystemObject") if not fso.fileExists(strFileName) then Set f = fso.OpenTextFile(strFileName, ForWriting, true) f.WriteLine completelogon else Set f = fso.openTextFile(strFileName, ForReadOnly, true) ReadAllTextFile = f.ReadAll msgbox(ReadAllTextFile) end if f.Close window.location.href = "Page_1.htm" End sub
Not really what I am after....I simply need a code that will see if a text file exists (under the current user) and if so to state a message ("Thank you. You have already completed the Staff Survey") and by clicking ok to close the window(without requesting permission from the user to do so). If the text file does not exist then to carry on...
Hope someone can help as I've tried several options and nothing seems to work.
The below should work. It will check for the existance of the file, if it exists, it will give you a message box and then exit If statement without processing. If the file does not exist, then it continues on.
Not sure why you are reading all the info from the doc into a variable, closing the file, reopening the file, rewriting the data into the same file along with one new line. Well...I should say I understand why you did it....ForWriting(2) would overwrite all info in the doc. You should just use "ForAppending(8)" and that would just write a new line on the first blank line in the doc. Save you some time and code.
< Message edited by chiltz -- 8/28/2008 12:40:08 AM >
Thank you... you've cracked it!!!! Strange but I tried this form of coding before but it just did nothing.... I think it was the placement of the "End If" as i placed it too early before.
I have removed the reading section on this part(as you said. It seems pointless!!!). I copied this part of coding from a colleagues work (who has now left) and am learning as I go. Having great fun... with a bit of frustration thrown in for good measure!!!!