如何通过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
sFolderandStrFilecombine to a valid file path. The code above adds a backslash ifsFolderdoesn't end with one, which prevents broken paths likeC:\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:
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.' 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
内容的提问来源于stack exchange,提问作者Gofurther
相关产品推荐
相关产品推荐

