你之前给的这段vba代码“VBA 代码:修改文件时间(完整代码)
Option Explicit
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
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
' Windows API 声明
Private Declare PtrSafe Function CreateFile Lib "kernel32" Alias "CreateFileA" _
(ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, _
ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare PtrSafe Function SetFileTime Lib "kernel32" _
(ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, _
lpLastWriteTime As FILETIME) As Long
Private Declare PtrSafe Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare PtrSafe Sub SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME)
' 打开文件的权限和操作模式常量
Private Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Const FILE_ATTRIBUTE_NORMAL = &H80
' 窗口交互声明
Private Declare PtrSafe Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
' 日期时间转 FILETIME
Private Function DateToFileTime(ByVal dt As Date) As FILETIME
Dim st As SYSTEMTIME
Dim ft As FILETIME
With st
.wYear = Year(dt)
.wMonth = Month(dt)
.wDay = Day(dt)
.wHour = Hour(dt)
.wMinute = Minute(dt)
.wSecond = Second(dt)
End With
Call SystemTimeToFileTime(st, ft)
DateToFileTime = ft
End Function
' 修改文件时间
Sub ModifyFileTime()
Dim filePath As String
Dim creationTime As FILETIME
Dim accessTime As FILETIME
Dim modifyTime As FILETIME
Dim hFile As Long
Dim answer As VbMsgBoxResult
' 选择文件
filePath = SelectFile()
If filePath = vbNullString Then
MsgBox "未选择文件!", vbExclamation
Exit Sub
End If
' 获取用户输入日期时间
Dim cTime, aTime, mTime As Date
On Error Resume Next
cTime = InputBox("请输入创建时间 (格式: yyyy-mm-dd hh:mm:ss)", "创建时间", "2023-01-01 12:00:00")
aTime = InputBox("请输入访问时间 (格式: yyyy-mm-dd hh:mm:ss)", "访问时间", "2023-01-01 12:00:00")
mTime = InputBox("请输入修改时间 (格式: yyyy-mm-dd hh:mm:ss)", "修改时间", "2023-01-01 12:00:00")
If Err.Number <> 0 Then
MsgBox "输入时间格式错误,操作中止。", vbCritical
Exit Sub
End If
On Error GoTo 0
' 转换为 FILETIME 结构
creationTime = DateToFileTime(cTime)
accessTime = DateToFileTime(aTime)
modifyTime = DateToFileTime(mTime)
' 打开文件
hFile = CreateFile(filePath, GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
If hFile = -1 Then
MsgBox "无法打开文件,请检查文件路径或权限。", vbCritical
Exit Sub
End If
' 设置文件时间
If SetFileTime(hFile, creationTime, accessTime, modifyTime) = 0 Then
MsgBox "修改时间失败,请检查系统保护或权限。", vbCritical
Else
MsgBox "文件时间修改成功!", vbInformation
End If
' 关闭文件句柄
Call CloseHandle(hFile)
End Sub
' 选择文件对话框
Function SelectFile() As String
Dim ofn As OPENFILENAME
Dim filter As String
Dim fileBuffer As String * 260
Dim titleBuffer As String * 260
filter = "所有文件(*.*)" & Chr(0) & "*.*" & Chr(0)
With ofn
.lStructSize = Len(ofn)
.hwndOwner = Application.hwnd
.lpstrFilter = filter
.lpstrFile = fileBuffer
.nMaxFile = Len(fileBuffer)
.lpstrFileTitle = titleBuffer
.nMaxFileTitle = Len(titleBuffer)
.lpstrTitle = "选择需要修改的文件"
End With
If GetOpenFileName(ofn) <> 0 Then
SelectFile = Left(ofn.lpstrFile, InStr(ofn.lpstrFile, Chr(0)) - 1)
Else
SelectFile = vbNullString
End If
End Function”在我的电脑里可以正常运行,换了一台域里面没有管理员权限的电脑无法执行,跳出来一个很小的窗口提示未选择文件,可能的原因有哪些?有没有解决措施?