Command button problem


J

Jason Lepack

I have a document on which the requirement is to force users to enter
data in specific fields in the top half of the first page and the
header. The rest of the document must be freely editable.

I created form fields in the page and then bookmarks in the header. I
split the page into two sections and set allow only form entries in
the first section.

Now I can't modify the header because it's protected (which is what I
want, because I don't want users accidentally deleting the bookmarks,
things are too fragile...) so I created a command button to modify the
header code is below:

Private Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As
String)
On Error GoTo updBM_Err
Dim BMRange As Range, doc As Document
Set doc = ActiveDocument
doc.Unprotect "toughpassword"
Set BMRange = doc.Bookmarks(BookmarkToUpdate).Range
BMRange.Text = TextToUse
doc.Bookmarks.Add BookmarkToUpdate, BMRange
doc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True,
Password:="toughpassword"
Exit Sub
updBM_Err:
If Err.Number = 4605 Then
Resume Next
Else
MsgBox Err.Number & " - " & Err.Description
End If
End Sub

' Prompts the user to modify the header
' Called by command button
Public Sub modifyHeader()
Dim s As String
s = InputBox("Enter a Title for your Document", , " ")
If Not s = "" Then UpdateBookmark "Title", s
s = InputBox("Enter a Document Number", , " ")
If Not s = "" Then UpdateBookmark "DocNum", s
s = InputBox("Enter a Revision Number", , " ")
If Not s = "" Then UpdateBookmark "RevNum", s
End Sub

That worked great but the command button showed up on print.

So I found the method of inserting the button into a text box and
hiding the text box on print. That worked great too.

Now here's the current problem. When I click my button I get the
error at doc.protect:
Err.Number = 4641
Err.Description = The ToolsProtectDocument statement is currently
disabled

So with just the button, runs fine, button prints.
Button in text box, button doesn't print, document won't reprotect.

ANy help on this situation, including other methods, would be greatly
appreciated. I've created lots of Excel and Access solutions, but
this is my first Word Macro.

Thanks,
Jason Lepack
 
Ad

Advertisements

J

Jason Lepack

I have a document on which the requirement is to force users to enter
data in specific fields in the top half of the first page and the
header. The rest of the document must be freely editable.

I created form fields in the page and then bookmarks in the header. I
split the page into two sections and set allow only form entries in
the first section.

Now I can't modify the header because it's protected (which is what I
want, because I don't want users accidentally deleting the bookmarks,
things are too fragile...) so I created a command button to modify the
header code is below:

Private Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As
String)
On Error GoTo updBM_Err
Dim BMRange As Range, doc As Document
Set doc = ActiveDocument
doc.Unprotect "toughpassword"
Set BMRange = doc.Bookmarks(BookmarkToUpdate).Range
BMRange.Text = TextToUse
doc.Bookmarks.Add BookmarkToUpdate, BMRange
doc.ProtectType:=wdAllowOnlyFormFields, NoReset:=True,
Password:="toughpassword"
Exit Sub
updBM_Err:
If Err.Number = 4605 Then
Resume Next
Else
MsgBox Err.Number & " - " & Err.Description
End If
End Sub

' Prompts the user to modify the header
' Called by command button
Public Sub modifyHeader()
Dim s As String
s = InputBox("Enter a Title for your Document", , " ")
If Not s = "" Then UpdateBookmark "Title", s
s = InputBox("Enter a Document Number", , " ")
If Not s = "" Then UpdateBookmark "DocNum", s
s = InputBox("Enter a Revision Number", , " ")
If Not s = "" Then UpdateBookmark "RevNum", s
End Sub

That worked great but the command button showed up on print.

So I found the method of inserting the button into a text box and
hiding the text box on print. That worked great too.

Now here's the current problem. When I click my button I get the
error at doc.protect:
Err.Number = 4641
Err.Description = The ToolsProtectDocument statement is currently
disabled

So with just the button, runs fine, button prints.
Button in text box, button doesn't print, document won't reprotect.

ANy help on this situation, including other methods, would be greatly
appreciated. I've created lots of Excel and Access solutions, but
this is my first Word Macro.

Thanks,
Jason Lepack

Anyone?
 
Ad

Advertisements

T

Thom R

I know it's a little late to reply to this dead thread, but it came top of my google search when I was looking for solutions for the same issue (i.e. Tools Protect Document statement disabled when trying to use a command button nested in a text box).

The reason I wanted to put a command button in a text box in the first place was to enable me to hide it when printing the document, so this solution worked for my purposes, but may not for everyone.

INSTEAD OF USING A TEXT BOX, USE A FORM FRAME.

1. VIEW>TOOLBARS>FORMS

2. CLICK "INSERT FRAME" ON TOOLBAR

3. CUT & PASTE COMMAND BUTTON INTO FRAME

4. CTRL+A INSIDE FRAME

5. FORMAT>FONT

6. CHECK "HIDDEN"

7. VOILA! You now have a Command Button that is hidden from view when printing, but is still be usable in a protected form.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top