+ Reply to Thread
Results 1 to 3 of 3

How to play an embedded WAV file on another worksheet

Hybrid View

  1. #1
    Registered User
    Join Date
    09-16-2013
    Location
    Belgium
    MS-Off Ver
    Excel 2010
    Posts
    16

    How to play an embedded WAV file on another worksheet

    It is possible to send commands to ActiveX objects on other worksheets very easily, such as:

    Worksheets("test").OLEObjects("TextBox1").Object.Value = ""

    where "test" is the other worksheet name.

    Is it possible to do the same if the object is an embedded WAV file?

    To play an embedded WAV file on the same worksheet, the code might look like this:


    ActiveSheet.Shapes.Range(Array("2")).Select
                   Selection.Verb Verb:=xlOpen

    where "2" is the name of the target file.

    Is it possible to modify this to replace 'ActiveSheet' with the name of another worksheet?

    I've tried all the expected permutations without success - your help would be greatly appreciated.

    Moderator Note:

    Pls use code tags around your code next time as per forum rules.
    Last edited by Fotis1991; 09-19-2013 at 06:20 AM.

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: How to play an embedded WAV file on another worksheet

    I'd guess you would need to do something like:
    Sub macro_2()
    Dim wavfil As OLEObject
    Set wavfil = Sheets("Sheet1").OLEObjects(1)
    wavfil.Verb Verb:=xlPrimary
    End Sub
    Rather than the shapes version but I think that the .verb method will select the sheet the object is on anyway if you run that code while another worksheet is selected. So you may as well just change the sheet in the code.

    An alternative, if you know what the target file is, would be to just play the .wav file from vba. Something like:
    Option Explicit
    Public Declare Function sndPlaySound32 _
        Lib "winmm.dll" _
        Alias "sndPlaySoundA" ( _
            ByVal lpszSoundName As String, _
            ByVal uFlags As Long) As Long
    
    Sub PlayTheSound(ByVal WhatSound As String)
        If Dir(WhatSound, vbNormal) = "" Then
            ' WhatSound is not a file. Get the file named by
            ' WhatSound from the Windows\Media directory.
            WhatSound = Environ("SystemRoot") & "\Media\" & WhatSound
            If InStr(1, WhatSound, ".") = 0 Then
                ' if WhatSound does not have a .wav extension,
                ' add one.
                WhatSound = WhatSound & ".wav"
            End If
            If Dir(WhatSound, vbNormal) = vbNullString Then
                Beep            ' Can't find the file. Do a simple Beep.
                Exit Sub
            End If
        Else
            ' WhatSound is a file. Use it.
        End If
    
        sndPlaySound32 WhatSound, 0&    ' Finally, play the sound.
    End Sub

  3. #3
    Registered User
    Join Date
    09-16-2013
    Location
    Belgium
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: How to play an embedded WAV file on another worksheet

    After the addition of some inverted commas, here is the working code:

    
    Private Sub CommandButton4_Click()
    
    
    Dim wavfil As OLEObject
    Set wavfil = Worksheets("2").OLEObjects("4")
    wavfil.Verb Verb:=xlPrimary
    
    End Sub
    This works perfectly. Thank you so much yudlugar, this has been a long-standing problem.
    Last edited by Leith Ross; 09-19-2013 at 05:37 PM. Reason: Corrected Code Tags

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Error in code - play music file automatically after opening file
    By mukeshbaviskar in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-15-2013, 10:06 AM
  2. Replies: 0
    Last Post: 01-23-2013, 05:22 PM
  3. Trying to embed & play an mp3 file with buttons for play/pause stop & mute
    By jagerman18 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-03-2012, 02:50 AM
  4. Can you auto-trigger a sound file to play when a worksheet opens?
    By Nanotexan in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-05-2006, 01:20 PM
  5. VBA to play wav file
    By newintown in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-08-2005, 12:23 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1