17

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.

Fadi
  • 3,302
  • 3
  • 18
  • 41
  • Have you tried looking for CMD line versions of turning airplane mode on and off? – Cody G Feb 07 '17 at 13:17
  • @CodyG.,Yes and I only found answers about Enable/Disable wireless adapter, But i don't want this, I want to toggle airplane mode. – Fadi Feb 07 '17 at 13:20
  • 2
    Given there are hypothetical safety implications to randomly activating the radio I doubt this is possible or easy to automate by design. – Alex K. Feb 07 '17 at 13:23
  • See https://social.microsoft.com/Forums/en-US/e41b0e6e-4b6f-49b6-ae0e-a09cd84fa47a/script-to-enable-and-disable-airplane-mode?forum=Offtopic and http://superuser.com/questions/882411/automatically-turn-airplane-mode-on-off-in-windows-8-1-when-the-ethernet-cable-i --- there seem to be AutoIt scripts floating around that might work for you. – Cody G Feb 07 '17 at 13:31
  • Thank You @AlexK.,I don't think I (or any one using my code) will use the laptop in plane. I just want to sync some files on network drive. – Fadi Feb 07 '17 at 13:32
  • Also, you may be able to use an HID emulator to simply send the "FN+F12" command. Function commands are tricky because they're device dependent, so you'd have to listen for what command it is sending. – Cody G Feb 07 '17 at 13:33
  • Thank You @CodyG., I couldn't download the zip because I don't have an account on eightforums.com. and I can't using `Sendkeys` to send `FN` key. – Fadi Feb 07 '17 at 13:42
  • Take a look at https://autohotkey.com/board/topic/38015-ahkhid-an-ahk-implementation-of-the-hid-functions/ --- Use the recorder to find your airplane mode button OR FN+F12 keyboard data. Then use AHK to send that same command. After you figure out that you can compile the AHK script to an EXE and call it via VBA – Cody G Feb 07 '17 at 15:58
  • Not sure about your environment, but If you just want to sync some files on network drive, have you tried to change the routes metric? – MC ND Feb 10 '17 at 21:05
  • Thank You @MCND, I'm not expert with networks so I didn't know what `change the routes metric` means , is it means changing the router settings, and what I should change. – Fadi Feb 10 '17 at 22:01
  • 1
    [This](http://www.opentechguides.com/how-to/article/networking/56/windows-routing-table.html) is a simple guide. See if it can help you – MC ND Feb 10 '17 at 22:51
  • SendKeys is really restricted. You can probably do this with virtual Keys. Use **WM_KEYUP** and **WM_KEYDOWN**. Check this article[https://msdn.microsoft.com/en-us/library/windows/desktop/gg153546(v=vs.85).aspx] . The key for FN is VK 0xff also – Seb Feb 15 '17 at 14:45
  • Thank You @Seb, I will try. – Fadi Feb 15 '17 at 20:01

2 Answers2

4

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.

Vityata
  • 42,633
  • 8
  • 55
  • 100
  • @Fadi - then, just for a test invert the settings of your PC in BIOS and change the funciton keys priority - see here - http://superuser.com/questions/735062/how-to-invert-the-fn-key Then, run the code and if it does what you need, look for a way to refer the function keys. – Vityata Feb 13 '17 at 14:27
  • Thank You @Vityata, the problem is with `FN` key, i try a lot of methods and download some application to detect keys press but nothing could detect 'Fn' key. and i don't want to change the funciton keys priority because i use 'F1-F12' keys a lot for a years in Excel and other applications. – Fadi Feb 13 '17 at 14:37
  • @Fadi - if you change it just for 5 minutes and check whether the code works, then you know that if you find a way to refer the `FN` key your task would be ready. Its just a direction. – Vityata Feb 13 '17 at 14:40
  • I tried that, some keys are detected but not the one that toggle Airplane mode `F12`. and when I run your code it works like before so sending `%{F11}` from VBA window will activate the Excel window. – Fadi Feb 13 '17 at 15:11
  • @Fadi - you are right, it should not be working. Try again, having only the Excel opened. Thus, with the Alt+Tab you can switch to the desktop and from there with the F12 you can manage the Airplane Mode. A little bit of workaround, but still something :) – Vityata Feb 13 '17 at 15:21
  • 1
    Nothing changed, I don't think the active windows has any effect here because when i manually press `F12` it toggle Airplane mode whatever the active windows is. – Fadi Feb 13 '17 at 15:37
  • Thank You again, when I run 'Sub TestMe()' from excel vbe it type in vbe window, so I made .exe from vb6 and when run it open windows 8 start page and then close it and flash some window (i can't know what this window is because it hide fast) then toshiba system setting application is started :), (this test on Windows 8). – Fadi Feb 16 '17 at 16:37
2

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 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...

Foxtrot
  • 607
  • 4
  • 10
  • Thank You, running this code give more then 30 adapter, can I list only connected adapter. – Fadi Feb 16 '17 at 16:01
  • Ok I could list only connected adapter by adding `where Netconnectionstatus=2 and PhysicalAdapter = true` to the query, this helps me a lot, thank you again. – Fadi Feb 17 '17 at 01:26
  • I failed to use API, please if you can give an example (VBA or VB6) of how to get "Airplane Mode" status (On or Off), that will be great. – Fadi Feb 17 '17 at 01:37
  • I don't have an example for the windows API, I've not yet used it. I just know it exists but there is little documentation online at the moment... – Foxtrot Feb 17 '17 at 07:46