Power status

chucrutes

New Member
Joined
Dec 15, 2008
Messages
6
Hi, somebody would know how I verify the battery status of the my notebook with VBA in the excel?

I saw one code, but it was in Visual basic, I can not to convert for VBA....

(Sorry my english, I'm brazilian....)
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
VBA basically is VB, but a slightly reduced version (unl;ess the code you saw was VB.Net which is quite different). What/where was the code? I would guess it uses Windows API functions but you can use them in VBA too.
 
Upvote 0
Unfortunately that is .Net so you can't use that. I'll see if I can find an API function that will work.
 
Upvote 0
Try this:
Code:
Private Type SYSTEM_POWER_STATUS
ACLineStatus As Byte
BatteryFlag As Byte
BatteryLifePercent As Byte
Reserved1 As Byte
BatteryLifeTime As Long
BatteryFullLifeTime As Long
End Type
Private Declare Function GetSystemPowerStatus Lib "kernel32" (lpSystemPowerStatus As SYSTEM_POWER_STATUS) As Long
Private Sub GetBatteryStatus()
Dim SPS As SYSTEM_POWER_STATUS
'get the battery powerstatus
GetSystemPowerStatus SPS
'show some information
Select Case SPS.ACLineStatus
Case 0
MsgBox "AC power status: Offline"
Case 1
MsgBox "AC power status: OnLine"
Case 2
MsgBox "AC power status: Unknown"
End Select
Select Case SPS.BatteryFlag
Case 1
MsgBox "Battery charge status: High"
Case 2
MsgBox "Battery charge status: Low"
Case 4
MsgBox "Battery charge status: Critical"
Case 8
MsgBox "Battery charge status: Charging"
Case 128
MsgBox "Battery charge status: No system battery"
Case 255
MsgBox "Battery charge status: Unknown Status"
End Select
End Sub
 
Upvote 0
Re: Power status - updated VBA

Updated code and showing all properties (and fixed typo):



Option Explicit

Private Declare Function GetSystemPowerStatus Lib "kernel32" (lpSystemPowerStatus As SYSTEM_POWER_STATUS) As Long

Private Type SYSTEM_POWER_STATUS
ACLineStatus As Byte
BatteryFlag As Byte
BatteryLifePercent As Byte
SystemStatusFlag As Byte
BatteryLifeTime As Long
BatteryFullLifeTime As Long
End Type


Public Sub getBatteryStatus()
'prints current battery status to immediate window

Dim SPS As SYSTEM_POWER_STATUS
GetSystemPowerStatus SPS 'get system battery power status

With SPS
Debug.Print "Battery Life: ", ;
Select Case .BatteryLifePercent
Case 255: Debug.Print "Unknown"
Case Else: Debug.Print .BatteryLifePercent & "%"
End Select

Debug.Print "Battery Life Time: ", ;
Select Case .BatteryLifeTime
Case -1: Debug.Print "Charging"
Case Else
Debug.Print Int(.BatteryLifeTime / 60) & "min / ";
Select Case .BatteryFullLifeTime
Case -1
If .BatteryLifePercent = 0 Then
Debug.Print "Unknown"
Else 'estimate FullLifeTime:
Debug.Print "~" & Int(.BatteryLifeTime / .BatteryLifePercent * 5 / 3) & "min"
End If
Case Else
Debug.Print .BatteryFullLifeTime & "sec"
End Select
End Select

Debug.Print "AC power status: ", ;
Select Case .ACLineStatus 'show some information
Case 0: Debug.Print "Offline"
Case 1: Debug.Print "OnLine"
Case Else: Debug.Print "Unknown"
End Select

Debug.Print "Battery charge status: ", ;
Select Case .BatteryFlag
Case 0: Debug.Print "Not Charging (33-66%)"
Case 1: Debug.Print "High (>66%)"
Case 2: Debug.Print "Low (<33%)"
Case 4: Debug.Print "Critical (<5%)"
Case 8: Debug.Print "Charging"
Case 128: Debug.Print "No System Battery"
Case 255: Debug.Print "Unknown Status"
End Select

Debug.Print "Battery saver: ", ;
Select Case .SystemStatusFlag
Case 0: Debug.Print "Off"
Case 1: Debug.Print "On (Save energy where possible)" 'Windows 10 only
End Select

End With
End Sub

 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,332
Members
449,077
Latest member
jmsotelo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top