Multiple Apps

Check Server Time

Ease of Use

Hard

Version tested with

2002 

Submitted by:

Jacob Hilderbrand

Description:

The macro demonstrates how to check the server time from VBA. 

Discussion:

You have a program that you want to work for a specific amount of time, or only during certain times. Since a user can easily change the system time, you can refer to the server time (assuming the program is on a server) in your code. 

Code:

instructions for use

			

Option Explicit Private Declare Function NetRemoteTOD Lib "Netapi32.dll" ( _ tServer As Any, _ pBuffer As Long) As Long 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 Type TIME_ZONE_INFORMATION Bias As Long StandardName(32) As Integer StandardDate As SYSTEMTIME StandardBias As Long DaylightName(32) As Integer DaylightDate As SYSTEMTIME DaylightBias As Long End Type Private Declare Function GetTimeZoneInformation Lib "kernel32" _ (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long Private Declare Function NetApiBufferFree Lib "Netapi32.dll" _ (ByVal lpBuffer As Long) As Long Private Type TIME_OF_DAY_INFO tod_elapsedt As Long tod_msecs As Long tod_hours As Long tod_mins As Long tod_secs As Long tod_hunds As Long tod_timezone As Long tod_tinterval As Long tod_day As Long tod_month As Long tod_year As Long tod_weekday As Long End Type Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _ (Destination As Any, _ Source As Any, _ ByVal Length As Long) Public Function getRemoteTOD(ByVal strServer As String) As Date Dim result As Date Dim lRet As Long Dim tod As TIME_OF_DAY_INFO Dim lpbuff As Long Dim tServer() As Byte tServer = strServer & vbNullChar lRet = NetRemoteTOD(tServer(0), lpbuff) If lRet = 0 Then CopyMemory tod, ByVal lpbuff, Len(tod) NetApiBufferFree lpbuff result = DateSerial(tod.tod_year, tod.tod_month, _ tod.tod_day) + _ TimeSerial(tod.tod_hours, tod.tod_mins - tod.tod_timezone, _ tod.tod_secs) getRemoteTOD = result Else Err.Raise Number:=vbObjectError + 1001, _ Description:="cannot get remote TOD" End If End Function Private Sub CheckServerTime() Dim d As Date d = getRemoteTOD("\\Your Server Name Here") MsgBox d End Sub

How to use:

  1. Open an Office Application (Word, Excel etc.).
  2. Alt + F11 to open the VBE.
  3. Insert | Module.
  4. Paste the code in the Code Window.
  5. Close the VBE (Alt + Q or press the X in the top right corner).
 

Test the code:

  1. Tools | Macro | Macros...
  2. Select CheckServerTime and press Run.
 

Sample File:

No Attachment 

Approved by Jacob Hilderbrand


This entry has been viewed 107 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express