PDA

View Full Version : Play Sound on Task Completion



blewett
07-03-2012, 11:40 AM
I am trying to play a sound when a task is marked as completed.

I've based my code on this post: vbaexpress.com/forum/showthread.php?t=8117 and this KB article: vbaexpress.com/kb/getarticle.php?kb_id=461 But I've just not been able to get it to work.

It seems to me as if I'd want to use the Write or AfterWrite events instead of PropertyChange, but I can't get anything to work.

JP2112
07-09-2012, 12:17 PM
Have you tried configuring the sounds for Windows events?

It varies by operating system, but you usually go to the Control Panel (in Windows) and find the "Sounds" icon.

blewett
07-09-2012, 03:23 PM
Have you tried configuring the sounds for Windows events?

It varies by operating system, but you usually go to the Control Panel (in Windows) and find the "Sounds" icon. Thanks for the suggestion JP, but I did not find anything in the sound properties that would be triggered when an Outlook task was marked as completed.

Re-reading my initial post I realized I did a poor job laying out what I was trying to do. Sorry about that. My code is, frankly, such a mess and I've restarted so many times I doubt pasting it is helpful, But here it is anyways. This is in ThisOutlookSession.


Dim myClass As cTaskEvents
Dim colTasks As New Collection
Public CompletedTaskID As String

Sub Init_Event_Handler()

Dim ns As NameSpace
Dim oFolder As MAPIFolder
Dim t As TaskItem Dim i As Long

Set ns = Application.GetNamespace("MAPI")
Set oFolder = ns.GetDefaultFolder(olFolderTasks)
'clear the collection
Do
i = colTasks.Count
If colTasks.Count > 0 Then colTasks.Remove colTasks.Count
Loop Until colTasks.Count = 0
CompletedTaskID = ""

For Each t In oFolder.Items
Set myClass = New cTaskEvents 'new class instance
myClass.Init t 'pass the item to the class' Init routine
colTasks.Add myClass 'add instance to collection
Next t

End Sub

Sub TaskCompleteSound()

WhatsNextAudio

End Sub



Private WithEvents m_Task As TaskItem

Public Sub Init(t As TaskItem)
'!requires a task item passed as an argument!
Set m_Task = t
End Sub

Private Sub m_Task_PropertyChange(ByVal Name As String)

If Name = "Complete" Then
If m_Task.Complete Then
If CompletedTaskID = "" Then
TaskCompleteSound
CompletedTaskID = m_Task.EntryID
Else
CompletedTaskID = ""
End If
End If
End If

End Sub

Private Sub Class_Terminate()
Set m_Task = Nothing
End Sub


I've got a macro called WhatsNextAudio that is set to play the specific file path:


Option Explicit


'API Class to take care of playing the file
Public Declare Function sndPlaySound32 Lib "winmm.dll" _
Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long

Sub WhatsNextAudio()
Call sndPlaySound32("\whatsnext.wav", 0) 'I removed the full path for this posting
End Sub



I've got no sound and all sorts of compile errors.