none
VBA Document Close Help Please RRS feed

  • Question

  • Hi,
    I have a word document that has in it the words "I, Student Name" and i
    am trying to write a code so that when the user closes the document a
    input box prompts them and asks the user to enter their name for i enter
    Bob Smith and it replaces Student name with Bit Smith and then closes
    but when i open the document again it has bob smith which is what i
    want. But when i go to close the document

    it asks for the name again. How can i make it so it doesnt ask for the
    name again when it already has the name of the person it in?

    the code i have is below here

    thanks in advanced

    P.S i am using Microsoft Word

    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



    Sunday, June 17, 2012 8:56 AM

All replies

  • You could use

    Private Sub Document_Close()
        Dim rng As Range
        Set rng = ThisDocument.Content
        If rng.Find.Execute("Student Name") Then
            rng.Text = InputBox("Enter Your Name")
        End If
        ThisDocument.Save
    End Sub


    Regards, Hans Vogelaar

    • Proposed as answer by Shasur Sunday, June 17, 2012 3:30 PM
    Sunday, June 17, 2012 9:02 AM
  • You could use

    Private Sub Document_Close()
        Dim rng As Range
        Set rng = ThisDocument.Content
        If rng.Find.Execute("Student Name") Then
            rng.Text = InputBox("Enter Your Name")
        End If
        ThisDocument.Save
    End Sub


    Regards, Hans Vogelaar

    Thanks you make it look so easy i have been trying to do that all day today looking online couldnt find any thing
    Sunday, June 17, 2012 9:10 AM
  • There was a recent similar posting http://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/9d0aff4e-fd17-4bd3-8056-151a13ae587a that may also help.

    For the benefit of others, please mark posts as answered or helpful when they answer or assist you in finding the answer. "Don't confuse fame with success. Paris Hilton is one; Helen Keller is the other." - with apologies to Erma Bombeck for changing her words.

    Wednesday, June 20, 2012 5:41 AM