ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Adjust Windows Date/Time from Excel VBA (https://www.excelbanter.com/excel-programming/387260-adjust-windows-date-time-excel-vba.html)

Chris L

Adjust Windows Date/Time from Excel VBA
 
Hi,

Is there any method to adjust Windows Date/Time from Excel VBA?

Regards,
Chris

NickHK

Adjust Windows Date/Time from Excel VBA
 
Chris,
I would imagine the SetSystemTime API. However, I would hope the user has
OK'd this change before you make the call.

<From API-Guide: http://www.allapi.net/
The SetSystemTime function sets the current system time and date. The system
time is expressed in Coordinated Universal Time (UTC).

Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type

Private Declare Function SetSystemTime Lib "kernel32" (lpSystemTime As
SYSTEMTIME) As Long

Private Sub Form_Load()
Dim lpSystemTime As SYSTEMTIME
lpSystemTime.wYear = 2000
lpSystemTime.wMonth = 1
'Note: The wDayOfWeek member of the SYSTEMTIME structure is ignored.
lpSystemTime.wDayOfWeek = -1
lpSystemTime.wDay = 24
lpSystemTime.wHour = 23
lpSystemTime.wMinute = 26
lpSystemTime.wSecond = 0
lpSystemTime.wMilliseconds = 0
'set the new time
SetSystemTime lpSystemTime
End Sub

</From API-Guide: http://www.allapi.net/

NickHK

"Chris L" wrote in message
...
Hi,

Is there any method to adjust Windows Date/Time from Excel VBA?

Regards,
Chris




urkec

Adjust Windows Date/Time from Excel VBA
 
You can try using this:


Sub setTime()

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,(SystemTime)} !\\" & _
strComputer & "\root\cimv2")

Set dt = CreateObject("WbemScripting.SWbemDateTime")

dt.Year = 2007
dt.Month = 4
dt.Day = 12
dt.Hours = 21
dt.Minutes = 10
dt.Seconds = 30
dt.Microseconds = 0
dt.UTC = -480 'GMT - 8 h

Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")

For Each objOperatingSystem In colOperatingSystems
objOperatingSystem.SetDateTime dt.Value
Next

End Sub


You need administrator privileges to run this

--
urkec


"Chris L" wrote:

Hi,

Is there any method to adjust Windows Date/Time from Excel VBA?

Regards,
Chris



All times are GMT +1. The time now is 01:33 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com