PDA

View Full Version : [SOLVED:] After exiting dropdown word doesn't go through "On-Exit" sub



OGKro
05-03-2023, 03:55 AM
I have a dropdown list that says Yes and No. Theres also a macro linked to it "On-Exit" which is :


Sub Exit_DD_33()
'ASZ 04/2023
If ActiveDocument.FormFields("DD33").Result = "NON" Then Delete_Dropdown33
End Sub

Sub Delete_Dropdown33()
'ASZ 04/2023
ADMIN_unprotec
ActiveDocument.Range(Start:=ActiveDocument.Bookmarks("StartDD33").Range.Start, End:=ActiveDocument.Bookmarks("EndDD33").Range.End).Select
Selection.Delete
ADMIN_protec
End Sub


For some reason it works for all the previous deletes but for this one it doesn't. Even tho it works when you go step by step. When just using tab after you chose No nothing happens.

Aussiebear
05-03-2023, 11:24 AM
Welcome to VBAX OGKro. Just wondering if you need to "Call" the sub to make it work?

gmayor
05-03-2023, 11:50 PM
The following should work


Sub Exit_DD_33()
'ASZ 04/2023
If UCase(ActiveDocument.FormFields("DD33").Result) = "NON" Then Delete_Dropdown33
End Sub

Sub Delete_Dropdown33()
'Graham Mayor - https://www.gmayor.com - Last updated - 04 May 2023
Dim oRng As Range
If Not ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If
Set oRng = ActiveDocument.FormFields("DD33").Range
oRng.Delete
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=""
Set oRng = Nothing
End Sub

OGKro
05-04-2023, 12:19 AM
It still doesn't work it seems that the contentcontrol checkbox i want to delete through this code is bothering it. Without the contentcontrol checkbox it deletes it with the code i wrote in my first post. But once i add the contentcontrol checkbox it just skips the code and goes to the contentcontrol checkbox.

gmayor
05-04-2023, 03:06 AM
The code you posted, and which I amended, is unsuitable for content controls. It is for legacy form fields, which your original code actually references.
You can't mix content controls and form fields in the same document, and content controls should not be protected as forms.
For content controls you need the following in the ThisDocument module of the document and the document should be protected as read only, with editors to mark the controls as editable. See https://www.gmayor.com/insert_content_control_addin.htm

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim oRng As Range
If ContentControl.ShowingPlaceholderText = False Then
Select Case ContentControl.Title
Case "DD33"
With ContentControl
If UCase(.Range.Text) = "NON" Then
If Not ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If
.LockContentControl = False
Set oRng = .Range
.Delete
oRng.Text = ""
ActiveDocument.Protect Type:=wdAllowOnlyReading, Password:=""
End If
End With
Case Else
End Select
End If
End Sub

OGKro
05-04-2023, 03:14 AM
I fixed it. I simpy put an empty formfield between the dropdown list and contentcontrol. That way it doesn't skip any code. What i learned from this experience is that you can't use a contentcontrol after a formfield if there is code attached to it.