You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过VBA修改PowerPoint文件的‘Date Last Modified’属性?

Fixing the FileDateModified Error When Updating PowerPoint's Last Modified Date with VBA

Hey there! Let's break down why you're hitting that error when trying to set f.FileDateModified and how to fix it.

The #1 Reason for the Error: File Locking

Looking at your code, you're opening the PowerPoint presentation with Presentations.Open(..., , , False) — that fourth parameter sets ReadOnly to False, meaning the file is open in write mode. When a file is actively open in PowerPoint, it's locked by the PowerPoint process, so the FileSystemObject can't modify its properties (like the last modified date) until the file is closed and the lock is released.

Quick Fix: Close the Presentation First

Here's a revised version of your code that handles the file correctly:

Dim sFullPath As String
' Make sure your path is properly formatted (adds a backslash if missing)
sFullPath = sFolder & IIf(Right(sFolder, 1) = "\", "", "\") & StrFile

' Step 1: Open and work with your presentation
Set oPresentation = Presentations.Open(sFullPath, ReadOnly:=False, Untitled:=False, WithWindow:=False)
' Add your PowerPoint editing logic here (e.g., slides, text, etc.)
' Save and close to release the file lock
oPresentation.Close SaveChanges:=True

' Step 2: Update the file's last modified date
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim oFile As Object
Set oFile = fso.GetFile(sFullPath)

' Set your desired date (example: current date/time, or a specific date)
oFile.FileDateModified = Now()
' Or use a specific date: oFile.FileDateModified = DateSerial(2024, 5, 20) & " " & TimeSerial(14, 30, 0)

' Clean up objects
Set oFile = Nothing
Set fso = Nothing
Set oPresentation = Nothing

Other Possible Issues to Check

  • Path Errors: Double-check that sFolder and StrFile combine to a valid file path. The code above adds a backslash if sFolder doesn't end with one, which prevents broken paths like C:\MyFolderMyPresentation.pptx.
  • File Permissions: Ensure you have write access to the file and its folder. If the file is marked as "ReadOnly" or stored in a protected folder (like Program Files), you'll get an error when trying to modify it.
  • Confused Properties?: If you're trying to update the internal PowerPoint document property (not the file system's last modified date), use this instead while the presentation is open:
    ' Update the built-in "Last Save Time" property
    oPresentation.BuiltInDocumentProperties("Last Save Time").Value = DateSerial(2024, 5, 20) & " " & TimeSerial(14, 30, 0)
    oPresentation.Save ' Save to make the change stick
    
    Note: Saving the presentation will automatically update the file system's last modified date to the current time, so if you want them to differ, you'll need to first save the presentation, close it, then use the FileSystemObject method to overwrite the file system date.

内容的提问来源于stack exchange,提问作者Gofurther

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:09:35