How to automatically toggle Airplane mode on Windows How to automatically toggle Airplane mode on Windows vba vba

How to automatically toggle Airplane mode on Windows


I do not have a function key on my keyboard, and this is not tested, but just an idea - why don't you try like this:

Sub SetMode()    CreateObject("Shell.Application").MinimizeAll            Application.SendKeys "{F12}"  'Try a way to refer the function keyEnd Sub

Another possible option is something like this, depending on your Windows:

Option ExplicitPublic Sub TestMe()    Application.SendKeys ("^{ESC}")    Application.Wait Now + TimeValue("00:00:01")    SendKeys ("{s}")    SendKeys ("{e}")    SendKeys ("{t}")    SendKeys ("{t}")    SendKeys ("{i}")    SendKeys ("{n}")    SendKeys ("{g}")    SendKeys ("{s}")    SendKeys "~", False    Application.Wait Now + TimeValue("00:00:01")    SendKeys ("{a}")    SendKeys ("{i}")    SendKeys ("{r}")    Application.Wait Now + TimeValue("00:00:01")    SendKeys "~", False    Application.Wait Now + TimeValue("00:00:01")    SendKeys "~", FalseEnd Sub

The ~ sign is for ENTER, and the ctrl+escape simulates the windows key on your keyboard. After you reach what you want you can navigate with tabs and arrows.


Solution 1 : SendKeys

AFAIK the Fn key on a keyboard is not intercepted by Windows, it is a hardware mapping to a function key ie. "Volume Up".Now the problem with that is that the "shutdown/enable wifi" key sends a signal to the hardware to power off the card.So that's that for the SendKey, there is no virtual key for "Wireless off/on" (although there is one for "volume up").

Solution 2 : The Windows 8 API

Now the other approach would be to use the Windows 8 API here https://msdn.microsoft.com/en-us/library/windows/hardware/hh406627(v=vs.85).aspx and more specifically the following interfaces :

  • IMediaRadioManager
  • IRadioInstance
  • IRadioInstanceCollection
  • IMediaRadioManagerNotifySink

This should allow you to get the radio for bluetooth, wifi, ... as well as the "airplane mode" then shut them down, but I've never tried to use that with VBS.

Solution 3 : Using WMI queries

Using WMI queries you can basically access anything in your machine, including network cards.The class you are looking for is "Win32_NetworkAdapter" and all docs can be found here : https://msdn.microsoft.com/en-us/library/aa394216(v=vs.85).aspx

Here is a little sample code that will list the current network adapters, you can customize this to save which ones were enabled before you run the script to be able to re enable them after.

' connects to the WMI server of the local machineSet objConnection = GetObject("winmgmts:" _    & "{impersonationLevel=Delegate," _    & "authenticationLevel=PktPrivacy}!" _    & "\\localhost\root\cimv2")' gets a list of all the network adapters in the systemSet objNetworkAdapters = objConnection.ExecQuery("SELECT * FROM  Win32_NetworkAdapter")' loops through all network adaptersFor Each objCurrentNetworkAdapter in objNetworkAdapters    ' objCurrentNetworkAdapter.Disable    ' objCurrentNetworkAdapter.Enable    WScript.Echo objCurrentNetworkAdapter.NameNext

Remark :

You are basically not supposed to access the "Airplane mode" from code as it is a user privilege to do so, imagine if someone builds an app that turns on roaming and data connection then starts updating while you are abroad...