- 帖子
- 109
- 主題
- 2
- 精華
- 0
- 積分
- 114
- 點名
- 0
- 作業系統
- Win7 Win10
- 軟體版本
- Office 2019 WPS
- 閱讀權限
- 20
- 性別
- 男
- 來自
- 深圳
- 註冊時間
- 2013-2-2
- 最後登錄
- 2024-11-6
|
3#
發表於 2016-1-20 12:53
| 只看該作者
回復 1# lshsien
添加一個新的模塊,將下面的代碼粘貼到此模塊中:- Option Explicit
- #If VBA7 Then
- Private Declare PtrSafe Function waveOutGetNumDevs Lib "winmm.dll" () As Long
- Private Declare PtrSafe Function mciSendString Lib "winmm.dll" Alias "mciSendStringW" (ByVal lpstrCommand As LongPtr, ByVal lpstrRetumString As LongPtr, ByVal uReturnLength As Long, ByVal hwndCallback As LongPtr) As Long
- Private Declare PtrSafe Function GetShortPathNameW Lib "kernel32" (ByVal lpszLongPath As LongPtr, ByVal lpszShortPath As LongPtr, ByVal cchBuffer As Long) As Long
- Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
- Private Declare PtrSafe Function PathFindExtension Lib "shlwapi.dll" Alias "PathFindExtensionW" (ByVal pszPath As LongPtr) As LongPtr
- Private Declare PtrSafe Function lStrLen Lib "kernel32.dll" Alias "lstrlenW" (ByVal lpString As LongPtr) As Long
- Private Declare PtrSafe Sub CopyMemoryByPtr Lib "kernel32.dll" Alias "RtlMoveMemory" (ByVal Destination As LongPtr, ByVal Source As LongPtr, ByVal Length As Long)
- #Else
- Private Declare Function waveOutGetNumDevs Lib "winmm.dll" () As Long
- Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringW" (ByVal lpstrCommand As Long, ByVal lpstrRetumString As Long, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
- Private Declare Function GetShortPathNameW Lib "kernel32" (ByVal lpszLongPath As Long, ByVal lpszShortPath As Long, ByVal cchBuffer As Long) As Long
- Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
- Private Declare Function PathFindExtension Lib "shlwapi.dll" Alias "PathFindExtensionW" (ByVal pszPath As Long) As Long
- Private Declare Function lStrLen Lib "kernel32.dll" Alias "lstrlenW" (ByVal lpString As Long) As Long
- Private Declare Sub CopyMemoryByPtr Lib "kernel32.dll" Alias "RtlMoveMemory" (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)
- #End If
- Private Type FileList
- FileName As String
- ShortName As String
- End Type
- Private FileList() As FileList
- Private ListCount As Long
- Public Sub Clean()
- ListCount = 0
- Erase FileList
- End Sub
- Public Property Get Item(ByVal Index As Long) As String
- If Index > 0 And Index <= ListCount Then
- Item = FileList(Index).FileName
- End If
- End Property
- Public Property Let Item(ByVal Index As Long, ByVal FileName As String)
- Dim I As Long
- If Index = ListCount + 1 Then
- ListCount = ListCount + 1
- ReDim Preserve FileList(1 To ListCount)
- End If
- If (Index > 0) And (Index <= ListCount) Then
- With FileList(Index)
- .FileName = FileName
-
- I = Len(FileName)
- If I > 0 Then
- .ShortName = GetShortPathName(FileName)
- Else
- .ShortName = vbNullString
- End If
- End With
- End If
- End Property
- Public Function ItemAdd(ByVal FileName As String) As Long
- Dim I As Long
- I = ListCount + 1
- Item(I) = FileName
- ItemAdd = I
- End Function
- Public Function ItemReMove(ByVal Index As Long) As Boolean
- If Index > 0 And Index <= ListCount Then
- With FileList(Index)
- .FileName = vbNullString
- .ShortName = vbNullString
- End With
- End If
- End Function
- Public Sub StartPlay()
- Dim I As Long
-
- If waveOutGetNumDevs > 0 Then
- For I = 1 To ListCount Step 1
- With FileList(I)
- If Len(.ShortName) Then
- Select Case UCase$(ExtractFileExtension(.ShortName))
- Case ".WAV": mciSendString StrPtr("open " & .ShortName & " type waveaudio alias JofornMusic"), 0, 0, 0
- Case ".MDI": mciSendString StrPtr("open " & .ShortName & " type sequencer alias JofornMusic"), 0, 0, 0
- Case Else: mciSendString StrPtr("open " & .ShortName & " alias JofornMusic"), 0, 0, 0
- End Select
- mciSendString StrPtr("play JofornMusic FROM 0"), 0&, 0, 0
- Do While Not IsEnd
- Sleep 50
- DoEvents
- Loop
- mciSendString StrPtr("close JofornMusic"), 0&, 0, 0
- End If
- End With
- Next I
- End If
- End Sub
- Private Function IsEnd() As Boolean
- Dim I As Long
- Dim strStatus As String
-
- strStatus = String$(256, vbNullChar)
- mciSendString StrPtr("status JofornMusic mode"), StrPtr(strStatus), 256, 0
- I = InStr(strStatus, vbNullChar)
- If I > 1 Then
- IsEnd = UCase$(Left$(strStatus, I - 1)) = "STOPPED"
- Else
- IsEnd = True
- End If
- End Function
- '从路径提取文件后缀名
- Public Function ExtractFileExtension(ByVal strPath As String) As String
- #If VBA7 Then
- Dim ptrExt As LongPtr
- #Else
- Dim ptrExt As Long
- #End If
- Dim ExtLen As Long
-
- strPath = strPath & vbNullChar
- ptrExt = PathFindExtension(StrPtr(strPath))
- If ptrExt Then
- ExtLen = lStrLen(ptrExt)
- If ExtLen > 0 Then
- ExtractFileExtension = String(ExtLen, vbNullChar)
- CopyMemoryByPtr StrPtr(ExtractFileExtension), ptrExt, ExtLen * 2
- End If
- End If
- End Function
- Public Sub StopPlay()
- mciSendString StrPtr("close JofornMusic"), 0&, 0, 0
- End Sub
- Private Function GetShortPathName(ByVal FileName As String) As String
- Dim I As Long
- Dim strTemp As String
-
- strTemp = Space$(256)
- I = GetShortPathNameW(StrPtr(FileName), StrPtr(strTemp), 256)
- If I > 0 Then GetShortPathName = Left$(strTemp, I)
- End Function
複製代碼 將原有的代碼修改為:- Public Sub macro_test2()
- Dim Bk_A As String
- Bk_A = Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 5)
- '-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-
- Dim book_name As String
- Dim book_path As String
-
- book_name = Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 5)
- book_path = ThisWorkbook.Path
-
- Clear
- ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_各位旅客您好" & ".wav"
- ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_6點" & ".wav"
- ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_分50" & ".wav"
- ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_分" & ".wav"
- ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_開往" & ".wav"
- ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_台北的" & ".wav"
- ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_車次5" & ".wav"
- ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_車次0" & ".wav"
- ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_車次0" & ".wav"
- ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_次" & ".wav"
- ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_高鐵" & ".wav"
- ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_北上" & ".wav"
- ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_的列車即將進站請前往" & ".wav"
- ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_第二月台" & ".wav"
- ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_搭乘並留意月台間隙謝謝" & ".wav"
- StartPlay
- End Sub
複製代碼 完成上面的兩步操作後運行macro_test2測試。 |
|