#1
|
|||
|
|||
AutoOpen code to change date code to date text
Appreciate any assistance with creating a macro when a Word document is opened, it will search for and find anywhere (body of the document, headers or footers) the date code and change it to date text. Thank you! Legal Learning Center |
#2
|
|||
|
|||
First, that would fix the current date in the document. It would not be the date that the document was created.
Second, use the CreateDate field in a template instead. Then, you will not need a macro. The field will update each time a document is created from the template and remain fixed. You are trying to reinvent the wheel. |
#3
|
|||
|
|||
Thank you
This macro would be used for old documents that have the date code in them already and we don't want the date code. Instead we want to replace that code with date text. We have a lot of OLD documents with this pesky code and we want to have it change to date text when opening the document. Does this make sense? |
#4
|
||||
|
||||
No, that makes no sense at all, because you have failed to realise what the effect would be, which is to lock in whatever date you process those documents on - not the dates the documents were created, printed or saved (whichever is relevant).
Any macro you run against the document to unlink DATE fields will only do so after those fields have updated. In other words, you will have already lost the date you're trying to preserve. That is the fundamental flaw in relying on code that simply unlinks existing DATE fields. You may be able to correct the problem by changing the DATE fields to CREATEDATE fields. Unlike DATE fields, CREATEDATE fields only update when the document is first saved or is saved via Save As. Other possibilities for retrieving (but not necessarily retaining) the relevant dates are PRINTDATE fields (which record when the document was last printed) or SAVEDATE fields (which record when the document was last saved). Do note that any change to the field type will require the document to be re-saved, which will inevitably change the results of a SAVEDATE field. Of course, you might use a SAVEDATE field to capture the date the document was last saved, then unlink that field before re-saving the document. For example, to replace DATE fields with CREATEDATE fields anywhere in the document (other than textboxes), you might use code like: Code:
Sub GetDateCreated() Application.ScreenUpdating = False Dim Sctn As Section, HdFt As HeaderFooter, Fld As Field With ActiveDocument For Each Fld In .Range.Fields If Fld.Type = wdFieldDate Then Fld.Code.Text = Replace(Fld.Code.Text, "DATE", "CREATEDATE") End If Next Fld For Each Sctn In .Sections For Each HdFt In Sctn.Headers With HdFt If .Exists Then If Sctn.Index = 1 Or .LinkToPrevious = False Then For Each Fld In .Range.Fields If Fld.Type = wdFieldDate Then Fld.Code.Text = Replace(Fld.Code.Text, "DATE", "CREATEDATE") End If Next Fld End If End If End With Next HdFt For Each HdFt In Sctn.Footers With HdFt If .Exists Then If Sctn.Index = 1 Or .LinkToPrevious = False Then For Each Fld In .Range.Fields If Fld.Type = wdFieldDate Then Fld.Code.Text = Replace(Fld.Code.Text, "DATE", "CREATEDATE") End If Next Fld End If End If End With Next HdFt Next Sctn End With Application.ScreenUpdating = True End Sub Code:
Sub DateCorrector() Application.ScreenUpdating = False Dim StrDt As Variant, Sctn As Section, HdFt As HeaderFooter, Fld As Field, Rng As Range StrDt = InputBox("Please Input the Correct Date", "Date Fixer") If IsDate(StrDt) Then StrDt = CDate(StrDt) Else MsgBox "Not a valid date! Exiting", vbCritical End If With ActiveDocument For Each Fld In .Range.Fields If Fld.Type = wdFieldDate Then Set Rng = Fld.Result Fld.Unlink Rng.Text = Format(StrDt, "MMM-DD-YYYY") End If Next Fld For Each Sctn In .Sections For Each HdFt In Sctn.Headers With HdFt If .Exists Then If Sctn.Index = 1 Or .LinkToPrevious = False Then For Each Fld In .Range.Fields If Fld.Type = wdFieldDate Then Set Rng = Fld.Result Fld.Unlink Rng.Text = Format(StrDt, "MMM-DD-YYYY") End If Next Fld End If End If End With Next HdFt For Each HdFt In Sctn.Footers With HdFt If .Exists Then If Sctn.Index = 1 Or .LinkToPrevious = False Then For Each Fld In .Range.Fields If Fld.Type = wdFieldDate Then Set Rng = Fld.Result Fld.Unlink Rng.Text = Format(StrDt, "MMM-DD-YYYY") End If Next Fld End If End If End With Next HdFt Next Sctn End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Quote:
Yes, this makes sense. However, the date will be the date that you run the macro, not the date that was originally in the document. See Paul's comments and macros. |
#6
|
|||
|
|||
Thank you,
It makes sense. Once the document is opened that has the date code, it is going to change before any code is run on it. I can unlink it at that time so going forward it will be text. Is there a way through a group policy or option to disable date code going forward? |
#7
|
||||
|
||||
Not really. The best you can do is use a CREATEDATE field so the date remains tied to whatever date the document is first saved, or saved via Save As. That way, whoever creates the document should at least notice that the date on a document that takes some days to complete becomes out of date.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Change font color if date in column F is prior to today's date. | kevinbradley57 | Excel Programming | 1 | 12-05-2018 07:35 PM |
VBA Code to search for field codes with certain text before the Field code and to change style | welcometocandyland | Word VBA | 4 | 02-08-2017 06:53 PM |
VBA code for inserting a future date | cosmopolitan | Word VBA | 1 | 08-14-2013 01:58 PM |
Change format of date when using Now function in VB code | Bondai | Excel Programming | 2 | 03-02-2012 05:09 PM |
Imported message date change to today's date | promark | Outlook | 1 | 12-23-2005 07:21 AM |