Override Built-in Keywords

You can override some of the built-in VB keywords with your own version of the function. For instance, FileDateTime is a handy built-in function in VB, but it suffers from one big problem: It cant set the date/time of a file. By overriding the built-in function, however, you can provide this feature. With this approach, the function can determine for itself how it is being used and perform accordingly.

You can override a number of keywords and functions in this manner:

Private Declare Function SystemTimeToFileTime Lib _	"kernel32" (lpSystemTime As SYSTEMTIME, _	lpFileTime As FILETIME) As LongPrivate Declare Function LocalFileTimeToFileTime _	Lib "kernel32" (lpLocalFileTime As FILETIME, _	lpFileTime As FILETIME) As LongPrivate Declare Function CreateFile Lib "kernel32" _	Alias "CreateFileA" (ByVal lpFileName As _	String, ByVal dwDesiredAccess As Long, ByVal _	dwShareMode As Long, lpSecurityAttributes As _	Any, ByVal dwCreationDisposition As Long, _	ByVal dwFlagsAndAttributes As Long, _	ByVal hTemplateFile As Long) As LongPrivate Declare Function SetFileTime Lib "kernel32" _	(ByVal hFile As Long, lpCreationTime As Any, _	lpLastAccessTime As Any, lpLastWriteTime As _	Any) As LongPrivate Declare Function CloseHandle Lib "kernel32" _	(ByVal hObject As Long) As LongPrivate Type FILETIME	dwLowDateTime As Long	dwHighDateTime As LongEnd TypePrivate 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 IntegerEnd TypePrivate Const GENERIC_WRITE As Long = &H40000000Private Const FILE_SHARE_READ As Long = &H1Private Const FILE_SHARE_WRITE As Long = &H2Private Const OPEN_EXISTING As Long = 3Public Function FileDateTime(ByVal FileName As String, _	Optional ByVal TimeStamp As Variant) As Date	' Raises an error if one occurs just like FileDateTime	Dim x As Long	Dim Handle As Long	Dim System_Time As SYSTEMTIME	Dim File_Time As FILETIME	Dim Local_Time As FILETIME	If IsMissing(TimeStamp) Then		'It's missing so they must want to GET the timestamp		'This acts EXACTLY like the original built-in function		FileDateTime = VBA.FileDateTime(FileName)	ElseIf VarType(TimeStamp) <> vbDate Then		'You must pass in a date to be valid		Err.Raise 450	Else		System_Time.wYear = Year(TimeStamp)		System_Time.wMonth = Month(TimeStamp)		System_Time.wDay = Day(TimeStamp)		System_Time.wDayOfWeek = _			Weekday(TimeStamp) - 1		System_Time.wHour = Hour(TimeStamp)		System_Time.wMinute = Minute(TimeStamp)		System_Time.wSecond = Second(TimeStamp)		System_Time.wMilliseconds = 0		'Convert the system time to a file time		x = SystemTimeToFileTime(System_Time, Local_Time)		'Convert local file time to file time based on UTC		x = LocalFileTimeToFileTime(Local_Time, File_Time)		'Open the file so we can get a file handle to 		'the file		Handle = CreateFile(FileName, GENERIC_WRITE, _			FILE_SHARE_READ Or FILE_SHARE_WRITE, _			ByVal 0&, OPEN_EXISTING, 0, 0)		If Handle = 0 Then			Err.Raise 53, "FileDateTime", _				"Can't open the file"		Else			'Now change the file time and date stamp			x = SetFileTime(Handle, ByVal 0&, _				ByVal 0&, File_Time)			If x = 0 Then				'Error occured				Err.Raise 1, "FileDateTime", _					"Unable to set file timestamp"			End If			Call CloseHandle(Handle)			'Return newly set date/time			FileDateTime = VBA.FileDateTime(FileName)		End If	End IfEnd Function
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

Top 5 B2B SaaS Marketing Agencies for 2023

In recent years, the software-as-a-service (SaaS) sector has experienced exponential growth as more and more companies choose cloud-based solutions. Any SaaS company hoping to stay ahead of the curve in this quickly changing industry needs to invest in effective marketing. So selecting the best marketing agency can mean the difference

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.