Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
calendar Object
I'm trying to use the calendar object in an online form I created. The code I
have was written by Martin Green (http://www.fontstuff.com/word/wordtut03a.htm). It works great on the computer I created the fields with (Word 03) but when I send the form to another computer (Word 02) I get an error message that says "This method or property is not available because the object refers to a protected area of the document." If I unlock the form in Word 02 I can run the macro just fine. The code looks like this (Form Code) Private Sub Calendar1_Click() ' Transfer date selected on calendar to the insertion ' insertion and close UserForm. Selection.Text = Format(Calendar1.Value, "m/d/yy") Selection.MoveRight Unit:=wdCharacter, Count:=1 Unload Me End Sub Private Sub cmdClose_Click() ' Close the form without entering a date if the user ' presses the [ESCAPE] key Unload Me End Sub Private Sub UserForm_Initialize() ' If selection is a date show the same date, otherwise show ' today's date. If IsDate(Selection.Text) Then Calendar1.Value = DateValue(Selection.Text) Else Calendar1.Value = Date End If End Sub The code looks like this (This Document Code) Sub OpenCalendar() ' Display the frmCalendar UserForm frmCalendar.Show End Sub Private Sub Document_Close() On Error Resume Next CustomizationContext = NormalTemplate ' Cancel the keyboard shortcut "Ctrl+Shift+C" KeyBindings.Key(KeyCode:=BuildKeyCode(wdKeyControl , _ wdKeyShift, wdKeyC)).Clear ' Remove the "Insert Date" command from the right-click menu Application.CommandBars("Text").Controls("Insert Date").Delete End Sub Private Sub Document_Open() Dim NewControl As CommandBarControl ' Assign the keyboard shortcut "Ctrl+Shift+C" CustomizationContext = NormalTemplate KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, _ wdKeyShift, wdKeyC), KeyCategory:=wdKeyCategoryMacro, _ Command:="ThisDocument.OpenCalendar" ' Add the "Insert Date" command to the right-click menu Set NewControl = Application.CommandBars("Text").Controls.Add With NewControl ..Caption = "Insert Date" ..OnAction = "ThisDocument.OpenCalendar" ..BeginGroup = True End With End Sub Martin's website says it should work on most versions of Word. Maybe I need the form to unlock, launch the calendar object, and then re-lock? Or, maybe there is another way? Something I'm missing? If you have the time to take a look at this, I would be very appreciative. Jason K. |
#2
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
calendar Object
Hi Jason,
The code you found isn't intended to be used in a protected form, but to insert a date in regular text. The core of the problem is what the code tries to do with the result of the calendar: Selection.Text = Format(Calendar1.Value, "m/d/yy") isn't allowed in a protected form, because it's interpreted as an attempt to remove the form field. Instead, what you need is something like this, but using the real name of the field that receives the date: ActiveDocument.FormFields("SomeDate").Result = _ Format(Calendar1.Value, "m/d/yy") In fact, in a protected form you don't need a lot of the code here. Instead of adding a macro's name as a command to the right-click menu, assign the OpenCalendar macro as the Entry macro of the particular form field that should contain the date. Get rid of the Document_Open and Document_Close routines (which also are ill-advised because they alter Normal.dot, generally a bad idea). -- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org On Fri, 29 Dec 2006 11:02:00 -0800, Jason K. wrote: I'm trying to use the calendar object in an online form I created. The code I have was written by Martin Green (http://www.fontstuff.com/word/wordtut03a.htm). It works great on the computer I created the fields with (Word 03) but when I send the form to another computer (Word 02) I get an error message that says "This method or property is not available because the object refers to a protected area of the document." If I unlock the form in Word 02 I can run the macro just fine. The code looks like this (Form Code) Private Sub Calendar1_Click() ' Transfer date selected on calendar to the insertion ' insertion and close UserForm. Selection.Text = Format(Calendar1.Value, "m/d/yy") Selection.MoveRight Unit:=wdCharacter, Count:=1 Unload Me End Sub Private Sub cmdClose_Click() ' Close the form without entering a date if the user ' presses the [ESCAPE] key Unload Me End Sub Private Sub UserForm_Initialize() ' If selection is a date show the same date, otherwise show ' today's date. If IsDate(Selection.Text) Then Calendar1.Value = DateValue(Selection.Text) Else Calendar1.Value = Date End If End Sub The code looks like this (This Document Code) Sub OpenCalendar() ' Display the frmCalendar UserForm frmCalendar.Show End Sub Private Sub Document_Close() On Error Resume Next CustomizationContext = NormalTemplate ' Cancel the keyboard shortcut "Ctrl+Shift+C" KeyBindings.Key(KeyCode:=BuildKeyCode(wdKeyContro l, _ wdKeyShift, wdKeyC)).Clear ' Remove the "Insert Date" command from the right-click menu Application.CommandBars("Text").Controls("Inser t Date").Delete End Sub Private Sub Document_Open() Dim NewControl As CommandBarControl ' Assign the keyboard shortcut "Ctrl+Shift+C" CustomizationContext = NormalTemplate KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, _ wdKeyShift, wdKeyC), KeyCategory:=wdKeyCategoryMacro, _ Command:="ThisDocument.OpenCalendar" ' Add the "Insert Date" command to the right-click menu Set NewControl = Application.CommandBars("Text").Controls.Add With NewControl .Caption = "Insert Date" .OnAction = "ThisDocument.OpenCalendar" .BeginGroup = True End With End Sub Martin's website says it should work on most versions of Word. Maybe I need the form to unlock, launch the calendar object, and then re-lock? Or, maybe there is another way? Something I'm missing? If you have the time to take a look at this, I would be very appreciative. Jason K. |
#3
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
calendar Object
These two changes, did the trick.
Dim ActiveField As String ActiveField = Selection.Bookmarks(1).Name ActiveDocument.FormFields(ActiveField).Result = _ Format(Calendar1.Value, "m/d/yy") Dim ActiveField As String Dim CurrentEntry As String ActiveField = Selection.Bookmarks(1).Name CurrentEntry = ActiveDocument.FormFields(ActiveField).Result Thanks for your help! ================================================== === "Jay Freedman" wrote: Hi Jason, The code you found isn't intended to be used in a protected form, but to insert a date in regular text. The core of the problem is what the code tries to do with the result of the calendar: Selection.Text = Format(Calendar1.Value, "m/d/yy") isn't allowed in a protected form, because it's interpreted as an attempt to remove the form field. Instead, what you need is something like this, but using the real name of the field that receives the date: ActiveDocument.FormFields("SomeDate").Result = _ Format(Calendar1.Value, "m/d/yy") In fact, in a protected form you don't need a lot of the code here. Instead of adding a macro's name as a command to the right-click menu, assign the OpenCalendar macro as the Entry macro of the particular form field that should contain the date. Get rid of the Document_Open and Document_Close routines (which also are ill-advised because they alter Normal.dot, generally a bad idea). -- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org On Fri, 29 Dec 2006 11:02:00 -0800, Jason K. wrote: I'm trying to use the calendar object in an online form I created. The code I have was written by Martin Green (http://www.fontstuff.com/word/wordtut03a.htm). It works great on the computer I created the fields with (Word 03) but when I send the form to another computer (Word 02) I get an error message that says "This method or property is not available because the object refers to a protected area of the document." If I unlock the form in Word 02 I can run the macro just fine. The code looks like this (Form Code) Private Sub Calendar1_Click() ' Transfer date selected on calendar to the insertion ' insertion and close UserForm. Selection.Text = Format(Calendar1.Value, "m/d/yy") Selection.MoveRight Unit:=wdCharacter, Count:=1 Unload Me End Sub Private Sub cmdClose_Click() ' Close the form without entering a date if the user ' presses the [ESCAPE] key Unload Me End Sub Private Sub UserForm_Initialize() ' If selection is a date show the same date, otherwise show ' today's date. If IsDate(Selection.Text) Then Calendar1.Value = DateValue(Selection.Text) Else Calendar1.Value = Date End If End Sub The code looks like this (This Document Code) Sub OpenCalendar() ' Display the frmCalendar UserForm frmCalendar.Show End Sub Private Sub Document_Close() On Error Resume Next CustomizationContext = NormalTemplate ' Cancel the keyboard shortcut "Ctrl+Shift+C" KeyBindings.Key(KeyCode:=BuildKeyCode(wdKeyContro l, _ wdKeyShift, wdKeyC)).Clear ' Remove the "Insert Date" command from the right-click menu Application.CommandBars("Text").Controls("Inser t Date").Delete End Sub Private Sub Document_Open() Dim NewControl As CommandBarControl ' Assign the keyboard shortcut "Ctrl+Shift+C" CustomizationContext = NormalTemplate KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, _ wdKeyShift, wdKeyC), KeyCategory:=wdKeyCategoryMacro, _ Command:="ThisDocument.OpenCalendar" ' Add the "Insert Date" command to the right-click menu Set NewControl = Application.CommandBars("Text").Controls.Add With NewControl .Caption = "Insert Date" .OnAction = "ThisDocument.OpenCalendar" .BeginGroup = True End With End Sub Martin's website says it should work on most versions of Word. Maybe I need the form to unlock, launch the calendar object, and then re-lock? Or, maybe there is another way? Something I'm missing? If you have the time to take a look at this, I would be very appreciative. Jason K. |
Reply |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
calendar control | Microsoft Word Help | |||
I used a 2006 Word Calendar Template last year, it was calendar wi | New Users | |||
Calendar object | Microsoft Word Help | |||
Adding Border to an object moves the caption!?? | Microsoft Word Help | |||
MS Word Picture Object | Microsoft Word Help |