1

What am I doing wrong here? I need to replace various characters with check boxes but get an error:

Sub ReplaceCheckboxes()

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Do
  With Selection.Find
    .Text = ChrW(13)
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = True
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
  End With
  Selection.Range.ContentControls.Add (wdContentControlCheckBox) ' error on this line
  If Selection.Find.Execute = False Then Exit Do
Loop

Selection.HomeKey Unit:=wdStory

End Sub

I get "Object Doesn't Support This Action" on line Selection.Range.ContentControls.Add (wdContentControlCheckBox). I've also tried

Set objCC = ActiveDocument.ContentControls.Add(wdContentControlCheckBox)

and

ActiveDocument.ContentControls.Add wdContentControlCheckBox, Selection.Range

I guess I'm misunderstanding how the find method works.

1 Answer 1

3

I don't think that actual line is the problem. If you step through the code you will see that checkboxes are getting inserted. The more fundamental problem is that you have created an infinite loop. There is no exit condition for the Do loop so your code is probably just running until it crashes Word.

****EDIT to note correct solution:****

The solution was to remove the parentheses around wdContentControlCheckBox. In VBA, parentheses are only put around the arguments of a function when the result it returns is assigned to a variable.

Examples (function call assigning result to a variable and NOT assigning result to a variable, respectively):

result = SomeFunction(arg1, arg2)

or

SomeFunction arg1, arg2

Sign up to request clarification or add additional context in comments.

6 Comments

You should either use Selection.Next or Replace:=wdReplaceAll or advance your selection to the end of the current selection with each loop. That will solve your infinite loop problem.
I just looked at the code again & there is indeed an exit condition: If Selection.Find.Execute = False Then Exit Do. But I still think it's an infinite loop issue because that condition is never causing the loop to exit because the Find was executed. When I run the code as is, it causes Word to crash but if I change the False to True, i.e.: If Selection.Find.Execute = True Then Exit Do then one checkbox is inserted and sub the finishes without error.
However, I think that Peter's suggestion is the better route because repeatedly running this code with the change to True causes the checkboxes to be inserted one right after the other in a row without the selection advancing.
Thanks for the comments, but the loop isn't the issue. This line causes the error Selection.Range.ContentControls.Add (wdContentControlCheckBox)
Yes, that was the problem, thank you. Perhaps you could add that to your answer in case others come across this question?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.