Crash ControlOnExit when clicking on another ContentControl

I’m learning how to develop macros with vba to automate the filling in of a document at work.I’m getting the hang of it, even though I’m aware that it’s certainly not optimised but I’m having a problem with a contentcontrolonexit event.

This is how it works:
we have 6 criteria for which the user must enter a number of occurrences, between 1 and 10, in the Occurrences contentcontrol. On exiting this contentcontrol, the ContentControlOnExit event is launched and activates 4 procedures. Everything works perfectly when the user exits the contentcontrol by clicking on an empty space in the document or a text or still image. However, if they click on the Image contentcontrol (to the left), 3 procedures work well but the “addBorders” procedure crashes and the document is closed.

visual extract of the document

The purpose of this procedure is to frame the image content control with a green border (if occurrences <4) or a red border (if occurrences >= 4).

Here are the code extracts for the ContentControlOnExit and addBorders events.

Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As Boolean)

Dim i As Integer
Dim ccNb, ccIndice, ccTT_V, ccTT_NV As Integer
Dim ccCritRisk As String

'----------------------If you change the number of occurrences of a criterion--------------------
If Left(CC.Tag, 7) = "Critère" Then

    CC.SetPlaceholderText , , "Occurences"
    ccIndice = CInt(Right(CC.Tag, 1)) 'the criterion index is retrieved
    
    If CC.ShowingPlaceholderText = False Then 'if the number of occurrences is filled in
        ccNb = CInt(CC.Range.Text) 'the number of occurences is retrieved
        
        'Pour le critère modifié:
        selectComment ccNb, ccIndice, critArray 'select and fill the comment on the right - WORKS WELL EVERYTIME
        occuCount 'fill the introduction text - WORKS WELL EVERYTIME
        addBorders ccNb, ccIndice 'add the border to the image of the criterion
        tableFill ccNb, ccIndice 'fill the table on first page -WORKS WELL EVERYTIME
     Else
        ActiveDocument.SelectContentControlsByTitle("Com" & CStr(ccIndice))(1).SetPlaceholderText , , "Commentaire automatique"
        ActiveDocument.SelectContentControlsByTitle("Com" & CStr(ccIndice))(1).Range.Text = PlaceholderText
     End If

Else
'Other things done with other contentcontrol in the document, not linked to the problem
End If
End Sub
Sub addBorders(ccNb, ccIndice)

If ccNb > 10 Then
    MsgBox ("Le nombre " & ccNb & " est trop élevé")
    Exit Sub
Else
    If ccNb < 4 Then
        With ActiveDocument.SelectContentControlsByTitle("Image" & CStr(ccIndice))(1).Range
            With .Borders(wdBorderLeft)
                    .LineStyle = wdLineStyleSingle
                    .LineWidth = wdLineWidth300pt
                    .Color = RGB(71, 212, 89)
            End With
        End With
    Else 'on met commentaire négatif et bordure rouge
        With ActiveDocument.SelectContentControlsByTitle("Image" & CStr(ccIndice))(1).Range
            With .Borders(wdBorderLeft)
                    .LineStyle = wdLineStyleSingle
                    .LineWidth = wdLineWidth300pt
                    .Color = RGB(200, 0, 0)
            End With
        End With
    End If
End If


End Sub

The addBorders procedure is the only one that involves selecting the contentcontrolImage on which the user has potentially clicked (to exit the Occurences contentcontrol). I don’t think it’s possible to select this already selected contentcontrol.

I tried changing the position of the cursor before running addBorders to make sure it wasn’t on the image but that didn’t work. I don’t know what else to do…

Technically, it’s not a problem because I’ll just have to explain to my colleagues to click on an empty space after entering the number of occurrences, but it’s restrictive and not really intuitive. I’d like the user to be “free to move” between the Occurences and Images contentcontrols.

If anyone has any ideas on how to solve the problem, thank you for your help. i can provide more details if necessary.

New contributor

Jeanne LR is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật