Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

start sound on opening word doc

Status
Not open for further replies.

printerprobs

Programmer
May 3, 2005
9
AU
Hi I have a Wav music file on my word document. I would like this music to play automatically when a user opens the document. Is this possible at all in VBA/ macros??

If this is not possible, can i play the music by clicking on button or picture perhaps ?

Thanks in Advance.
 
Hi,

Add a Windows API declaration at the top of your document's code module:
Declare Function sndPlaySound32 Lib "winmm.dll" Alias "sndPlaySoundA" _
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Then call the function, passing it the name of the WAV file you want to play from an AutoOpen sub. For example:
Sub AutoOpen()
Call sndPlaySound32("C:\WIN2K\Media\Microsoft Office 2000\APPLAUSE.WAV", 0)
End Sub
The 0 at the end pauses macro execution while the sound is played. Change it to 1 to let this and other macros continue processing while the sound is played.

Cheers
 
Hi Thanks a lot for the reply. It works great. But i failed to mention that the file will be opened on different machines where the sound file cannot be accessed. Is it possible anyway to attach the file to the application? Thanks
 
Yes. Embed the sound object - rather than linking it.

You do not need to use the Windows API posted.

Assuming it is the FIRST InlineShape - that is, the index = 1, then put the following into the document ThisDocument module will work on any machine. This will make the sound file - if it is InlineShape(1) - play on Document_Open.
Code:
Private Sub Document_Open()
ActiveDocument.InlineShapes(1).OLEFormat.DoVerb VerbIndex:=wdOLEVerbPrimary
End Sub
This looks at the primary Verb of the object, which in this case IS to play the sound file, and actions it.

Notes:
1. When you embed the sound file it does show up as an object in the document. Select it and shrink it down as small as you want.

2. The sound file will play in whatever is you default sound application.

3. Once the call to the player application is fired you may delete the sound file from the document. This does NOT affect the playing, as the file has in fact already been passed. This may (or may not) be useful. In other words, it is possible to have a document fire a sound file, play it, and while playing it immediately delete that sound from itself. A one-time play.

Gerry
 
I do want to point out that if you are embedding WAV files....yikes, you could get some honking big files being sent.

If this is actually a viable thing to do, I would seriously considering getting a conversion utility and make them MP3. Even then, depending on the length of the file, they can add up.

Gerry
 
Thanks a heaps, macropod and fumei. That works for me. Great!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top