Automatic Sound Clips

Written by Allen Wyatt (last updated April 1, 2023)
This tip applies to Word 97, 2000, 2002, and 2003


Word documents can contain quite a bit more than text. You can also include graphics and multimedia objects, such as sound clips. The normal way that you play a sound clip is to double-click on its icon within the document. What if you want the sound clip to start playing when you first open the document, without the need to double-click?

The best way to approach this problem is through the use of a macro. You can easily create a macro that automatically runs when you open a document, and the macro can play the sound file. The following is a very simple macro that does just that:

Private Sub Document_Open()
    ThisDocument.InlineShapes(1).Select
    Selection.InlineShapes(1).OLEFormat.DoVerb VerbIndex:=wdOLEVerbPrimary
End Sub

The macro assumes that the sound clip is the very first object that was inserted, inline, in your document. If not, you will need to figure out the index number for the clip, within the InlineShapes collection, and use that index value in the macro.

The heart of the macro uses the DoVerb method, which is a generic way of executing different actions on an OLE object (in this case, the sound file). The VerbIndex parameter determines the action that is executed. In this usage, VerbIndex is set equal to wdOLEVerbPrimary, which means "the primary action for an OLE object of this type." Since this is a sound file, the primary action is to play it.

Perhaps a better approach is to use the bookmark capabilities of Word to your advantage. You can use a bookmark to identify the sound clip you want to play, and then use that information in the macro to determine what is played. Consider the following macro:

Private Sub Document_Open()
    Selection.GoTo What:=wdGoToBookmark, Name:="WavSound"
    Selection.InlineShapes(1).OLEFormat.DoVerb VerbIndex:=wdOLEVerbPrimary
End Sub

This macro also assumes that the sound clip is placed inline in your document. However, the clip should be bookmarked using the name WavSound. The macro selects that bookmark (the sound clip) and plays it using the DoVerb method.

These approaches work fine if you want to play a sound clip that is a part of your document. You may want to play a sound clip that is not a part of the document. In that way, the sound clip's icon doesn't appear in the document and mess up the appearance of the document.

The way you accomplish this is to ask the Windows API to play the sound for you. This method works when you want to access a sound file you know is already on the computer, as long as the system also has the Windows Media Player installed. The following example should work on a Windows XP system. (The macro may or may not work on a Vista system, depending on how the system is configured.)

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

Private Sub Document_Open()
    PlaySound "c:\windows\media\tada.wav", ByVal0&, &H1
End Sub

The Document_Open macro runs when the document is opened, but it calls the PlaySound function. This function is defined in the private declaration as an alias for the PlaySoundA method of the winmm.dll. This method doesn't launch the media player itself, and it doesn't insert anything into the body of the document.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (3902) applies to Microsoft Word 97, 2000, 2002, and 2003.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Moving Section Breaks

Section breaks are used to divide a document into two or more sections that can be independently formatted. If you want ...

Discover More

Saving Search and Replace Information in a Macro

You may want to save a user's existing Find and Replace settings before changing them in your macro. This tip examines ...

Discover More

Removing the Last Digit in a Number

If you have a long numeric value in a cell, you may have a need to remove the last digit of that value. You can do so ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 365 applications with VBA programming. Written in clear terms and understandable language, the book includes systematic tutorials and contains both intermediate and advanced content for experienced VB developers. Designed to be comprehensive, the book addresses not just one Office application, but the entire Office suite. Check out Mastering VBA for Microsoft Office 365 today!

More WordTips (menu)

Changing an AutoShape

Got an AutoShape you previously added to a document, buy you now want to change to a different shape? You can change ...

Discover More

Changing Label Printing Order

If you want to change the order in which labels are printed when doing a mail merge, Word doesn't provide many options. ...

Discover More

Printing a Discussion

There are times you may have to work offline. Here is how to print out your discussion.

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 + 9?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.