Checkbox to delete text field

G

Greg Maxey

You could try stepping through the code one line at a time using F8 to try
to see where the breakdown occurs. Watch the pStrTest line. You know what
it sould be as you step through. If it isn't then try to determine why.
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?

:

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"
 
M

macropod

Hi sg,

I'm not sure if you noticed, but Greg's macro is coded to test chckbox formfields in the first paragraph of the document only. Even
when that's the case, I get vba errors with the macro under some conditions, so I'd suggest re-working it as:
Sub Scratchmacro()
Dim pStrTest As String, oFF As FormField
With ActiveDocument
.Unprotect
For Each oFF In .Paragraphs(1).Range.FormFields
If oFF.Type = wdFieldFormCheckBox Then _
If oFF.Result = True Then _
If Trim(ActiveDocument.Bookmarks(oFF.Name & "_Cap").Range.Text) <> "" Then _
pStrTest = pStrTest & _
ActiveDocument.Bookmarks(oFF.Name & "_Cap").Range.Text & ", "
Next
'Clean up the string text.
If Right(pStrTest, 2) = ", " Then pStrTest = Left(pStrTest, Len(pStrTest) - 2) & "."
If InStr(pStrTest, ",") > 0 Then _
pStrTest = Left(pStrTest, InStrRev(pStrTest, ",") - 1) & " and" _
& Mid(pStrTest, InStrRev(pStrTest, ",") + 1)
.Protect Type:=wdAllowOnlyFormFields, noreset:=True
End With
MsgBox pStrTest
End Sub

Aside from dealing with the errors, the code also tests for and skips over any of the bookmarks that might be empty in spite (or
because) of the checkboxes being checked.

The other thing to be aware of is that, if the last bookmark has any commas in it, the last of those commas that gets replaced, not
the last comma between the bookmarks.

--
Cheers
macropod
[Microsoft MVP - Word]


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

:

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

sg

In the following line

Set oFFs = ActiveDocument.Paragraphs(1).Range.FormFields

does it matter that this is not the first paragraph in the document?

Greg Maxey said:
You could try stepping through the code one line at a time using F8 to try
to see where the breakdown occurs. Watch the pStrTest line. You know what
it sould be as you step through. If it isn't then try to determine why.
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







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

:

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 = ""
 
M

macropod

Hi sg,

Yes, it matters. With greg's code, you have to specify the paragraph. If the fields span more than one paragraph, you need to test
each of them - perhaps via a loop. Alternatively, you could dispense with the paragraph condition altogether and simply loop through
the known set of relevant checkbox formfields.

--
Cheers
macropod
[Microsoft MVP - Word]


sg said:
In the following line

Set oFFs = ActiveDocument.Paragraphs(1).Range.FormFields

does it matter that this is not the first paragraph in the document?

Greg Maxey said:
You could try stepping through the code one line at a time using F8 to try
to see where the breakdown occurs. Watch the pStrTest line. You know what
it sould be as you step through. If it isn't then try to determine why.
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?


:

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







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

:

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 = ""
 
G

Greg Maxey

sg,

Yes. The code I sent you was just something quick and dirty to show you how
you might build a text string from a selection of checkboxes. It was not
intended as a specific solution to your particular case.

In the following line

Set oFFs = ActiveDocument.Paragraphs(1).Range.FormFields

does it matter that this is not the first paragraph in the document?

Greg Maxey said:
You could try stepping through the code one line at a time using F8
to try to see where the breakdown occurs. Watch the pStrTest line.
You know what it sould be as you step through. If it isn't then try
to determine why.
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?


:

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







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

:

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 = ""
 
S

sg

Is it possible to make it look at a certain section instead of a paragraph in
this code? While all the checkboxes are in the same paragraph, it is hard to
know which paragraph it is since the document is so long. However, this
paragraph is also its own section so that would be easy to identify.

macropod said:
Hi sg,

I'm not sure if you noticed, but Greg's macro is coded to test chckbox formfields in the first paragraph of the document only. Even
when that's the case, I get vba errors with the macro under some conditions, so I'd suggest re-working it as:
Sub Scratchmacro()
Dim pStrTest As String, oFF As FormField
With ActiveDocument
.Unprotect
For Each oFF In .Paragraphs(1).Range.FormFields
If oFF.Type = wdFieldFormCheckBox Then _
If oFF.Result = True Then _
If Trim(ActiveDocument.Bookmarks(oFF.Name & "_Cap").Range.Text) <> "" Then _
pStrTest = pStrTest & _
ActiveDocument.Bookmarks(oFF.Name & "_Cap").Range.Text & ", "
Next
'Clean up the string text.
If Right(pStrTest, 2) = ", " Then pStrTest = Left(pStrTest, Len(pStrTest) - 2) & "."
If InStr(pStrTest, ",") > 0 Then _
pStrTest = Left(pStrTest, InStrRev(pStrTest, ",") - 1) & " and" _
& Mid(pStrTest, InStrRev(pStrTest, ",") + 1)
.Protect Type:=wdAllowOnlyFormFields, noreset:=True
End With
MsgBox pStrTest
End Sub

Aside from dealing with the errors, the code also tests for and skips over any of the bookmarks that might be empty in spite (or
because) of the checkboxes being checked.

The other thing to be aware of is that, if the last bookmark has any commas in it, the last of those commas that gets replaced, not
the last comma between the bookmarks.

--
Cheers
macropod
[Microsoft MVP - Word]


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







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

:

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

Greg Maxey

Yes. Use the Sections colletion with the appropriate index.

Sub ScratchMacro()
'Long or short document. You should be able to determine the current
paragraph index with:
MsgBox ActiveDocument.Range(0,
Selection.Paragraphs(1).Range.End).Paragraphs.Count
End Sub


Sub ScratchMacroII()
Dim oFF As FormField
For Each oFF In ActiveDocument.Sections(1).Range.FormFields
'...
Next oFF
End Sub

Is it possible to make it look at a certain section instead of a
paragraph in this code? While all the checkboxes are in the same
paragraph, it is hard to know which paragraph it is since the
document is so long. However, this paragraph is also its own section
so that would be easy to identify.

macropod said:
Hi sg,

I'm not sure if you noticed, but Greg's macro is coded to test
chckbox formfields in the first paragraph of the document only. Even
when that's the case, I get vba errors with the macro under some
conditions, so I'd suggest re-working it as:
Sub Scratchmacro()
Dim pStrTest As String, oFF As FormField
With ActiveDocument
.Unprotect
For Each oFF In .Paragraphs(1).Range.FormFields
If oFF.Type = wdFieldFormCheckBox Then _
If oFF.Result = True Then _
If Trim(ActiveDocument.Bookmarks(oFF.Name &
"_Cap").Range.Text) <> "" Then _ pStrTest = pStrTest & _
ActiveDocument.Bookmarks(oFF.Name & "_Cap").Range.Text &
", " Next
'Clean up the string text.
If Right(pStrTest, 2) = ", " Then pStrTest = Left(pStrTest,
Len(pStrTest) - 2) & "." If InStr(pStrTest, ",") > 0 Then _
pStrTest = Left(pStrTest, InStrRev(pStrTest, ",") - 1) & " and" _
& Mid(pStrTest, InStrRev(pStrTest, ",") + 1)
.Protect Type:=wdAllowOnlyFormFields, noreset:=True
End With
MsgBox pStrTest
End Sub

Aside from dealing with the errors, the code also tests for and
skips over any of the bookmarks that might be empty in spite (or
because) of the checkboxes being checked.

The other thing to be aware of is that, if the last bookmark has any
commas in it, the last of those commas that gets replaced, not the
last comma between the bookmarks.

--
Cheers
macropod
[Microsoft MVP - Word]


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


:

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







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

:

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

sg

When I use the code you suggested, I get an error that says the requested
member does not exist.

The following line is highlighted:

If Trim(ActiveDocument.Bookmarks(oFF.Name & "_CB").Range.Text) <> "" Then _

I double-checked and I have the bookmark names set to end with _CB. I also
did change it to look at the correct section of the document.

I'm so sorry to keep bothering you with this. Of course, if you have more
help to offer, I am extremely grateful!

macropod said:
Hi sg,

I'm not sure if you noticed, but Greg's macro is coded to test chckbox formfields in the first paragraph of the document only. Even
when that's the case, I get vba errors with the macro under some conditions, so I'd suggest re-working it as:
Sub Scratchmacro()
Dim pStrTest As String, oFF As FormField
With ActiveDocument
.Unprotect
For Each oFF In .Paragraphs(1).Range.FormFields
If oFF.Type = wdFieldFormCheckBox Then _
If oFF.Result = True Then _
If Trim(ActiveDocument.Bookmarks(oFF.Name & "_Cap").Range.Text) <> "" Then _
pStrTest = pStrTest & _
ActiveDocument.Bookmarks(oFF.Name & "_Cap").Range.Text & ", "
Next
'Clean up the string text.
If Right(pStrTest, 2) = ", " Then pStrTest = Left(pStrTest, Len(pStrTest) - 2) & "."
If InStr(pStrTest, ",") > 0 Then _
pStrTest = Left(pStrTest, InStrRev(pStrTest, ",") - 1) & " and" _
& Mid(pStrTest, InStrRev(pStrTest, ",") + 1)
.Protect Type:=wdAllowOnlyFormFields, noreset:=True
End With
MsgBox pStrTest
End Sub

Aside from dealing with the errors, the code also tests for and skips over any of the bookmarks that might be empty in spite (or
because) of the checkboxes being checked.

The other thing to be aware of is that, if the last bookmark has any commas in it, the last of those commas that gets replaced, not
the last comma between the bookmarks.

--
Cheers
macropod
[Microsoft MVP - Word]


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







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

:

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

macropod

Hi sg,

Does you document actually have a bookmark that corresponds with whatever oFF.Name resolves to, follwed by '_CB', when the error
occurs?

--
Cheers
macropod
[Microsoft MVP - Word]


sg said:
When I use the code you suggested, I get an error that says the requested
member does not exist.

The following line is highlighted:

If Trim(ActiveDocument.Bookmarks(oFF.Name & "_CB").Range.Text) <> "" Then _

I double-checked and I have the bookmark names set to end with _CB. I also
did change it to look at the correct section of the document.

I'm so sorry to keep bothering you with this. Of course, if you have more
help to offer, I am extremely grateful!

macropod said:
Hi sg,

I'm not sure if you noticed, but Greg's macro is coded to test chckbox formfields in the first paragraph of the document only.
Even
when that's the case, I get vba errors with the macro under some conditions, so I'd suggest re-working it as:
Sub Scratchmacro()
Dim pStrTest As String, oFF As FormField
With ActiveDocument
.Unprotect
For Each oFF In .Paragraphs(1).Range.FormFields
If oFF.Type = wdFieldFormCheckBox Then _
If oFF.Result = True Then _
If Trim(ActiveDocument.Bookmarks(oFF.Name & "_Cap").Range.Text) <> "" Then _
pStrTest = pStrTest & _
ActiveDocument.Bookmarks(oFF.Name & "_Cap").Range.Text & ", "
Next
'Clean up the string text.
If Right(pStrTest, 2) = ", " Then pStrTest = Left(pStrTest, Len(pStrTest) - 2) & "."
If InStr(pStrTest, ",") > 0 Then _
pStrTest = Left(pStrTest, InStrRev(pStrTest, ",") - 1) & " and" _
& Mid(pStrTest, InStrRev(pStrTest, ",") + 1)
.Protect Type:=wdAllowOnlyFormFields, noreset:=True
End With
MsgBox pStrTest
End Sub

Aside from dealing with the errors, the code also tests for and skips over any of the bookmarks that might be empty in spite (or
because) of the checkboxes being checked.

The other thing to be aware of is that, if the last bookmark has any commas in it, the last of those commas that gets replaced,
not
the last comma between the bookmarks.

--
Cheers
macropod
[Microsoft MVP - Word]


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


:

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







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

:

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:
 

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