On my laptop I can toggle Airplane mode manually by pressing FN+F12, I want to do the same thing automatically from VB6 project or VBA.
I did a lot of search and only found answers about Enable/Disable wireless adapter or using Sendkeys
for Windows 8:
Dim WSh As Object
Set WSh = CreateObject("Wscript.Shell")
WSh.Run "C:\WINDOWS\system32\rundll32.exe %SystemRoot%\system32\van.dll,RunVAN", , True
Sleep 200
WSh.SendKeys " "
Sleep 1000
WSh.SendKeys "{ESC}"
But this code is not reliable and I don't think it will work on Windows 7 or Windows 10.
So my question is: Is there any reliable way to automatically toggle Airplane mode on Windows.
Function key Some computers, such as laptops, include a dedicated hardware key to enable or disable Airplane mode. If this is the case, you can simply press the Function key + Airplane key to turn on the feature.
Function key method Laptops usually include a dedicated physical key to enable or disable Airplane mode. If this is the case, press the Function key + Airplane keyboard shortcut (or dedicated key) to turn on the feature.
Reasons Why Windows 10 Gets Stuck in Airplane Mode Usually, the problem is due to software bugs or glitches, faulty network drivers, or a simple physical switch. In most cases, your first approach should be to restart the computer.
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 key
End Sub
Another possible option is something like this, depending on your Windows:
Option Explicit
Public 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 "~", False
End 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 :
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 machine
Set objConnection = GetObject("winmgmts:" _
& "{impersonationLevel=Delegate," _
& "authenticationLevel=PktPrivacy}!" _
& "\\localhost\root\cimv2")
' gets a list of all the network adapters in the system
Set objNetworkAdapters = objConnection.ExecQuery("SELECT * FROM Win32_NetworkAdapter")
' loops through all network adapters
For Each objCurrentNetworkAdapter in objNetworkAdapters
' objCurrentNetworkAdapter.Disable
' objCurrentNetworkAdapter.Enable
WScript.Echo objCurrentNetworkAdapter.Name
Next
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With