Click here to Skip to main content
15,745,569 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys.
My system has these audio playback devices:
1 - Speakers (Realtek High Definition Audio)
2 - CABLE Input (VB-Audio Virtual Cable) (Actually "CABLE Input" is just name and it isn't input. It is related to playback of the sound system of my laptop.)

What I have tried:

After a lot of web searching, I found bellow code. But this is only recognizing the audio systems, not changing.
Public Function getSoundDevices(Optional strComputer As String = ".") As Object
    Const cstrQuery As String = "Select * from Win32_SoundDevice"
    Dim objWMIService As Object 'TypeName = SWbemServicesEx '

    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set getSoundDevices = objWMIService.ExecQuery(cstrQuery, , 48)
    Set objWMIService = Nothing
End Function

Then call the function to use the SWbemObjectSet as input for your other code.
Public Sub test_getSoundDevices()
    Dim objSoundDevices As Object 'TypeName = SWbemObjectSet '
    Dim objDevice As Object 'TypeName = SWbemObjectEx '
    Set objSoundDevices = getSoundDevices()
    For Each objDevice In objSoundDevices
        '* do what you want for each sound device here *'
        Debug.Print "ProductName: " & objDevice.ProductName
    Next objDevice
    Set objDevice = Nothing
    Set objSoundDevices = Nothing
End Sub
Posted
Updated 26-Mar-23 1:06am
v3
Comments
Gerry Schmitz 26-Mar-23 1:02am    
Cable "input" is not "playback"; it isn't switchable with "speakers" (i.e. audio output ... or "line out" .. or phone jack)
Sh.H. 26-Mar-23 6:59am    
Dear Gerry!
Actually "CABLE Input" is the name. It is related to output of the sound system of my laptop.
I gonna add this message in the main post.
Thanks for mentioning.
Gerry Schmitz 26-Mar-23 13:30pm    
The MANUAL appears to contain everything you need to know.

https://vac.muzychenko.net/en/manual/main.htm#WhatIs
Member 15627495 27-Mar-23 3:28am    
your function don't have a return 'obj' statement, can you explain me the 'how' ? which 'object' is sent ?

why define a const '"Select * from Win32_SoundDevice"' just to store a string query ?
Set getSoundDevices = objWMIService.ExecQuery("Select * from Win32_SoundDevice", , 48)
// it's 'one way use' in your script
Sh.H. 27-Mar-23 3:34am    
Thanks. But after this code you wrote, which one will be selected? 'Speakers' or 'CABLE-Input' ?

1 solution

Well...
After a lot of searching, I found a solution.
Here it is:
I used an application called 'SoundVolumeView.exe' from this website:
View / change sound volume on Windows from command line or GUI[^]

This application is very useful and you can do anything with your audio system.

So I used bellow code in my VBA Excel project to change playback device. It changes my audio system to Omni and set the volume to 100% and makes it unmute.

VB
Call Shell(ThisWorkbook.Path & "\SoundVolumeView.exe" & _
    " /SetDefault " & Chr(34) & "Omni" & Chr(34) & " 0" & _
    " /SetDefault " & Chr(34) & "Omni" & Chr(34) & " 1" & _
    " /SetVolume " & Chr(34) & "Omni" & Chr(34) & " 100" & _
    " /Unmute " & Chr(34) & "Omni" & Chr(34), vbNormalFocus)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900