Generate a Sound When Saving a File

User avatar
sobershea
2StarLounger
Posts: 184
Joined: 08 Feb 2010, 23:37
Location: Howell, Michigan USA

Generate a Sound When Saving a File

Post by sobershea »

I found the following code online and it plays a sound when any Word doc is opened. I put the code in VBA under Normal This Document

Code: Select all

Private Declare PtrSafe Function PlaySound Lib "winmm.dll" _
  Alias "PlaySoundA" (ByVal IpszName As String, _
  ByVal hModule As Long, ByVal dsFlags As Long) As Long  'I only half understand the declaration

Private Sub Document_Open()
    PlaySound "c:\windows\media\alarm03.wav", ByVal0&, &H1  'And I don't understand the ByVal0&, &H1
End Sub
What I would like now is a routine that makes a sound every time I save any file. I assume it would require the same declaration as above and be located in the same place. I have played around with some code

Code: Select all

Private Sub Document_Save() 
    IF ActiveDocument.Save Then
PlaySound "c:\windows\media\alarm03.wav", ByVal0&, &H1
EndIF

End Sub
But I know I'm missing something.
TIA

:scratch:
Sherry

User avatar
HansV
Administrator
Posts: 78633
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Generate a Sound When Saving a File

Post by HansV »

A document does not have a Save event. You could do the following in the Visual Basic Editor:

1) Select your Normal template.

2) Select Insert > Class Module.
Name the class module clsMyClass.
Copy the following code into the class module:

Code: Select all

Private WithEvents app As Application

Private Sub app_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
    Beep
End Sub

Private Sub Class_Initialize()
    Set app = Application
End Sub

Private Sub Class_Terminate()
    Set app = Nothing
End Sub
3) Select Insert > Module
Copy the following line into this module:

Code: Select all

Public MyClass As New clsMyClass
4) Quit and restart Word.
Best wishes,
Hans

User avatar
sobershea
2StarLounger
Posts: 184
Joined: 08 Feb 2010, 23:37
Location: Howell, Michigan USA

Re: Generate a Sound When Saving a File

Post by sobershea »

Thank you, Hans. When I save a file I don't get a Beep. Don't I have to reference one of the sound files in Windows Media?
Sherry

User avatar
HansV
Administrator
Posts: 78633
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Generate a Sound When Saving a File

Post by HansV »

The code that I posted uses the Default Beep set in the Sounds control panel. If you have disabled the Default Beep, you'd get no sound.
You can replace Beep with

PlaySound "c:\windows\media\alarm03.wav", ByVal0&, &H1

Don't forget to copy/paste the Private Declare lines at the top of the class module.
Best wishes,
Hans

User avatar
sobershea
2StarLounger
Posts: 184
Joined: 08 Feb 2010, 23:37
Location: Howell, Michigan USA

Re: Generate a Sound When Saving a File

Post by sobershea »

This is what I have, and still no sound.
In Class Modules I created the following named clsMyClass

Code: Select all

Private Declare PtrSafe Function PlaySound Lib "winmm.dll" _
  Alias "PlaySoundA" (ByVal IpszName As String, _
  ByVal hModule As Long, ByVal dsFlags As Long) As Long

Private WithEvents app As Application

Private Sub app_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
    PlaySound "c:\windows\media\alarm04.wav", ByVal0&, &H0
End Sub

Private Sub Class_Initialize()
    Set app = Application
End Sub

Private Sub Class_Terminate()
    Set app = Nothing
End Sub
Plus I have Module1

Code: Select all

Public MyClass As New clsMyClass
:scratch:
Sherry

User avatar
HansV
Administrator
Posts: 78633
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Generate a Sound When Saving a File

Post by HansV »

It worked when I tested it. I'm afraid I don't know why it doesn't work for you :sad:
Best wishes,
Hans

User avatar
sobershea
2StarLounger
Posts: 184
Joined: 08 Feb 2010, 23:37
Location: Howell, Michigan USA

Re: Generate a Sound When Saving a File

Post by sobershea »

Thanks for trying, Hans. :cheers:
Sherry