none
stop word from closing RRS feed

  • Question

  • Hi,

    i am trying to write a code for my students in my class for apart of there assignment template but i am not to sure what i have to do next could some one please give me a hand

    what i have so far is a word document with this in it
    "I, Student Name, declare that this assignment is my own work, excepting content that I have clearly attributed to others."

    now what i am trying to do is when the student closes it it comes up with a input box saying please enter your name but the students can still press cancel and get out of it

    how do i make it so they cant get out of the document unless they enter there name

    this is the code i have

    Private Sub Document_Close()
    Dim rng As Range
    Set rng = ThisDocument.Content
        rng.Find.Execute "student name", False, , , , , , , , InputBox("Enter Your Name"), wdReplaceAll
        ThisDocument.Save
    End Sub

    Tuesday, May 29, 2012 5:41 AM

All replies

  • Try something like;

    Dim sname As String
    
    While Len(Trim(sName)) = 0
        sName = Trim(InputBox("Enter your name"))
    Wend
    
    'Remainder of code
    .
    .
    .
    

    Of course it would help if you could then look up the student's name in a table to ensure they have entered a valid name.

    Tuesday, May 29, 2012 6:01 AM
  • ok thanks but i am not sure i did it right

    this is what i have

    Private Sub Document_Close()
    Dim sname As String
    While Len(Trim(sname)) = 0
        sname = Trim(InputBox("Enter your name"))
    Wend
    Dim rng As Range
    Set rng = ThisDocument.Content
        rng.Find.Execute "student name", False, , , , , , , , InputBox("Enter Your Name"), wdReplaceAll
        ThisDocument.Save
    End Sub
    

    and its not letting me cancel which is good but when i type the name in it doesnt put it in the document and say for example i enter steve johnson after i enter it, it asks for it again but still nothing in the document

    Tuesday, May 29, 2012 6:07 AM
  • The reason your code doesn't work is because

    rng.Find.Execute "student name", False, , , , , , , , InputBox("Enter Your Name"), wdReplaceAll

    is searching for student name in the document which isn't there so the student is asked yet again to enter a name

    After you obtain a value for sname, write it to the document immediately.

    You don't need the other stuff.


    • Edited by ieee488 Tuesday, May 29, 2012 1:27 PM
    Tuesday, May 29, 2012 1:27 PM
  • It isn't because "student name" wasn't found in the document that you are being prompted to enter the name again, it is because you are using the InputBox function again in the "Find.Execute" line.

    The following code will replace instances of "student name" in you document with the name they enter from the InputBox;

    Private Sub Document_Close()
        Dim sName As String
        Dim rng As Range
        
        While Len(Trim(sName)) = 0
            sName = Trim(InputBox("Enter your name"))
        Wend
        
        Set rng = ThisDocument.Content
        rng.Find.Execute "student name", False, , , , , , , , sName, wdReplaceAll
        ThisDocument.Save
    End Sub
    

    The While...Wend loop is used to ensure they enter a value for student name - it will just keep prompting until they enter something.  the Find.Execute then uses this value to replace all instances of "student name" in the document.

    It is common practice however in these scenarios to ensure the value to be replaced is not something that could normally appear on the text so I would strongly recommend that you use "<<StudentName>>" instead of "student name". Your code would then be;

    Private Sub Document_Close()
        Dim sName As String
        Dim rng As Range
        
        While Len(Trim(sName)) = 0
            sName = Trim(InputBox("Enter your name"))
        Wend
        
        Set rng = ThisDocument.Content
        rng.Find.Execute "<<StudentName>>", False, , , , , , , , sName, wdReplaceAll
        ThisDocument.Save
    End Sub
    

    Tuesday, May 29, 2012 10:27 PM
  • It isn't because "student name" wasn't found in the document that you are being prompted to enter the name again, it is because you are using the InputBox function again in the "Find.Execute" line.

    The following code will replace instances of "student name" in you document with the name they enter from the InputBox;

    Private Sub Document_Close()
        Dim sName As String
        Dim rng As Range
        
        While Len(Trim(sName)) = 0
            sName = Trim(InputBox("Enter your name"))
        Wend
        
        Set rng = ThisDocument.Content
        rng.Find.Execute "student name", False, , , , , , , , sName, wdReplaceAll
        ThisDocument.Save
    End Sub

    The While...Wend loop is used to ensure they enter a value for student name - it will just keep prompting until they enter something.  the Find.Execute then uses this value to replace all instances of "student name" in the document.

    It is common practice however in these scenarios to ensure the value to be replaced is not something that could normally appear on the text so I would strongly recommend that you use "<<StudentName>>" instead of "student name". Your code would then be;

    Private Sub Document_Close()
        Dim sName As String
        Dim rng As Range
        
        While Len(Trim(sName)) = 0
            sName = Trim(InputBox("Enter your name"))
        Wend
        
        Set rng = ThisDocument.Content
        rng.Find.Execute "<<StudentName>>", False, , , , , , , , sName, wdReplaceAll
        ThisDocument.Save
    End Sub

    ok thanks i tryed the second one and it is working but when i close the document it asks for my name (which is right) and then it closes but when i open it and try to close it agian it asks for my name again. how can i stop it doing that? and just close seeing as it has the persons name
    Wednesday, May 30, 2012 4:30 AM
  • The following code will check if "<<StudentName>>" exists when the document is closing.  If it does the the user will be prompted to enter their name.  If it doesn't then the user won't be prompted.

    Private Sub Document_Close()
        Dim sName As String
        Dim rng As Range
        
        Set rng = ThisDocument.Content
        
        If rng.Find.Execute("<<StudentName>>", True, True) Then
            While Len(Trim(sName)) = 0
                sName = Trim(InputBox("Enter your name"))
            Wend
            
            rng.Find.Execute "<<StudentName>>", False, , , , , , , , sName, wdReplaceAll
        End If
        
        ThisDocument.Save
    End Sub
    

    • Proposed as answer by Ray Brack Wednesday, May 30, 2012 10:10 PM
    Wednesday, May 30, 2012 4:47 AM