VBA code to turn volume on, up, down

Kerry Newman

New Member
Joined
Feb 23, 2018
Messages
19
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I am trying to find some code to turn my speaker volume on, up or down.
I am using the Speak function if that has any relevance...... thanks!
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Do you get any errors ? Are you running a Mac or a PC ? if Windows , which version ?
 
Upvote 0
You have to call the relevant routine from another sub. For example (as Wellsr demonstrates (and by the way, he is a genius!)), to turn the volume up, use this code:

Code:
Sub Turn_Volume_UP

Call VolUp

End Sub

Add this to a Command Button, and your volume will increase. Do something similar for decreasing volume, and muting.

I'm using Office 365 32bit and Windows 10. His code works for me.

Have fun.

Cheers

pvr928
 
Last edited:
Upvote 0
You have to call the relevant routine from another sub. For example (as Wellsr demonstrates (and by the way, he is a genius!)), to turn the volume up, use this code:

Code:
Sub Turn_Volume_UP

Call VolUp

End Sub

Add this to a Command Button, and your volume will increase. Do something similar for decreasing volume, and muting.

I'm using Office 365 32bit and Windows 10. His code works for me.

Have fun.

Cheers

pvr928

Thanks,
I'll try that....
 
Upvote 0
I am trying to find some code to turn my speaker volume on, up or down.
I am using the Speak function if that has any relevance...... thanks!
Really, Really hard to find a way - I was even trying to adjust external amps with 0-10V via USB!
I found this:
View / change sound volume on Windows 11/10/7/8/2008 from command-line or GUI
I got the zip file and copied the files to C:\ just to be handy.
Then I had to find out how to run a ".exe" from VBA.
This worked:
("Realtek High Definition Audio" is the "Friendly name" from looking at SoundVolumeView.exe in Windows.)
(I don't know where these .wav files are from , but Freesound - sound search has many)
'========================================= Sound Balance TEST
Shell "C:\SoundVolumeView.exe /SetVolumeChannels ""Realtek High Definition Audio\Device\Speakers\Render"" 24 62", vbNormalFocus
Call PlaySound(Application.ActiveWorkbook.Path & "\ringin.wav", 0, SND_SYNC Or SND_FILENAME Or SND_NOSTOP)
Shell "C:\SoundVolumeView.exe /SetVolumeChannels ""Realtek High Definition Audio\Device\Speakers\Render"" 62 24", vbNormalFocus
Call PlaySound(Application.ActiveWorkbook.Path & "\ringin.wav", 0, SND_SYNC Or SND_FILENAME Or SND_NOSTOP)

Shell "C:\SoundVolumeView.exe /SetVolumeChannels ""Realtek High Definition Audio\Device\Speakers\Render"" 24 62", vbNormalFocus
Call PlaySound(Application.ActiveWorkbook.Path & "\blip.wav", 0, SND_SYNC Or SND_FILENAME Or SND_NOSTOP)
Shell "C:\SoundVolumeView.exe /SetVolumeChannels ""Realtek High Definition Audio\Device\Speakers\Render"" 62 24", vbNormalFocus
Call PlaySound(Application.ActiveWorkbook.Path & "\blip.wav", 0, SND_SYNC Or SND_FILENAME Or SND_NOSTOP)

Shell "C:\SoundVolumeView.exe /SetVolumeChannels ""Realtek High Definition Audio\Device\Speakers\Render"" 21 21", vbNormalFocus
Call PlaySound(Application.ActiveWorkbook.Path & "\ringin.wav", 0, SND_SYNC Or SND_FILENAME Or SND_NOSTOP)
Call PlaySound(Application.ActiveWorkbook.Path & "\blip.wav", 0, SND_SYNC Or SND_FILENAME Or SND_NOSTOP)
Call PlaySound(Application.ActiveWorkbook.Path & "\testsnd.wav", 0, SND_SYNC Or SND_FILENAME Or SND_NOSTOP)
'========================================= Sound Balance TEST
 
Upvote 0
Try this alternative :
VBA Code:
Option Explicit

#If Win64 Then
    Private Const NULL_PTR = 0^
#Else
    Private Const NULL_PTR = 0&
#End If

#If VBA7 Then
    Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageW" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
#Else
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageW" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
#End If

Private Const APPCOMMAND_VOLUME_MUTE As Long = &H80000
Private Const APPCOMMAND_VOLUME_UP As Long = &HA0000
Private Const APPCOMMAND_VOLUME_DOWN As Long = &H90000
Private Const WM_APPCOMMAND As Long = &H319
 
 
Sub ToggleMute()
    Call SendMessage(Application.hwnd, WM_APPCOMMAND, NULL_PTR, ByVal APPCOMMAND_VOLUME_MUTE)
End Sub

Sub DecreaseVol()
    Call SendMessage(Application.hwnd, WM_APPCOMMAND, NULL_PTR, ByVal APPCOMMAND_VOLUME_DOWN)
End Sub

Sub IncreaseVol()
    Call SendMessage(Application.hwnd, WM_APPCOMMAND, NULL_PTR, ByVal APPCOMMAND_VOLUME_UP)
End Sub


Note that this is also a keyboard based workaround that may not work with certain keyboards.
I believe the propper way of doing this in vista or higher is via the core audio interfaces
 
Upvote 0
Try this alternative :
VBA Code:
Option Explicit

#If Win64 Then
    Private Const NULL_PTR = 0^
#Else
    Private Const NULL_PTR = 0&
#End If

#If VBA7 Then
    Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageW" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
#Else
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageW" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
#End If

Private Const APPCOMMAND_VOLUME_MUTE As Long = &H80000
Private Const APPCOMMAND_VOLUME_UP As Long = &HA0000
Private Const APPCOMMAND_VOLUME_DOWN As Long = &H90000
Private Const WM_APPCOMMAND As Long = &H319
 
 
Sub ToggleMute()
    Call SendMessage(Application.hwnd, WM_APPCOMMAND, NULL_PTR, ByVal APPCOMMAND_VOLUME_MUTE)
End Sub

Sub DecreaseVol()
    Call SendMessage(Application.hwnd, WM_APPCOMMAND, NULL_PTR, ByVal APPCOMMAND_VOLUME_DOWN)
End Sub

Sub IncreaseVol()
    Call SendMessage(Application.hwnd, WM_APPCOMMAND, NULL_PTR, ByVal APPCOMMAND_VOLUME_UP)
End Sub


Note that this is also a keyboard based workaround that may not work with certain keyboards.
I believe the propper way of doing this in vista or higher is via the core audio interfaces
Thanks! I wanted to set both Left and Right so that I could adjust the balance. Like: APPCOMMAND_VOLUME_SET_LEFT_RIGHT.
I was so excited to find "SoundVolumeView" - they found what actually works. I used their command line version in the end.
 
Upvote 0

Forum statistics

Threads
1,214,515
Messages
6,119,970
Members
448,933
Latest member
Bluedbw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top