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.
Jeanne LR is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.