Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve the computer's charging status & current battery level

I needed a VBA procedure to retrieve the system's current battery charge level and charging status.

I was surprised not to find anything on Stack Overflow, and I eventually figured it out, so I'll share the code here as a Q+A.

like image 810
ashleedawg Avatar asked Jun 12 '18 10:06

ashleedawg


People also ask

How do I check my computer battery status?

Select Search on the taskbar, type Command prompt, press and hold (or right-click) Command prompt, and then select Run as administrator > Yes. At the command prompt, type powercfg /batteryreport, then press Enter. The battery report will be an HTML file that's stored in a folder on your PC.

How do I show my charging status?

Open your phone's Settings app. Under "Battery," see how much charge you have left, and about how long it will last.

How do I see how much Windows 10 is charging?

Click on the battery icon on the Taskbar, and a notification should show you the percentage of remaining charge available as well as the number of hours and minutes until the charge runs out.

How do you know my laptop is fully charged when turned off?

As the battery charges, the battery symbol will fill up. If you click the battery symbol on the screen and it says it's 100%, then the battery is fully charged and you can unplug the laptop from the mains power.


1 Answers

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

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() 'batt info to immediate window
    
    Dim SPS As SYSTEM_POWER_STATUS
    GetSystemPowerStatus SPS 'get system batt 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 1 + 8: Debug.Print "High (>66%)- Charging"
            Case 2 + 8: Debug.Print "Low (<33%) - Charging"
            Case 4 + 8: Debug.Print "Critical (<5%) - 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

More Information:

  • MSDN : GetSystemPowerStatus function (Windows)
  • MSDN : SYSTEM_POWER_STATUS structure
  • MSDN : Battery Saver
like image 108
ashleedawg Avatar answered Sep 27 '22 22:09

ashleedawg