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