Hi sg,
Try:
Sub ShowHide()
Dim BmkNm As String
Dim NewTxt As String
Dim BmkRng As Range
Dim FFld As FormField
BmkNm = "MyBkMrk"
NewTxt = ", including mental health || and chemical ** dependency services"
Application.ScreenUpdating = False
With ActiveDocument
If .Bookmarks.Exists(BmkNm) Then
.Unprotect
Set BmkRng = .Bookmarks(BmkNm).Range
If .FormFields("Check9").Result = 0 Then
NewTxt = ""
For Each FFld In BmkRng.FormFields
FFld.Delete
Next
ElseIf BmkRng.Text <> "" Then
.Protect Type:=wdAllowOnlyFormFields, noreset:=True
End
End If
BmkRng.Text = NewTxt
.Bookmarks.Add BmkNm, BmkRng
If NewTxt <> "" Then
BmkRng.Select
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "||"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
Set FFld = ActiveDocument.FormFields _
.Add(Range:=Selection.Range, Type:=wdFieldFormTextInput)
With FFld
.Name = "Text98"
.TextInput.EditType Type:=wdNumberText, Default:="", Format:="$#,##0.00"
.TextInput.Width = 8
.Result = "$0.00"
End With
BmkRng.Select
With Selection.Find
.Text = "**"
.Execute
End With
Set FFld = ActiveDocument.FormFields _
.Add(Range:=Selection.Range, Type:=wdFieldFormTextInput)
With FFld
.Name = "Text99"
.TextInput.EditType Type:=wdNumberText, Default:="", Format:="0"
.TextInput.Width = 2
.Result = "0"
End With
.Bookmarks("Text98").Range.Select
End If
.Protect Type:=wdAllowOnlyFormFields, noreset:=True
End If
End With
Set BmkRng = Nothing
Application.ScreenUpdating = True
End Sub
Note: It is inadvisable to use 'Text1' as a document bookmark name when working with vba and formfields since, when a text
Formfield
is inserted, the 'Text1' bookmark name is assigned to it unless another Formfield is already using that name. Consequently, the
'Text1' bookmark name gets 'stolen' from wherever else you might have had it. Indeed, it's often best to give your formfields
meaningful bookmark names. Accordingly, I've used the 'MyBkMrk' document bookmark name instead. Again. I'd suggest using a
meaningful bookmark name.
As per you other request, the code selects the newly-inserted 'Text98' formfield, which I've given the $0.00 value.
--
Cheers
macropod
[Microsoft MVP - Word]
sg said:
I used the more simple code you suggested and it worked much better. Thanks!
One question so far on inserting the form field: I will probably have
several form fields that I need to insert in the string:
$0.00 for days 1 through 0 and $0.00 for day 0 and after
The first $0.00 needs to be a form field, so does the 0 and the second $0.00
and the second 0.
Is this possible?
Also, I couldn't figure out (with my limited knowledge) how to format the
form field for currency. I will also need to format some of them as a whole
number.
Thanks in advance - you're going beyond and above what help I expected to
get! I really need to learn this stuff myself...
:
Hi sg,
OK, the macro runs for me without having to unprotect the document. Your use of the 'Text1' bookmark suggests you're updating
a
formfield with the 'Text1' bookmark, not just a bookmark. In that case, your code could be simplified to:
Sub ShowHide()
Dim BmkNm As String
Dim NewTxt As String
BmkNm = "Text1"
NewTxt = ", including mental health and chemical dependency services"
With ActiveDocument
If .FormFields("Check9").Result = 0 Then NewTxt = ""
.FormFields(BmkNm).Result = NewTxt
End With
End Sub
As for the formfield insertion issue, what you'll need to do in code is something along the lines of:
1. add to the text string inserted by the code, a unique character string for each formfield that you need to insert.
2. insert the text string
3. replace each unique character string with the corresponding formfield.
For that, you could use code like:
Sub ShowHide()
Dim BmkNm As String
Dim NewTxt As String
Dim BmkRng As Range
Dim FFld As FormField
BmkNm = "Text1"
NewTxt = ", including mental health || and chemical dependency services"
With ActiveDocument
If .FormFields("Check1").Result = 0 Then NewTxt = ""
If .Bookmarks.Exists(BmkNm) Then
.Unprotect
Set BmkRng = .Bookmarks(BmkNm).Range
BmkRng.Text = NewTxt
.Bookmarks.Add BmkNm, BmkRng
BmkRng.Select
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "||"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
Set FFld = ActiveDocument.FormFields _
.Add(Range:=Selection.Range, Type:=wdFieldFormTextInput)
With FFld
.Name = "Text99"
.Result = "Default Text"
End With
.Protect Type:=wdAllowOnlyFormFields, noreset:=True
End If
End With
Set BmkRng = Nothing
End Sub
This assumes 'Text1' is an ordinary bookmark, not a formfield bookmark, and the unique string to identify where the new
formfield
is
to go is '||'. The inserted formfield is given the 'Text99' bookmark name and a 'Default Text' content - you can omit these
and/or
modify other formfield properties if you like.
--
Cheers
macropod
[Microsoft MVP - Word]
When I tried to run the macro as you sent it to me, it told me it couldn't
run on a protected document. I don't know much about VB, so I just pulled
the protect and unprotect part from another macro I have and then it worked,
so I left it.
You were right about the checkbox - ok there now.
Can I bother you with another question?
In this same document, I might have a checkbox and when it is checked, I
want the text to stay in, but part of what needs to stay in is a text form
field that the user needs to fill in. Something like:
$0.00 for days 1 through 0 and $0.00 for day 0 and after
Where the $0.00 both need to be text fields that the user can complete as
well as filling in the 0 values. They will only need to do this if the
checkbox that is right before this text needs to be included in the document.
Is this possible?
:
Hi sg,
Why are you unprotecting & reprotecting the document - that shouldn't be necessary?
The only reason I can think of for your checkbox disappearing is that your bookmarked range includes the checkbox. Unless
you
want
the checkbox to disappear - and we've already discussed why it shouldn't, make sure your bookmarked range does not include
the
checkbox.
--
Cheers
macropod
[Microsoft MVP - Word]
I forgot about the Tools|Options|View setting. That took care of that
problem.
I am still having one issue. When I tab through the checkbox, it
disappears, whether its checked or not. The text stays or goes as it should,
but I thought the checkbox was also supposed to stay put so they could change
their mind.
The macro that I have is:
Sub ShowHide()
ActiveDocument.Unprotect
Dim BmkNm As String
Dim NewTxt As String
Dim BmkRng As Range
BmkNm = "Text1"
NewTxt = ", including mental health and chemical dependency services"
With ActiveDocument
If .FormFields("Check9").Result = 0 Then NewTxt = ""
If .Bookmarks.Exists(BmkNm) Then
Set BmkRng = .Bookmarks(BmkNm).Range
BmkRng.Text = NewTxt
.Bookmarks.Add BmkNm, BmkRng
End If
End With
Set BmkRng = Nothing
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True
End Sub
I don't think I changed anything to make this happen. I appreciate all your
help on this - now if we can just get this one last thing...
:
Hi sg,
Go to Tools|Options|View and make sure the 'Hidden text' option is checked and to Tools|Options|Print and make sure the
'Hidden
text' option is unchecked
--
Cheers
macropod
[Microsoft MVP - Word]
Thanks for your help.
The macro works great, however, I am having a problem with hiding the
checkbox as you suggested. When I hide the checkbox (Format, text, check the
hidden option), I don't see the checkbox, even when the form is protected.
The only way I can see it is if I click on the Show/Hide button. Am I
missing something here?
:
Hi sg,
You could leave the checkbox on the page, but simply have its formatting set to 'hidden' text. That way, the checkbox
is
always
available and only ever appears in the printout if the 'hidden text' print option is checked.
As for the text itself, you could place a bookmark in the document and simply update the bookmark according to the
state
of
the
checkbox. To do that, you could add the following macro to a standard vba module in the document and select it as the
'on
exit'
macro to run from your checkbox formfield:
Sub UpdateBookMark()
Dim BmkNm As String
Dim NewTxt As String
Dim BmkRng As Range
BmkNm = "MyBookmarkName"
NewTxt = "Text To Show"
With ActiveDocument