Problem running macro from QAT

Z

zxcv

Hi,
I have a form with several text formfields. I'm trying to format the
contents of certain formfields based on user input of other formfields. For
example, if a user enters the number "1" in Text1, I may want the contents of
another formfield, say Text2, to be formatted as bold. My code is as follows:

If doc.FormFields("Text1").Result = 1 Then
doc.FormFields("Text2").Select
Selection.Font.Bold = True
End If

The funny thing is, the code works when I trigger it from within Text1 as a
"run macro on exit". But if I try to run the macro by assigning it to a QAT
button, I get the following error:

Run-time error '4605':
This method or property is not available because the object refers to a
protected area of the document.

I'm confused by this error message because the selected object is a
formfield, which is *not* a protected area of the document. I'm further
confused because, as I said before, the code works as an "run on exit" macro.

I'd appreciate it if anyone could shed some light on this for me.
Jack
 
D

Doug Robbins - Word MVP on news.microsoft.com

You will have to have the code in the macro unprotect the document, do the
formatting and then re-protect the document.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
G

Graham Mayor

Some actions cannot be run without unprotecting the form. The form field
itself may be 'unprotected', but it is nevertheless in a protected area of
the form in order to function as a form field. You therefore need something
like

Dim i As Integer
Dim bProtected As Boolean

'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""

End If
With ActiveDocument
If .FormFields("Text1").Result = 1 Then
.FormFields("Text2").Range.Bold = True
Else
.FormFields("Text2").Range.Bold = False
End If
End With

'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End If


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
Z

zxcv

Thanks for your very helpful replies -- esp. your "fleshed out" response
Graham! ;-) I didn't realize that you could programmatically
unprotect/reprotect a form. I'm still left wondering why the macro worked
when triggered as an "on exit" from the formfield but not when triggered from
a QAT button. I guess it's just one of those quaint Word quirks!
 

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