怎样使用VBA获取与修改文件创建日期?
1、首先在开发工具中打开VBA编辑器

2、在单元格区域当中输入一些内容作为例子

3、在VBA编辑器中插入模块

4、在模块当中输入如下代码,然后运行
Private Sub CommandButton1_Click()
Dim FileName As String
FileName=Application.GetOpenFilename(, , "请选择文件")
If FileName <> "False" Then
Me.TextBox1=FileName
Else
MsgBox "未选择参考文件!", vbOKOnly+64, "提醒"
End If
End Sub
Private Sub CommandButton2_Click()
Dim fs, files, ans As String
If TextBox1 <> "" Then
Set fs=CreateObject("Scripting.FileSystemObject")
Set files=fs.GetFile(TextBox1.Text)
Me.TextBox2.Text=files.DateCreated
Else
MsgBox "请先输入文件路径!", vbOKOnly, "提示"
End If
End Sub
Private Sub CommandButton3_Click()
Dim hFile As Long, OFS As OFSTRUCT, NEW_TIME As FILETIME, SYS_TIME As SYSTEMTIME
If TextBox3 <> "" And TextBox4 <> "" And TextBox5 <> "" Then
With SYS_TIME
.wYear=TextBox3
.wMonth=TextBox4
.wDay=TextBox5
End With
Call SystemTimeToFileTime(SYS_TIME, NEW_TIME)
hFile=OpenFile(Me.TextBox1.Text, OFS, OF_READWRITE)
Call SetFileTime(hFile, NEW_TIME, NEW_TIME, NEW_TIME)
CloseHandle hFile
Else
MsgBox "请输入完整!", vbOKOnly+64, "提示"
End If
Unload Me
End Sub

5、单击“获取时间”按钮,第二个文本框中立即返回刚选择文件的创建时间,如图

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
阅读量:184
阅读量:41
阅读量:189
阅读量:179
阅读量:45