Allow formatting in form fields

S

Starbird

I have built a template that is protected to allow filling in form fields.
Is it possible to allow the user to select styles to apply to their text
when they fill in a text field? If so, how would I go about that?

Thanks in advance for any assistance!

SRD
 
J

Jean-Guy Marcil

Starbird was telling us:
Starbird nous racontait que :
I have built a template that is protected to allow filling in form
fields. Is it possible to allow the user to select styles to apply to
their text when they fill in a text field? If so, how would I go
about that?

Here is some code I have used to allow bold/italic/underlining in form
fields before. I built a toolbar and assign the Sel_Bold etc. to buttons on
the toolbar. I think you can easily adapt this to applying styles instead.
Just keep in mind that styles affect the whole paragraph, not only the user
selection.

'_______________________________________
Public myRange As Range
'_______________________________________
Sub Sel_Bold()

Sel_Unprotect

myRange.Bold = wdToggle

Sel_Protect

End Sub
'_______________________________________

'_______________________________________
Sub Sel_Italic()

Sel_Unprotect

myRange.Italic = wdToggle

Sel_Protect

End Sub
'_______________________________________

'_______________________________________
Sub Sel_Underline()

Sel_Unprotect

If myRange.Underline = wdUnderlineNone Then
myRange.Underline = wdUnderlineSingle
Else
myRange.Underline = wdUnderlineNone
End If

Sel_Protect

End Sub
'_______________________________________

'_______________________________________
Sub Sel_Unprotect()

If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If

Set myRange = Selection.Range

End Sub
'_______________________________________

'_______________________________________
Sub Sel_Protect()

If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, _
Password:=""
End If

myRange.Select

End Sub
'_______________________________________
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
S

Starbird

Thanks Jean, this looks like a good starting place. At least I know it is
possible if you have done it successfully, hopefully I can get it from here.

Thanks again,
SRD
 
S

Shalea

I am getting a syntax error when I use the code below. I have a protected
form where I need to allow the user to bold and italicize words. Any
suggestions?
 
S

Starbird

Hi Shalea, what I found when I used the code was that I had to also include
code to verify whether the style alredy existed or not. I use the following
function and no longer get ther error.

Function StyleExists(styleName As String) As Boolean
On Error GoTo StyleExists_Err
Dim blnStyleExists As Boolean

' Assume style doesn't exist when you begin.
blnStyleExists = False
ActiveDocument.styles.Add (styleName), Type:=wdStyleTypeCharacter

StyleExists_End:
StyleExists = blnStyleExists
Exit Function

StyleExists_Err:
Select Case Err.Number

Case 5173
' Error Number 5173 occurred, thus the style exists.
blnStyleExists = True
Case Else
' Handle unforseen circumstances.
End Select
Resume StyleExists_End
End Function

I hope this helps. It was a frustrating process to get this formatting
within a text field to work, but it works great now and the users are
extremely pleased. Hope you have as good of an outcome.

SRD
 

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