#1
|
|||
|
|||
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 ActiveDocument.Unprotect ' ' Display InputBox and get number of days GetInput: 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, _ NoReset:=True GoTo GetInput End Sub |
#2
|
|||
|
|||
Cross posted at VBA Express.
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
New laptop/ultrabook, future of MS Office? | Jamtart | Office | 5 | 06-24-2013 10:24 AM |
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 |
Inserting Date, formatting to superscript and subscript | louq | Word | 1 | 10-22-2009 09:29 AM |