Share via

Type mismatch when using SelectContentControlsByTitle in a very simple subroutine

KSteele 71 Reputation points
2022-08-09T12:50:52.547+00:00

Sub Demo()
Dim CCtrl As ContentControl
'On Error Resume Next
Set CCtrl = ThisDocument.SelectContentControlsByTitle("qwe123")
If CCtrl Is Nothing Then
Debug.Print "found nothing"
End If
Set CCtrl = Nothing
End Sub

The above code fails with "type mismatch" unless I uncomment "On error resume next". Can someone explain the reason for this?

Developer technologies | Visual Basic for Applications
0 comments No comments

Answer accepted by question author

Viorel 127K Reputation points
2022-08-09T13:30:39.65+00:00

I think that the error indicates that SelectContentControlsByTitle does not return a Content Control.

According to documentation, try this:

Dim CCtrls As ContentControls  
Set CCtrls = ThisDocument.SelectContentControlsByTitle("qwe123")  
If CCtrls.Count = 0 Then  
    Debug.Print "found nothing"  
Else  
    ' get the first found control '  
    Dim cc As ContentControl  
    Set cc = CCtrls(1)  
    ' ...  
End If  

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. KSteele 71 Reputation points
    2022-08-09T14:42:28.473+00:00

    Thanks Viorel-1

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.