|
要实现这个功能,你可以使用以下VBA代码:
1. 首先,在Excel中插入一个ActiveX控件(如"Microsoft Sound Control 6.0 (Spinner.Sound)"),并将其命名为"SoundControl"。
2. 然后,将以下代码复制到工作表的VBA编辑器中:
```vba
Option Explicit
Private Sub Workbook_Open()
Application.OnTime Now + TimeValue("00:00:01"), "CheckTimeAndPlaySound"
End Sub
Private Sub CheckTimeAndPlaySound()
Dim ws As Worksheet
Dim lastRow As Long
Dim currentTime As Date
Dim timeColumn As Range
Dim messageColumn As Range
Dim soundControl As Object
' 设置工作表和列
Set ws = ThisWorkbook.Worksheets("Sheet1") ' 将"Sheet1"替换为你的工作表名称
Set timeColumn = ws.Range("B1:B" & ws.Cells(ws.Rows.Count, "B").End(xlUp).Row)
Set messageColumn = ws.Range("A1:D" & ws.Cells(ws.Rows.Count, "A").End(xlUp).Row)
' 获取当前时间
currentTime = Now
' 遍历时间列,检查是否有匹配的时间
For Each cell In timeColumn
If cell.Value = currentTime Then
' 播放语音
Set soundControl = Me.SoundControl
soundControl.FileName = messageColumn.Cells(cell.Row, 1).Value & messageColumn.Cells(cell.Row, 3).Value & messageColumn.Cells(cell.Row, 4).Value
soundControl.Play
End If
Next cell
' 每隔1秒检查一次时间并播放语音
Application.OnTime Now + TimeValue("00:00:01"), "CheckTimeAndPlaySound"
End Sub
```
3. 最后,确保你的工作表中有正确的数据,然后保存并关闭VBA编辑器。现在,当表格中的B列时间与电脑时间相同时,将会自动播放语音。此外,即使当前工作簿最小化,该宏仍会在后台运行。 |
|