Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Thread Tools Display Modes
Old 08-14-2013, 10:18 AM
cosmopolitan cosmopolitan is offline VBA code for inserting a future date Windows Vista VBA code for inserting a future date Office 2007
VBA code for inserting a future date
Join Date: Aug 2013
Posts: 2
cosmopolitan is on a distinguished road
Default VBA code for inserting a future date

I have the below code in a 2007 word document to be able to insert a future date, but the form is protected due to having form fields. I found an example of how to put in code to unprotect and protect again so that the future date macro works, however I don't know where to insert the code to protect the document again. I have the code in to unprotect it. Can someone please tell me what code needs to be put in to protect the document again and where to insert it?

Sub InsertFutureDate()
' Written by Graham Mayor and posted on the word.docmanagement
' newsgroup in March 2000
' Inserts a future date in a document - note that this is not a field
' Some style revisions and error handler by Charles Kenyon
Dim Message As String
Dim Mask As String
Dim Title As String
Dim Default As String
Dim Date1 As String

Dim MyValue As Variant
Dim MyText As String
Dim Var1 As String
Dim Var2 As String
Dim Var3 As String
Dim Var4 As String
Dim Var5 As String
Dim Var6 As String
Dim Var7 As String
Dim Var8 As String
Mask = "MMMM d, yyyy" ' Set Date format
Default = "7" ' Set default.
Title = "Plus or minus date starting with " & Format(Date, Mask)
Date1 = Format(Date, Mask)
Var1 = "Enter number of days by which to vary above date. " _
& "The number entered will be added to "
Var2 = Format(Date + Default, Mask) ' Today plus default (7)
Var3 = Format(Date - Default, Mask) ' Today minus default (7)
Var4 = ". The default ("
Var5 = ") will produce the date "
Var6 = ". Minus (-"
Var7 = ". Entering '0' (zero) will insert "
Var8 = " (today). Click cancel to quit."
MyText = Var1 & Date1 & Var4 & Default & Var5 & Var2 & Var6 _
& Default & Var5 & Var3 & Var7 & Date1 & Var8

' Display InputBox and get number of days
MyValue = InputBox(MyText, Title, Default)
If MyValue = "" Then
End 'quit subroutine
End If
On Error GoTo Oops ' just in case user typed non-number
Selection.InsertBefore Format((Date + MyValue), Mask)
Selection.Collapse (wdCollapseEnd)
End 'End subroutine
Oops: ' error handler in case user types something other than a number
MsgBox Prompt:="Sorry, only a number will work, please try again.", _
Buttons:=vbExclamation, _
Title:="A number is needed here."
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _

GoTo GetInput

End Sub
Reply With Quote
Old 08-14-2013, 01:58 PM
gmaxey gmaxey is offline VBA code for inserting a future date Windows 7 32bit VBA code for inserting a future date Office 2010 (Version 14.0)
Word MVP 2003-2009
Join Date: May 2010
Location: Marble, NC
Posts: 1,189
gmaxey has a spectacular aura aboutgmaxey has a spectacular aura aboutgmaxey has a spectacular aura about

Cross posted at VBA Express.
Greg Maxey
Please visit my web site at
Reply With Quote

Thread Tools
Display Modes

Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA code for inserting a future date New laptop/ultrabook, future of MS Office? Jamtart Office 5 06-24-2013 10:24 AM
VBA code for inserting a future date Inserting a date calculated from a table cell theMikeD Word 2 09-11-2012 05:46 PM
Actual work in the future joday48 Project 3 07-06-2012 06:00 AM
Change format of date when using Now function in VB code Bondai Excel Programming 2 03-02-2012 05:09 PM
VBA code for inserting a future date Inserting Date, formatting to superscript and subscript louq Word 1 10-22-2009 09:29 AM

Other Forums: Access Forums - Senior Forums

All times are GMT -7. The time now is 05:55 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2021 DragonByte Technologies Ltd. is not affiliated with Microsoft