Results 1 to 6 of 6
  1. #1
    Lounger
    Join Date
    Mar 2007
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    DocumentBeforeClose (Word XP SP1)

    Hi everyone

    I have created a template in word and would like this to be emailed when the user closes it. On looking through the web I found an example of exactly what I wanted to do but cant get it to work. I have put the code below into a class module as advised but when the document is closing it is doing nothing. If anyone can give me a hand that would be great. the code is

    Public WithEvents appWord As Word.Application
    Dim X As New EventClassModule
    Sub Register_Event_Handler()
    Set X.appWord = Word.Application
    End Sub
    Private Sub appWord_DocumentBeforeClose()
    Options.SendMailAttach = True
    ActiveDocument.SendMail
    End Sub

    Thanks in advance

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: DocumentBeforeClose (Word XP SP1)

    The code you posted is slightly mixed up, but when untangled, it would e-mail *each* document when it is closed, regardless of the template it is based on. Is that what you want, or do you only want to e-mail documents based on your template? If so, the code would be easier.

  3. #3
    Lounger
    Join Date
    Mar 2007
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: DocumentBeforeClose (Word XP SP1)

    thanks for getting back to me Hans

    I want only to email those documents based on the template.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: DocumentBeforeClose (Word XP SP1)

    For that, you don't need a class module. Instead, put the following procedure in the ThisDocument module of the template:

    Private Sub Document_Close()
    Options.SendMailAttach = True
    ActiveDocument.SendMail
    End Sub

    I suspect that this could irritate users - each time they close the document, an e-mail pops up. You could ask the user first:

    Private Sub Document_Close()
    If MsgBox("Do you want to e-mail the document?", _
    vbQuestion + vbYesNo) = vbYes Then
    Options.SendMailAttach = True
    ActiveDocument.SendMail
    End If
    End Sub

    but then, users will see the message box each time they close the document. <img src=/S/crazy.gif border=0 alt=crazy width=15 height=15>

  5. #5
    Lounger
    Join Date
    Mar 2007
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: DocumentBeforeClose (Word XP SP1)

    Hans

    that worked perfectly thanks for your help. Just out of interest, how easy is it to pre-populate the address box with an email address?

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: DocumentBeforeClose (Word XP SP1)

    In theory, you should be able to use code like that below (it should be inserted above the SendMail line). But for some reason, it won't fill in the recipient when I try it...

    With ActiveDocument.MailEnvelope
    .Introduction = "You will find the monthly report attached."
    .Item.Subject = "Test e-mail"
    .Item.Recipients.Add "you@somewhere.com"
    End With

Posting Permissions

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