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.
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 :
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...
Related
I am interacting with a .bat file launched in my VBA code. The shell is open and I managed to send a few commands however some other commands fail like F9, DOWN and UP.
Here is my chunk of code :
Sub OpenBrice()
Path = "F:\Brice\valpp"
ChDir Path
Shell Path & "\Brice.bat", vbNormalFocus
'Wait during opening BRICE
Wait (2)
'PASS
SendKeys "123"
Wait (1)
'Press Enter:
SendKeys "~"
Wait (1)
'Update current date
SendKeys "O"
Wait (1)
SendKeys "F"
Wait (1)
SendKeys (Format(Now(), "ddmmyy"))
Wait (1)
SendKeys "{ESC}"
Wait (2)
SendKeys "{F9}"
The problem happens on the last line, {F9} doesn't work, as well for {DOWN} or {UP}..
Further in the code, the line : SendKeys "{F8}" works perfectly...
I suspect a bad configuration of the shell or something similar ? Thanks a lot!
P.S : I am just testing some code, I know that I should declare all my variable and use Option Explicit
I'm trying to update some data into a SharePoint document and while trying this in local environment I do not face any issue.
But when I try to do the same in virtual desktop, I couldn't make it. I'm being populated with Windows Alert to key-in username & password. I've tried using 'SendKeys' for this scenario, but it doesn't make sense.
....
SendKeys "abc\ATX123",5
SendKeys "Password1",5
SendKeys "{ENTER}",5
....
This snippet is just passing 'ENTER' without entering ID & Pwd. Can anyone suggest me some possible solution, please?
Finally, I've found a way to achieve this requirement,,,,, bit indirect, but that was the only way I could find at the end.
What I did is simple - just created one windows scripted file 'Logon.vbs' to handle that popup screen and called the vbs in VBA.
Refer to the sample below, if you're looking for something like this:
Windows Script:
'Creating an script object
Set oWSH = WScript.CreateObject("WScript.Shell")
'Activating alert screen
oWSH.AppActivate ("Windows Security")
'Passing the value UserName/UserID
oWSH.SendKeys "USERNAME" 'ensure to complete the username with apropriate domain e.g., abc/A123456
'Changing the focus to password textbox
oWSH.SendKeys "{TAB}"
'Passing the value password
oWSH.SendKeys "PASSWORD"
'Clicking enter to complete the screen
oWSH.SendKeys "{ENTER}"
'Releasing the script object
Set oWSH = Nothing
VBA code to call the VBS script - Logon.vbs:
Shell "WScript C:\Users\ABC\Desktop\Logon.vbs", vbNormalFocus
First of all you'll want to SendKeys "yourString", Boolean. As I know the SendKeys command wants a True or a False as second input.
You could also try "tabbing" from a field to another:
SendKeys "abc\ATX123", True
SendKeys "{TAB}", True
SendKeys "Password1", True
SendKeys "{ENTER}", True
if the first one didn't get get written, maybe the focus is on another component of the userform. Try "tabbing" to find out if your "Username" textbox is yet activated as the UserForm appears. In case it isn't you could also try to set some focus
I am currently trying to automate screenshot grabs from Bloomberg Terminal using DDE within VBA. Currently I have come up with a way to automatically pull screenshots by 'printing' through the Microsoft XPS Document Writer:
Dim ch As Variant
Dim CUSIP As String
CUSIP = Range("B12")
ch = DDEInitiate("winblp", "bbk")
Call DDEExecute(ch, "<blp-1>" & CUSIP & " mtge<GO>")
Call DDEExecute(ch, "<blp-1> DES<Go><print>")
Application.Wait Now + TimeValue("0:00:02")
Application.SendKeys "DES ", False
Application.SendKeys "{enter}", False
Call DDETerminate(ch)
The code seems to work fine on the first run, but then crashes the developer on each subsequent. Any feedback or further suggestions would be appreciated. Thanks!
Probably too late but this may help someone as I've had to do this as well. You can just use the <copy> command in your DDE calls and paste it into Word/Excel/Outlook without using send keys:
Call DDEExecute(ch, "<blp-1> <copy>")
I'm trying to write a simple script to fill in login details on a program that requires signing in using a keyboard shortcut.
set wshshell = wscript.CreateObject("wscript.shell")
WshShell.SendKeys "username{tab}passowrd{enter}"
This works as desired on my work computer (xp) but on my home computer (windows 8) the keyboard shortcut deactivates the current window so that the details are not entered.
Any help would be greatly appreciated.
You may want to look into the shell's AppActivate method. You could write something like this:
boolSuccess = False
do until boolSuccess = True
boolSuccess = WshShell.AppActivate("AppName")
WshShell.sleep 500
loop
WshShell.SendKeys "username{tab}password{enter}"
This will move the focus to your target app, and then send the keys. The tragically kludgey part is that you may need that 'sleep' between the AppActivate and the SendKeys to be sure that your target app has time to receive the focus.
How would I output a sentence with VB.NET?
Example: Your program and Internet Explorer are open, your program outputs "http://stackoverflow.com" and presses "{ENTER}", and it will go to the page.
Example: An auto-talking bot for MSN.
If you mean "send a string to a running program", I think you're looking for SendKeys
Edit
SendKeys takes a string argument (that's why it's named SendKeys, not SendKey), so yes, you can send "sentences".