View Full Version : Solved: Adjust Date Field for -7 days
CBrine
01-26-2007, 07:57 AM
Does anyone know how to setup a field that would be 7 days prior to the current day? Here's the code I use for the current day, but I've never been able to figure out how to minus 7 days from it.
{Date \@ "MMM d, yyyy"]
Cal
TonyJollans
01-26-2007, 11:50 AM
It is possible but you wouldn't believe how difficult it is! Word doesn't really have facilities for this kind of thing.
CBrine
01-26-2007, 12:14 PM
I kinda figured that. I tried every possible combination of options I could think of and wasn't successful. I was hoping someone had a shortcut that I had missed. Oh well, maybe I should switch the doc to an excel workbook!!:-)
mdmackillop
01-26-2007, 01:36 PM
Here's a possible workaround.
Private Sub Document_Open()
ActiveDocument.CustomDocumentProperties.Add _
Name:="MyDate", LinkToContent:=False, Value:=Now() - 7, _
Type:=msoPropertyTypeDate
End Sub
{DOCPROPERTY "MyDate" \* MERGEFORMAT}
jdubya
01-29-2007, 10:03 AM
It is possible but you wouldn't believe how difficult it is! Word doesn't really have facilities for this kind of thing.
I can't believe that they don't either, Tony.
Cal, try this. Create a bookmark (I used MyDate) in your document where you want to place the calculated date.
Then use the following code:
Option Explicit
Sub Document_Open()
Dim bookmark As String
UpdateBookmark bookmark:="MyDate", val:=Format$(Now() - 7, "mm\/dd\/yyyy")
End Sub
Private Function UpdateBookmark(ByVal bookmark As String, ByVal val As String)
Dim rng As Range
If ActiveDocument.Bookmarks.Exists(bookmark) Then
Set rng = ActiveDocument.Bookmarks(bookmark).Range
rng.Text = val
ActiveDocument.Bookmarks.Add bookmark, rng
Else
MsgBox "Bookmark " & bookmark & " not found."
End If
Set rng = Nothing
End Function
I got the idea of using a bookmark from Bob in this thread, http://vbaexpress.com/forum/showthread.php?t=10940. (http://vbaexpress.com/forum/showthread.php?t=10940)
Jon
TonyJollans
01-29-2007, 12:11 PM
Just in case you do want to create a field you can find macropod's treatise on the subject here (http://www.wopr.com/w3tfiles/10-249902-DateCalc.zip) It will show you more than you probably ever wanted to know.
CBrine
01-29-2007, 08:05 PM
Thanks for all the help guy's, but I was hoping to do it without the addition of code. I just have a weekly status update that I need to do each week, and it needs the current date, plus the date 7 days ago as a range to show when the status update covers. Just a single date that I always update manually, so I was hoping I could use the field code formula's to do something. It not something I worth using code for, just to save me a few key stokes each week. Thanks for the reply's.
Cal
PS-Tony, interesting document, defintly more then I ever wanted to learn about word date fields.:-) Although if you have something else that just details general formating of fields, using field codes, that would be interesting. I just got out of mail merge hell using an excel datasource. Some very interesting formating that just didn't work when merged into word. I was doing lots of currency's and percentages that needed a specific percision. What a FRIGGING nightmare.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.