Consulting

Results 1 to 9 of 9

Thread: Document Close

  1. #1
    VBAX Newbie
    Joined
    Nov 2009
    Posts
    5
    Location

    Document Close

    I found this code on the internet:
    [VBA]
    Sub FileClose()

    NewCloseDialog

    End Sub

    Sub DocClose()

    NewCloseDialog


    End Sub

    Sub NewCloseDialog()

    On Error GoTo Error

    If ActiveDocument.Saved = False Then
    SureCloseDialog.Show

    Else:

    ActiveDocument.Close

    End If

    Exit Sub

    Error:

    MsgBox ("Something went wrong.")

    End Sub

    [/VBA]

    This is really handy. But it only works when you click on: Office-Button>Close. How can I add a function to it that it always happens when you try to close the file????
    P.S. I am a noob, so I need well explanation... Not: Oh, you need to add an close.Dialog If function on the third acudelir..

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Is this for MS Word?

    Did you look at the document close procedure in the thisdocument module?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Newbie
    Joined
    Nov 2009
    Posts
    5
    Location
    Yes this is made for MS Word 2003, and used (by me) for MS Word 2007.

    And no, I did not look at the document close procedure in the thisdocument module, because I do not know what that is....

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    In the vbe look for the thisdocument module in the project explorer and put your code in the document close procedure.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    VBAX Newbie
    Joined
    Nov 2009
    Posts
    5
    Location
    ok.........
    which code???????????
    where then exactly????

  6. #6
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    You know how to find the visual basic editor or vbe

    a shortcut key combination to get you there is to hit the alt key and f11.

    On the left side of the vbe look for your project explorer. If it's not visible go to view and select project explorer.

    In the project explorer look for the line that says thisdocument. Doulbe click it to see the code module on the right....

    there are two drop downs at the top of the module. Start with the one on the left and select document from the drop down.

    Then on the right drop down select "close"

    that is where you put the code you want to run when your document is closed.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  7. #7
    VBAX Newbie
    Joined
    Nov 2009
    Posts
    5
    Location
    Yes but this doesnt work when you press AltF4 or [X]...

  8. #8
    VBAX Newbie
    Joined
    Nov 2009
    Posts
    5
    Location
    (sorry for double post)
    It worked!
    Really thanks!

  9. #9
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    You're welcome. Be sure to mark your thread solved using the thread tools at the top of the page.

    You will still be able to post followup questions on this thread.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •