i modified it a bit to work with VBA. now it works. except that line Set objWMP = CreateObject("WMPlayer.OCX") throws error of Run-time error 429, ActiveX componenet cannot create object
any idea how to fix this?
Option ExplicitDeclare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)Sub test()Const RemovableDisk = 2Const AlarmSound = "C:\Users\flora\Downloads\New folder\siren.wav"Const AlarmVolume = 100 ' 0 to 100' Global variablesDim objWMIDim colEventsDim objEvent' Create WMI objectSet objWMI = GetObject("winmgmts:\\.\root\cimv2")' Hook events related to Win32_LogicalDiskSet colEvents = objWMI.ExecNotificationQuery("Select * From __InstanceOperationEvent Within 1 Where TargetInstance isa 'Win32_LogicalDisk'")' Loop endlessly (ctrl-C to exit program from console)Do While True ' Get next event Set objEvent = colEvents.NextEvent ' Only interested in removable disk events If objEvent.TargetInstance.DriveType = RemovableDisk Then ' See what even this is and process if desired Select Case objEvent.Path_.Class ' Drive removed evemt is what we want Case "__InstanceDeletionEvent" ' Display message indicating drive removed and play alarm sound 'WScript.Echo MsgBox "Drive " & objEvent.TargetInstance.DeviceId & " has been removed." Call PlaySound(AlarmSound, AlarmVolume) End Select End IfLoopEnd Sub' Constants' Play a sound file using WMPLAYER objectSub PlaySound(strFile, intVolume) ' Local variables Dim objWMP Dim intSaveVolume ' Create player object Set objWMP = CreateObject("WMPlayer.OCX") ' Save current volume if we are using our own level If intVolume >= 0 And intVolume <= 100 Then intSaveVolume = objWMP.Settings.Volume objWMP.Settings.Volume = intVolume End If ' Set path to sound file and play it objWMP.URL = strFile objWMP.Controls.Play ' Wait for it to finish playing While objWMP.PlayState <> 1 ' Stopped Sleep 100 Wend ' Restore original player volume if we changed it If intVolume >= 0 And intVolume <= 100 Then objWMP.Settings.Volume = intSaveVolume End If ' Tear down player object objWMP.Close Set objWMP = NothingEnd Sub
Just curious, what's your motivation for running this from VBA versus stand alone?
~bp
Flora Edwards
ASKER
when i run your code in my prviate machine with admin rights, it worked, when i run it in my office machine with limited user. i get the following error when running the vbs. so in VBA env i do not face that. so instead i am using Excel to avoid the block of vbs.
Bill Prew
Okay, got it. I wonder if Powershell is locked down for you?
Also, you might search for a simple "Hello world" HTA example file online and grab that and see if you can run the HTA file without error. If so it would be easy to use that. I'd send you a test file but I'm on mobile at the moment.
~bp
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Flora Edwards
ASKER
thanks so much Bill.
yes, powershell is open and also i can run .hta files. i prefer hta over pwershell.
many thanks.
byundt
Does the following statement work in your VBA?
Dim objWMP As ObjectSet objWMP = CreateObject("new:{6BF52A52-394A-11d3-B153-00C04F79FAA6}")
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
Unlimited question asking, solutions, articles and more.
Bill Prew
My first suspicion would be that on your system WAV files are not associated with Windows Media Player. A few things to do. First you can make the WMP embded object visible in the window by the following.
You can also check the WAV file extension and see what is handling it via the ASSOC and FTYPE commands, here is what mine looks like.
[c:\]assoc .wav
.wav=WMP11.AssocFile.WAV
[c:\]ftype WMP11.AssocFile.WAV
WMP11.AssocFile.WAV="%ProgramFiles(x86)%\Windows Media Player\wmplayer.exe" /Open "%L"
Bill Prew
Make any progress on this?
~bp
Flora Edwards
ASKER
thank you very much Bill. you found the issue. my machine in domain envi did not have windows media player. your solution is perfect and it works.
again thanks sooooo much.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
~bp