Checkbox to delete text field

S

sg

I have a form shows a checkbox followed by a text field. If the checkbox is
checked, I would like to keep the text in the text field but get rid of the
checkbox. If the checkbox is not checked, I want to delete the text in the
text field and delete the checkbox.

If possible, an even better solution would be to not have the text in the
text field, but rather just text in the document itself. However, I wasn't
sure this would be possible since the text is part of a paragraph and cannot
be separated into a section without appearing on a new line (at least I
didn't know how to do this). I figured at least the textbox would be
something Word could recognize and delete depending on whether the checkbox
is checked or not.

Any suggestions? Thanks in advance!
 
M

macropod

Hi sg,

While what you're asking is doable, it's poor design. After all, what happens if the user makes a mistake? As specified, there's no
way to correct it. Such a mistake could be as simple as tabbing through the checkbox formfield without intentionally doing anything.
 
S

sg

Do you have any suggestions of a better design? I'm fairly new to this...as
you can probably tell.

macropod said:
Hi sg,

While what you're asking is doable, it's poor design. After all, what happens if the user makes a mistake? As specified, there's no
way to correct it. Such a mistake could be as simple as tabbing through the checkbox formfield without intentionally doing anything.

--
Cheers
macropod
[Microsoft MVP - Word]


sg said:
I have a form shows a checkbox followed by a text field. If the checkbox is
checked, I would like to keep the text in the text field but get rid of the
checkbox. If the checkbox is not checked, I want to delete the text in the
text field and delete the checkbox.

If possible, an even better solution would be to not have the text in the
text field, but rather just text in the document itself. However, I wasn't
sure this would be possible since the text is part of a paragraph and cannot
be separated into a section without appearing on a new line (at least I
didn't know how to do this). I figured at least the textbox would be
something Word could recognize and delete depending on whether the checkbox
is checked or not.

Any suggestions? Thanks in advance!
 
M

macropod

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
If .FormFields("Check1").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
End Sub

The above macro assumes you're testing the state of a checkbox whose bookmark name is "Check1" and that you want to update a
bookmark named "MyBookmarkName" with the text "Text To Show". Simply change those details to suit your needs. Once the document is
protected for forms, everything should function as outlined.

--
Cheers
macropod
[Microsoft MVP - Word]


sg said:
Do you have any suggestions of a better design? I'm fairly new to this...as
you can probably tell.

macropod said:
Hi sg,

While what you're asking is doable, it's poor design. After all, what happens if the user makes a mistake? As specified, there's
no
way to correct it. Such a mistake could be as simple as tabbing through the checkbox formfield without intentionally doing
anything.

--
Cheers
macropod
[Microsoft MVP - Word]


sg said:
I have a form shows a checkbox followed by a text field. If the checkbox is
checked, I would like to keep the text in the text field but get rid of the
checkbox. If the checkbox is not checked, I want to delete the text in the
text field and delete the checkbox.

If possible, an even better solution would be to not have the text in the
text field, but rather just text in the document itself. However, I wasn't
sure this would be possible since the text is part of a paragraph and cannot
be separated into a section without appearing on a new line (at least I
didn't know how to do this). I figured at least the textbox would be
something Word could recognize and delete depending on whether the checkbox
is checked or not.

Any suggestions? Thanks in advance!
 
S

sg

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?

macropod said:
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
If .FormFields("Check1").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
End Sub

The above macro assumes you're testing the state of a checkbox whose bookmark name is "Check1" and that you want to update a
bookmark named "MyBookmarkName" with the text "Text To Show". Simply change those details to suit your needs. Once the document is
protected for forms, everything should function as outlined.

--
Cheers
macropod
[Microsoft MVP - Word]


sg said:
Do you have any suggestions of a better design? I'm fairly new to this...as
you can probably tell.

macropod said:
Hi sg,

While what you're asking is doable, it's poor design. After all, what happens if the user makes a mistake? As specified, there's
no
way to correct it. Such a mistake could be as simple as tabbing through the checkbox formfield without intentionally doing
anything.

--
Cheers
macropod
[Microsoft MVP - Word]


I have a form shows a checkbox followed by a text field. If the checkbox is
checked, I would like to keep the text in the text field but get rid of the
checkbox. If the checkbox is not checked, I want to delete the text in the
text field and delete the checkbox.

If possible, an even better solution would be to not have the text in the
text field, but rather just text in the document itself. However, I wasn't
sure this would be possible since the text is part of a paragraph and cannot
be separated into a section without appearing on a new line (at least I
didn't know how to do this). I figured at least the textbox would be
something Word could recognize and delete depending on whether the checkbox
is checked or not.

Any suggestions? Thanks in advance!
 
M

macropod

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]


sg said:
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?

macropod said:
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
If .FormFields("Check1").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
End Sub

The above macro assumes you're testing the state of a checkbox whose bookmark name is "Check1" and that you want to update a
bookmark named "MyBookmarkName" with the text "Text To Show". Simply change those details to suit your needs. Once the document
is
protected for forms, everything should function as outlined.

--
Cheers
macropod
[Microsoft MVP - Word]


sg said:
Do you have any suggestions of a better design? I'm fairly new to this...as
you can probably tell.

:

Hi sg,

While what you're asking is doable, it's poor design. After all, what happens if the user makes a mistake? As specified,
there's
no
way to correct it. Such a mistake could be as simple as tabbing through the checkbox formfield without intentionally doing
anything.

--
Cheers
macropod
[Microsoft MVP - Word]


I have a form shows a checkbox followed by a text field. If the checkbox is
checked, I would like to keep the text in the text field but get rid of the
checkbox. If the checkbox is not checked, I want to delete the text in the
text field and delete the checkbox.

If possible, an even better solution would be to not have the text in the
text field, but rather just text in the document itself. However, I wasn't
sure this would be possible since the text is part of a paragraph and cannot
be separated into a section without appearing on a new line (at least I
didn't know how to do this). I figured at least the textbox would be
something Word could recognize and delete depending on whether the checkbox
is checked or not.

Any suggestions? Thanks in advance!
 
S

sg

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...

macropod said:
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]


sg said:
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?

macropod said:
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
If .FormFields("Check1").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
End Sub

The above macro assumes you're testing the state of a checkbox whose bookmark name is "Check1" and that you want to update a
bookmark named "MyBookmarkName" with the text "Text To Show". Simply change those details to suit your needs. Once the document
is
protected for forms, everything should function as outlined.

--
Cheers
macropod
[Microsoft MVP - Word]


Do you have any suggestions of a better design? I'm fairly new to this...as
you can probably tell.

:

Hi sg,

While what you're asking is doable, it's poor design. After all, what happens if the user makes a mistake? As specified,
there's
no
way to correct it. Such a mistake could be as simple as tabbing through the checkbox formfield without intentionally doing
anything.

--
Cheers
macropod
[Microsoft MVP - Word]


I have a form shows a checkbox followed by a text field. If the checkbox is
checked, I would like to keep the text in the text field but get rid of the
checkbox. If the checkbox is not checked, I want to delete the text in the
text field and delete the checkbox.

If possible, an even better solution would be to not have the text in the
text field, but rather just text in the document itself. However, I wasn't
sure this would be possible since the text is part of a paragraph and cannot
be separated into a section without appearing on a new line (at least I
didn't know how to do this). I figured at least the textbox would be
something Word could recognize and delete depending on whether the checkbox
is checked or not.

Any suggestions? Thanks in advance!
 
M

macropod

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]


sg said:
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...

macropod said:
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]


sg said:
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
If .FormFields("Check1").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
End Sub

The above macro assumes you're testing the state of a checkbox whose bookmark name is "Check1" and that you want to update a
bookmark named "MyBookmarkName" with the text "Text To Show". Simply change those details to suit your needs. Once the
document
is
protected for forms, everything should function as outlined.

--
Cheers
macropod
[Microsoft MVP - Word]


Do you have any suggestions of a better design? I'm fairly new to this...as
you can probably tell.

:

Hi sg,

While what you're asking is doable, it's poor design. After all, what happens if the user makes a mistake? As specified,
there's
no
way to correct it. Such a mistake could be as simple as tabbing through the checkbox formfield without intentionally doing
anything.

--
Cheers
macropod
[Microsoft MVP - Word]


I have a form shows a checkbox followed by a text field. If the checkbox is
checked, I would like to keep the text in the text field but get rid of the
checkbox. If the checkbox is not checked, I want to delete the text in the
text field and delete the checkbox.

If possible, an even better solution would be to not have the text in the
text field, but rather just text in the document itself. However, I wasn't
sure this would be possible since the text is part of a paragraph and cannot
be separated into a section without appearing on a new line (at least I
didn't know how to do this). I figured at least the textbox would be
something Word could recognize and delete depending on whether the checkbox
is checked or not.

Any suggestions? Thanks in advance!
 
S

sg

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?

macropod said:
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]


sg said:
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...

macropod said:
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
If .FormFields("Check1").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
End Sub

The above macro assumes you're testing the state of a checkbox whose bookmark name is "Check1" and that you want to update a
bookmark named "MyBookmarkName" with the text "Text To Show". Simply change those details to suit your needs. Once the
document
is
protected for forms, everything should function as outlined.

--
Cheers
macropod
[Microsoft MVP - Word]


Do you have any suggestions of a better design? I'm fairly new to this...as
you can probably tell.

:

Hi sg,

While what you're asking is doable, it's poor design. After all, what happens if the user makes a mistake? As specified,
there's
no
way to correct it. Such a mistake could be as simple as tabbing through the checkbox formfield without intentionally doing
anything.

--
Cheers
macropod
[Microsoft MVP - Word]


I have a form shows a checkbox followed by a text field. If the checkbox is
checked, I would like to keep the text in the text field but get rid of the
checkbox. If the checkbox is not checked, I want to delete the text in the
text field and delete the checkbox.

If possible, an even better solution would be to not have the text in the
text field, but rather just text in the document itself. However, I wasn't
sure this would be possible since the text is part of a paragraph and cannot
be separated into a section without appearing on a new line (at least I
didn't know how to do this). I figured at least the textbox would be
something Word could recognize and delete depending on whether the checkbox
is checked or not.

Any suggestions? Thanks in advance!
 
M

macropod

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]


sg said:
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?

macropod said:
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]


sg said:
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
If .FormFields("Check1").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
End Sub

The above macro assumes you're testing the state of a checkbox whose bookmark name is "Check1" and that you want to update
a
bookmark named "MyBookmarkName" with the text "Text To Show". Simply change those details to suit your needs. Once the
document
is
protected for forms, everything should function as outlined.

--
Cheers
macropod
[Microsoft MVP - Word]


Do you have any suggestions of a better design? I'm fairly new to this...as
you can probably tell.

:

Hi sg,

While what you're asking is doable, it's poor design. After all, what happens if the user makes a mistake? As specified,
there's
no
way to correct it. Such a mistake could be as simple as tabbing through the checkbox formfield without intentionally
doing
anything.

--
Cheers
macropod
[Microsoft MVP - Word]


I have a form shows a checkbox followed by a text field. If the checkbox is
checked, I would like to keep the text in the text field but get rid of the
checkbox. If the checkbox is not checked, I want to delete the text in the
text field and delete the checkbox.

If possible, an even better solution would be to not have the text in the
text field, but rather just text in the document itself. However, I wasn't
sure this would be possible since the text is part of a paragraph and cannot
be separated into a section without appearing on a new line (at least I
didn't know how to do this). I figured at least the textbox would be
something Word could recognize and delete depending on whether the checkbox
is checked or not.

Any suggestions? Thanks in advance!
 
S

sg

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...

macropod said:
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]


sg said:
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?

macropod said:
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
If .FormFields("Check1").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
End Sub

The above macro assumes you're testing the state of a checkbox whose bookmark name is "Check1" and that you want to update
a
bookmark named "MyBookmarkName" with the text "Text To Show". Simply change those details to suit your needs. Once the
document
is
protected for forms, everything should function as outlined.

--
Cheers
macropod
[Microsoft MVP - Word]


Do you have any suggestions of a better design? I'm fairly new to this...as
you can probably tell.

:

Hi sg,

While what you're asking is doable, it's poor design. After all, what happens if the user makes a mistake? As specified,
there's
no
way to correct it. Such a mistake could be as simple as tabbing through the checkbox formfield without intentionally
doing
anything.

--
Cheers
macropod
[Microsoft MVP - Word]


I have a form shows a checkbox followed by a text field. If the checkbox is
checked, I would like to keep the text in the text field but get rid of the
checkbox. If the checkbox is not checked, I want to delete the text in the
text field and delete the checkbox.

If possible, an even better solution would be to not have the text in the
text field, but rather just text in the document itself. However, I wasn't
sure this would be possible since the text is part of a paragraph and cannot
be separated into a section without appearing on a new line (at least I
didn't know how to do this). I figured at least the textbox would be
something Word could recognize and delete depending on whether the checkbox
is checked or not.

Any suggestions? Thanks in advance!
 
S

sg

I have run into one more problem when inserting the form field. When I check
the checkbox and tab out of it, the form field is inserted, however, the
formfield is not selected for the user to fill it in. It goes to the next
form field on the form. I can use a Shift-Tab to go backwards - can I make
it go to the new form field instead?

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...

macropod said:
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]


sg said:
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
If .FormFields("Check1").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
End Sub

The above macro assumes you're testing the state of a checkbox whose bookmark name is "Check1" and that you want to update
a
bookmark named "MyBookmarkName" with the text "Text To Show". Simply change those details to suit your needs. Once the
document
is
protected for forms, everything should function as outlined.

--
Cheers
macropod
[Microsoft MVP - Word]


Do you have any suggestions of a better design? I'm fairly new to this...as
you can probably tell.

:

Hi sg,

While what you're asking is doable, it's poor design. After all, what happens if the user makes a mistake? As specified,
there's
no
way to correct it. Such a mistake could be as simple as tabbing through the checkbox formfield without intentionally
doing
anything.

--
Cheers
macropod
[Microsoft MVP - Word]


I have a form shows a checkbox followed by a text field. If the checkbox is
checked, I would like to keep the text in the text field but get rid of the
checkbox. If the checkbox is not checked, I want to delete the text in the
text field and delete the checkbox.

If possible, an even better solution would be to not have the text in the
text field, but rather just text in the document itself. However, I wasn't
sure this would be possible since the text is part of a paragraph and cannot
be separated into a section without appearing on a new line (at least I
didn't know how to do this). I figured at least the textbox would be
something Word could recognize and delete depending on whether the checkbox
is checked or not.

Any suggestions? Thanks in advance!
 
M

macropod

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...

macropod said:
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]


sg said:
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
If .FormFields("Check1").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
End Sub

The above macro assumes you're testing the state of a checkbox whose bookmark name is "Check1" and that you want to
update
a
bookmark named "MyBookmarkName" with the text "Text To Show". Simply change those details to suit your needs. Once the
document
is
protected for forms, everything should function as outlined.

--
Cheers
macropod
[Microsoft MVP - Word]


Do you have any suggestions of a better design? I'm fairly new to this...as
you can probably tell.

:

Hi sg,

While what you're asking is doable, it's poor design. After all, what happens if the user makes a mistake? As
specified,
there's
no
way to correct it. Such a mistake could be as simple as tabbing through the checkbox formfield without intentionally
doing
anything.

--
Cheers
macropod
[Microsoft MVP - Word]


I have a form shows a checkbox followed by a text field. If the checkbox is
checked, I would like to keep the text in the text field but get rid of the
checkbox. If the checkbox is not checked, I want to delete the text in the
text field and delete the checkbox.

If possible, an even better solution would be to not have the text in the
text field, but rather just text in the document itself. However, I wasn't
sure this would be possible since the text is part of a paragraph and cannot
be separated into a section without appearing on a new line (at least I
didn't know how to do this). I figured at least the textbox would be
something Word could recognize and delete depending on whether the checkbox
is checked or not.

Any suggestions? Thanks in advance!
 
S

sg

I must be doing something wrong, but I'm not sure what. I copied your code
exactly, changed the bookmark name but when I check the box nothing happens.

The only way I can get it to work is if I leave the checkbox unchecked, tab
past it to the next field, then go back and check the box. When I do that,
the text and form fields pop in and the field is selected as it should be.

Any ideas?

macropod said:
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...

macropod said:
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
 
M

macropod

Hi sg,

Do you have any other macros that might be interfering with the process? The code certainly works for me.

--
Cheers
macropod
[Microsoft MVP - Word]


sg said:
I must be doing something wrong, but I'm not sure what. I copied your code
exactly, changed the bookmark name but when I check the box nothing happens.

The only way I can get it to work is if I leave the checkbox unchecked, tab
past it to the next field, then go back and check the box. When I do that,
the text and form fields pop in and the field is selected as it should be.

Any ideas?

macropod said:
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
 
S

sg

Not sure about whether another macro is interfering with it, but I found away
to make it work and it will be fine.

You've done so much to help me already - I hate to ask another question...

If you don't want to answer this one, I will post a new question.

In one place in this document, I will have several checkboxes that are
followed by text. Depending on the user filling out the form, they could
select one checkbox or several of them, ultimately making a list of items.

How do I add the word "and" inbetween the last item and the next to last
item on the list but not have it show up if the person only uses one of the
items?

Again, thank you so much for all your help - you are a lifesaver!

macropod said:
Hi sg,

Do you have any other macros that might be interfering with the process? The code certainly works for me.

--
Cheers
macropod
[Microsoft MVP - Word]


sg said:
I must be doing something wrong, but I'm not sure what. I copied your code
exactly, changed the bookmark name but when I check the box nothing happens.

The only way I can get it to work is if I leave the checkbox unchecked, tab
past it to the next field, then go back and check the box. When I do that,
the text and form fields pop in and the field is selected as it should be.

Any ideas?

macropod said:
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]


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
 
G

Greg Maxey

It might be easier to simply build your string and then clean it up.

E.g., - for each checked item add "item name, " to the string. So if A B
and D are checked the string would look like this:

A, B, D,

Then clean it up:

Sub Scratchmacro()
Dim pStrTest As String
pStrTest = "A, B, D, "
'Clean up the string text.
If Right(pStrTest, 2) = ", " Then pStrTest = Left(pStrTest, Len(pStrTest) -
2) & "."
On Error Resume Next
pStrTest = Left(pStrTest, InStrRev(pStrTest, ",") - 1) & " and" &
Mid(pStrTest, InStrRev(pStrTest, ",") + 1)
MsgBox pStrTest
End Sub
Not sure about whether another macro is interfering with it, but I
found away to make it work and it will be fine.

You've done so much to help me already - I hate to ask another
question...

If you don't want to answer this one, I will post a new question.

In one place in this document, I will have several checkboxes that are
followed by text. Depending on the user filling out the form, they
could select one checkbox or several of them, ultimately making a
list of items.

How do I add the word "and" inbetween the last item and the next to
last item on the list but not have it show up if the person only uses
one of the items?

Again, thank you so much for all your help - you are a lifesaver!

macropod said:
Hi sg,

Do you have any other macros that might be interfering with the
process? The code certainly works for me.

--
Cheers
macropod
[Microsoft MVP - Word]


sg said:
I must be doing something wrong, but I'm not sure what. I copied
your code exactly, changed the bookmark name but when I check the
box nothing happens.

The only way I can get it to work is if I leave the checkbox
unchecked, tab past it to the next field, then go back and check
the box. When I do that, the text and form fields pop in and the
field is selected as it should be.

Any ideas?

:

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]


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
 
S

sg

Thanks for your reply, Greg.

I'm sorry it has taken me so long to reply. I will try what you suggested,
butI have one question - sorry if its pretty elementary, I'm very knew to
using the code to make these things happen.

When I am putting the string together, how do I tell it which strings to
use?

Greg Maxey said:
It might be easier to simply build your string and then clean it up.

E.g., - for each checked item add "item name, " to the string. So if A B
and D are checked the string would look like this:

A, B, D,

Then clean it up:

Sub Scratchmacro()
Dim pStrTest As String
pStrTest = "A, B, D, "
'Clean up the string text.
If Right(pStrTest, 2) = ", " Then pStrTest = Left(pStrTest, Len(pStrTest) -
2) & "."
On Error Resume Next
pStrTest = Left(pStrTest, InStrRev(pStrTest, ",") - 1) & " and" &
Mid(pStrTest, InStrRev(pStrTest, ",") + 1)
MsgBox pStrTest
End Sub
Not sure about whether another macro is interfering with it, but I
found away to make it work and it will be fine.

You've done so much to help me already - I hate to ask another
question...

If you don't want to answer this one, I will post a new question.

In one place in this document, I will have several checkboxes that are
followed by text. Depending on the user filling out the form, they
could select one checkbox or several of them, ultimately making a
list of items.

How do I add the word "and" inbetween the last item and the next to
last item on the list but not have it show up if the person only uses
one of the items?

Again, thank you so much for all your help - you are a lifesaver!

macropod said:
Hi sg,

Do you have any other macros that might be interfering with the
process? The code certainly works for me.

--
Cheers
macropod
[Microsoft MVP - Word]


I must be doing something wrong, but I'm not sure what. I copied
your code exactly, changed the bookmark name but when I check the
box nothing happens.

The only way I can get it to work is if I leave the checkbox
unchecked, tab past it to the next field, then go back and check
the box. When I do that, the text and form fields pop in and the
field is selected as it should be.

Any ideas?

:

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]


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?
 
G

Greg Maxey

You would have to link your formfield checkbox to some text value that
serves as its caption. For example

CB1 Cat CB2 Dog CB3 Bird CB4 Fish

might be the first paragraph of your document. You have added text to serve
as the checkbox captions. You could bookmark the text to link it to its CB
e.g, bookmark "Cat" as "Check1_CAP", bookmark "Dog" as "Check2_CAP"

Then use code like this:

Sub Scratchmacro()
Dim pStrTest As String
Dim oFFs As FormFields
Dim oFF As FormField
Set oFFs = ActiveDocument.Paragraphs(1).Range.FormFields
For Each oFF In oFFs
If oFF.Type = wdFieldFormCheckBox Then
If oFF.Result = True Then
pStrTest = pStrTest & ActiveDocument.Bookmarks(oFF.Name &
"_Cap").Range.Text & ", "
End If
End If
Next
'Clean up the string text.
If Right(pStrTest, 2) = ", " Then pStrTest = Left(pStrTest, Len(pStrTest) -
2) & "."
On Error Resume Next
pStrTest = Left(pStrTest, InStrRev(pStrTest, ",") - 1) & " and" &
Mid(pStrTest, InStrRev(pStrTest, ",") + 1)
MsgBox pStrTest
End Sub






Thanks for your reply, Greg.

I'm sorry it has taken me so long to reply. I will try what you
suggested, butI have one question - sorry if its pretty elementary,
I'm very knew to using the code to make these things happen.

When I am putting the string together, how do I tell it which strings
to use?

Greg Maxey said:
It might be easier to simply build your string and then clean it up.

E.g., - for each checked item add "item name, " to the string. So
if A B and D are checked the string would look like this:

A, B, D,

Then clean it up:

Sub Scratchmacro()
Dim pStrTest As String
pStrTest = "A, B, D, "
'Clean up the string text.
If Right(pStrTest, 2) = ", " Then pStrTest = Left(pStrTest,
Len(pStrTest) - 2) & "."
On Error Resume Next
pStrTest = Left(pStrTest, InStrRev(pStrTest, ",") - 1) & " and" &
Mid(pStrTest, InStrRev(pStrTest, ",") + 1)
MsgBox pStrTest
End Sub
Not sure about whether another macro is interfering with it, but I
found away to make it work and it will be fine.

You've done so much to help me already - I hate to ask another
question...

If you don't want to answer this one, I will post a new question.

In one place in this document, I will have several checkboxes that
are followed by text. Depending on the user filling out the form,
they could select one checkbox or several of them, ultimately
making a list of items.

How do I add the word "and" inbetween the last item and the next to
last item on the list but not have it show up if the person only
uses one of the items?

Again, thank you so much for all your help - you are a lifesaver!

:

Hi sg,

Do you have any other macros that might be interfering with the
process? The code certainly works for me.

--
Cheers
macropod
[Microsoft MVP - Word]


I must be doing something wrong, but I'm not sure what. I copied
your code exactly, changed the bookmark name but when I check the
box nothing happens.

The only way I can get it to work is if I leave the checkbox
unchecked, tab past it to the next field, then go back and check
the box. When I do that, the text and form fields pop in and the
field is selected as it should be.

Any ideas?

:

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]


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?
 
S

sg

Ok, I bookmarked all the fields next to the checkboxes as you said. I edited
the line of code:
pStrTest = pStrTest & ActiveDocument.Bookmarks(oFF.Name &
"_Cap").Range.Text & ", "

to use the constistent name I gave the bookmarks (_CB).

Then I set the code to run on exit from the last field in the list of all
the info I have. When I run the macro, all I get is a message box that says
Microsoft Word, but there is no actual message. Even when I click on OK,
nothing happens.

Any ideas?


Greg Maxey said:
You would have to link your formfield checkbox to some text value that
serves as its caption. For example

CB1 Cat CB2 Dog CB3 Bird CB4 Fish

might be the first paragraph of your document. You have added text to serve
as the checkbox captions. You could bookmark the text to link it to its CB
e.g, bookmark "Cat" as "Check1_CAP", bookmark "Dog" as "Check2_CAP"

Then use code like this:

Sub Scratchmacro()
Dim pStrTest As String
Dim oFFs As FormFields
Dim oFF As FormField
Set oFFs = ActiveDocument.Paragraphs(1).Range.FormFields
For Each oFF In oFFs
If oFF.Type = wdFieldFormCheckBox Then
If oFF.Result = True Then
pStrTest = pStrTest & ActiveDocument.Bookmarks(oFF.Name &
"_Cap").Range.Text & ", "
End If
End If
Next
'Clean up the string text.
If Right(pStrTest, 2) = ", " Then pStrTest = Left(pStrTest, Len(pStrTest) -
2) & "."
On Error Resume Next
pStrTest = Left(pStrTest, InStrRev(pStrTest, ",") - 1) & " and" &
Mid(pStrTest, InStrRev(pStrTest, ",") + 1)
MsgBox pStrTest
End Sub






Thanks for your reply, Greg.

I'm sorry it has taken me so long to reply. I will try what you
suggested, butI have one question - sorry if its pretty elementary,
I'm very knew to using the code to make these things happen.

When I am putting the string together, how do I tell it which strings
to use?

Greg Maxey said:
It might be easier to simply build your string and then clean it up.

E.g., - for each checked item add "item name, " to the string. So
if A B and D are checked the string would look like this:

A, B, D,

Then clean it up:

Sub Scratchmacro()
Dim pStrTest As String
pStrTest = "A, B, D, "
'Clean up the string text.
If Right(pStrTest, 2) = ", " Then pStrTest = Left(pStrTest,
Len(pStrTest) - 2) & "."
On Error Resume Next
pStrTest = Left(pStrTest, InStrRev(pStrTest, ",") - 1) & " and" &
Mid(pStrTest, InStrRev(pStrTest, ",") + 1)
MsgBox pStrTest
End Sub

sg wrote:
Not sure about whether another macro is interfering with it, but I
found away to make it work and it will be fine.

You've done so much to help me already - I hate to ask another
question...

If you don't want to answer this one, I will post a new question.

In one place in this document, I will have several checkboxes that
are followed by text. Depending on the user filling out the form,
they could select one checkbox or several of them, ultimately
making a list of items.

How do I add the word "and" inbetween the last item and the next to
last item on the list but not have it show up if the person only
uses one of the items?

Again, thank you so much for all your help - you are a lifesaver!

:

Hi sg,

Do you have any other macros that might be interfering with the
process? The code certainly works for me.

--
Cheers
macropod
[Microsoft MVP - Word]


I must be doing something wrong, but I'm not sure what. I copied
your code exactly, changed the bookmark name but when I check the
box nothing happens.

The only way I can get it to work is if I leave the checkbox
unchecked, tab past it to the next field, then go back and check
the box. When I do that, the text and form fields pop in and the
field is selected as it should be.

Any ideas?

:

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]


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"
 

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