is it possible to use word macro to save a word file without updating the "Date Modified" Property?

yanlok1345
StarLounger
Posts: 74
Joined: 18 Oct 2023, 14:48

is it possible to use word macro to save a word file without updating the "Date Modified" Property?

Post by yanlok1345 »

Hi everyone,

I'm trying to create a Word macro that saves the current document without changing the "Date Modified" property.

However, it seems that whenever I save the document using the SaveAs method, the "Date Modified" property is updated to the current date and time.

I've done some research and it seems that this is the default behavior of the SaveAs method in Word. When you save a document using this method, Word creates a new file with a new file path, and the "Date Modified" property is automatically updated to the current date and time.

However, I need to find a way to save a copy of the document without changing the "Date Modified" property. I've tried using the FileSystemObject in VBA to copy the file, but this also updates the "Date Modified" property.

Has anyone found a way to save a currently opened Word document without changing the "Date Modified" property? Any help or advice would be greatly appreciated!

Here's the code I'm currently using to save a copy of the document:

Code: Select all

Sub SaveDocwithoutDMP()

    Dim docName As String
    Dim fileSaveName As String
    Dim filePath As String
   
    docName = ActiveDocument.Name
    
    filePath = Left(docName, InStrRev(docName, "\"))
    fileSaveName = Left(docName, InStrRev(docName, ".") - 1) & "_Copy" & Right(docName, Len(docName) - InStrRev(docName, "."))
    
    ActiveDocument.SaveAs FileName:=filePath & fileSaveName
End Sub
Thanks in advance for any help you can provide!

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15621
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: is it possible to use word macro to save a word file without updating the "Date Modified" Property?

Post by ChrisGreaves »

yanlok1345 wrote:
20 Mar 2024, 13:21
... However, it seems that whenever I save the document using the SaveAs method, the "Date Modified" property is updated to the current date and time.
Hi Yanloki.
Loosely speaking "This is the way that Microsoft Windows works", so the apparent answer is "No this can't be done".
But I detest being told "It can't be done", so ...

You can save the file and then use a separate tool to reset the date/time stamp that shows in applications such as File Explorer.
A long time ago I used a DOS utility called TOUCH.COM, but can't remember now whether it used the DOS system date/time to reset the timestamp, or whether it accepted a parameter so that a set of files could receive identical timestamps.
There must be a small utility that runs under Windows CMD.EXE prompt that does the trick.

If so, then you save the file, wait a couple of seconds (in VBA OnTime) and then from within your application, run a script that runs your Touch program, passing it whatever date/time you want. In your case I suspect that you would pass it the date/time that you had preserved in your application immediately before saving the file.

A second method comes to mind:
Obtain the desired date/time
Reset the Windows system date/time
Save the file (which will thus be marked date-modified with your desired date/time)
Reset the Windows system date/time to the current time (NOW())

How good is your VBA?
Cheers, Chris
There's nothing heavier than an empty water bottle

User avatar
SpeakEasy
4StarLounger
Posts: 550
Joined: 27 Jun 2021, 10:46

Re: is it possible to use word macro to save a word file without updating the "Date Modified" Property?

Post by SpeakEasy »

You can do it with Powershell, e.g

Get-ChildItem D:\Downloads\DeleteMe\example.xml | % {$_.LastWriteTime = '20/03/2023 014:15:16'}

At that can be Shelled from VBA

Code: Select all

Shell "powershell Get-ChildItem  D:\Downloads\DeleteMe\example.xml | % {$_.LastWriteTime = '20/03/2023 014:15:16'}", 0
In addition, you could also consider the Windows API - SetFileTime

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15621
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: is it possible to use word macro to save a word file without updating the "Date Modified" Property?

Post by ChrisGreaves »

SpeakEasy wrote:
20 Mar 2024, 14:36
You can do it with Powershell, e.g
:thumbup: :cool:
Cheers, Chris
There's nothing heavier than an empty water bottle

yanlok1345
StarLounger
Posts: 74
Joined: 18 Oct 2023, 14:48

Re: is it possible to use word macro to save a word file without updating the "Date Modified" Property?

Post by yanlok1345 »

SpeakEasy wrote:
20 Mar 2024, 14:36
You can do it with Powershell, e.g

Get-ChildItem D:\Downloads\DeleteMe\example.xml | % {$_.LastWriteTime = '20/03/2023 014:15:16'}

At that can be Shelled from VBA

Code: Select all

Shell "powershell Get-ChildItem  D:\Downloads\DeleteMe\example.xml | % {$_.LastWriteTime = '20/03/2023 014:15:16'}", 0
In addition, you could also consider the Windows API - SetFileTime
Thank you for your message and the solution you provided for modifying the last write time of the XML file. I appreciate you taking the time to explain the PowerShell command and VBA code in detail. Thank you again for your help and guidance.

yanlok1345
StarLounger
Posts: 74
Joined: 18 Oct 2023, 14:48

Re: is it possible to use word macro to save a word file without updating the "Date Modified" Property?

Post by yanlok1345 »

ChrisGreaves wrote:
20 Mar 2024, 13:48
yanlok1345 wrote:
20 Mar 2024, 13:21
... However, it seems that whenever I save the document using the SaveAs method, the "Date Modified" property is updated to the current date and time.
Hi Yanloki.
Loosely speaking "This is the way that Microsoft Windows works", so the apparent answer is "No this can't be done".
But I detest being told "It can't be done", so ...

You can save the file and then use a separate tool to reset the date/time stamp that shows in applications such as File Explorer.
A long time ago I used a DOS utility called TOUCH.COM, but can't remember now whether it used the DOS system date/time to reset the timestamp, or whether it accepted a parameter so that a set of files could receive identical timestamps.
There must be a small utility that runs under Windows CMD.EXE prompt that does the trick.

If so, then you save the file, wait a couple of seconds (in VBA OnTime) and then from within your application, run a script that runs your Touch program, passing it whatever date/time you want. In your case I suspect that you would pass it the date/time that you had preserved in your application immediately before saving the file.

A second method comes to mind:
Obtain the desired date/time
Reset the Windows system date/time
Save the file (which will thus be marked date-modified with your desired date/time)
Reset the Windows system date/time to the current time (NOW())

How good is your VBA?
Cheers, Chris
Thank you for your response. I appreciate the time and effort you put into providing a detailed and helpful explanation. I found your suggestions and explanations very useful and informative.

I appreciate your help and will definitely consider your suggestions.

Thank you again for your assistance.