PDA

View Full Version : How to disable commands



francozola25
06-27-2008, 05:04 AM
Hi is there a way that i can disable the insert Text Form Field. I don't want my users to be able to go View, Toolbars, Forms insert Formfield.

I only want them to be to do this through a macro.

CreganTur
06-27-2008, 05:10 AM
You can accomplish this by protecting the document. When a document is protected no changes can be made.

Take a look at this awesome example that gerry created- he uses bookmarks instead of formfields, as do I- there's really no reason to use formfields if you aren't going to allow your users the option of manually filling them in.

Notice that the doc is protected and that you cannot make changes to it. When the UserForm code runs it first unprotects the document, makes the changes to the bookmark ranges, and then reprotects the document.

fumei
06-27-2008, 11:13 AM
"Hi is there a way that i can disable the insert Text Form Field. I don't want my users to be able to go View, Toolbars, Forms insert Formfield.

I only want them to be to do this through a macro."

May I ask why? I am interested because I am opposed to any restrictions on users unless absolutely required. What is so important that requires such a restriction? What is in your macro that makes it better? How would they start this macro? What are the circumstances in which they would use it? Does it have good error trapping to ensure the conditions you wish to restrict it for, are in fact applicable? What exactly does your macro (to replace inserting a formfield) actually do?

To answer your subject line - How to disable commands - you do not really. What you do is override them.

They are quite fine-grained.

Sub TextFormField()

is the actual Sub that inserts a text formfield. In other words, you could use:Sub TextFormField()
Msgbox "Text formfield are not permitted."
End Sub

and no text formfield would, or could, be inserted. This would NOT affect Sub CheckboxFormField(), which is the actual Sub that inserts a checkbox formfield.

Just to be clear, most Word "commands" are, in fact, Subs. Actual sub-routines. Writing a Sub with the same name overrides the internal Word Sub. A Sub with the same name applies to ONLY the document/template it is in.

To be clear. If document yadda.doc has:

Sub TextFormField()
MsgBox "No way, Jose."
End Sub

and you also have document blahblah.doc open (and it does NOT have a written Sub TextFormField), then trying to insert a text formfield in yadda.doc would get the message (and no formfield), but trying in blahblah.doc would insert a text formfield.

To make it global, the written Sub would have to be in Normal.dot.

However, again, if you would, please explain WHY this is desired.

francozola25
06-27-2008, 02:13 PM
The reason i have this is because i want to update the Field Codes in my document

What happens is that when i click View - Toolbars - Forms - Insert Text formfield.

What happens when you display the field codes (ALT-F9) is {FORMTEXT}. I need to change this to the following {Title} to make sure my macro down below updates the enter values in the userform.

I was hoping to set up a macro that willl click the View-Toolbars-Forms-Insert Text Formfield-press ALT+F9 Replace FORMTEXT with the word Title.

I want to hide/disable the Forms toolbar. I am worried that my users with Insert the Text Formfield and leave the value as FORMTEXT. I don't mind replacing the Normal.dot

I realise that this could of been done easier by CreganTur post but i have already have {Title} in 250 documents


Sub Update()
Dim Title As String
Dim frmTitle As UserForm1
Dim oStory As Range
'initialise the UserForm1
Set frmTitle = New UserForm1
'Display the Userfrom and declare Title as the TextFormField
With frmTitle
.Show
ActiveDocument.BuiltInDocumentProperties("Title").Value = .Title.Text
End With
Unload frmTitle
'Release object references.
Set frmTitle = Nothing
'Call Macro to update all values with Title of the FormField
Call UpdateStoryRanges(ActiveDocument)
End Sub


Sub UpdateStoryRanges(CurrentDoc As Document)
'Loop to update the main body of the document
For Each oStory In CurrentDoc.StoryRanges
'Update the all the FormFields
oStory.Fields.Update
If oStory.StoryType < wdMainTextStory Then
'Loop through main body of document until all fields are updated
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory
Set oStory = Nothing
End Sub

fumei
07-06-2008, 03:19 AM
You are confusing things.

A formfield is NOT the same as a BuiltInDocumentProperties - your "Title".

Two different beasts all together. There is, in fact, no relation.

FORMTEXT does indeed display with Alt-F9, but its value is what ever is its value, i.e. what ever was input as text into the formfield.

BuiltInDocumentProperties are something quite different. They are "true" fields, although formfields are fields. However, formfields can not be entered via Insert > Fields. BuiltInDocumentProperties can. Like I said, two very different beasts. Your code: 'Update the all the FormFields
oStory.Fields.Update

is incorrect. The code does update fields, but does NOT "update " formfields, as they are updated when ever focus is in them, them leaves them.

"I am worried that my users with Insert the Text Formfield and leave the value as FORMTEXT"

No matter what you enter as text into a text formfield - "Jack and the Bean Stalk", "Yogi Bear", "Bambi" - it will always shows as FORMTEXT with an Alt-F9. So, your comment:

"I need to change this to the following {Title} to make sure my macro down below updates the enter values in the userform."

is impossible. If it IS a text formfield, then it IS a text formfield, and it will always be FORMTEXT with Alt-F9.

If you want to use the {Title} field, then use the Title field...as a field.

May I ask if you are actually getting a value for frmTitle.Title.Text?
With frmTitle
.Show
ActiveDocument.BuiltInDocumentProperties("Title").Value = .Title.Text
End With



.Title.Text is - at least as written - is indicating a property (.Title) of frmTitle.

frmTitle is set as a userform.

Userforms do NOT have a .Title property.

Are you using Option Explicit? Likely not, as .Title.Text should return you a complie error.

"Method of data member not found."

Because, again, a userform does not have a Title property.

francozola25
07-21-2008, 02:43 AM
Apologies for this fumei

You are correct, i thought insert FORMFIELD and insert Field inbuild document properties were the same.

Could i ask while still on this thread, i have been looking at Gerrys example of bookmarks to automatcially enter values using the insert box. I have tried this with mine but am having some diffculties.

I have a TextBox2 on my UserForm1 and i am calling CallFillABookmark("here", TextBox2) within my code


Set bDoc = Documents.Open("\\brymyserver\Forms\Web Forms\" & ExcelFN & ".Doc", ReadOnly:=True, Visible:=False)
bDoc.BuiltInDocumentProperties("Title") = aDoc.BuiltInDocumentProperties("Title")
Call UpdateStoryRanges(bDoc)
Call FillABookmark("here", TextBox2)



Sub FillABookmark(strBM As String, strText As String)
Dim oRange As Word.Range
Set oRange = ActiveDocument.Bookmarks(strBM).Range
ActiveDocument.Bookmarks(strBM).Range.Text = strText
With oRange
.Collapse Direction:=wdCollapseEnd
.MoveEnd Unit:=wdCharacter, Count:=Len(strText)
End With
ActiveDocument.Bookmarks.Add strBM, Range:=oRange
End Sub


I keep getting an error 5941--The requested member of the collection does not exist.

I have inserted a bookmark named here in the form already prior to opening.

francozola25
07-21-2008, 05:50 AM
i am going to close this post and start a new one as it is a different topic.