Consulting

Results 1 to 6 of 6

Thread: After exiting dropdown word doesn't go through "On-Exit" sub

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Regular
    Joined
    May 2023
    Posts
    9
    Location

    After exiting dropdown word doesn't go through "On-Exit" sub

    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.
    Last edited by Aussiebear; 05-03-2023 at 11:20 AM. Reason: Added code tags to supplied code

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,180
    Location
    Welcome to VBAX OGKro. Just wondering if you need to "Call" the sub to make it work?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  4. #4
    VBAX Regular
    Joined
    May 2023
    Posts
    9
    Location
    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.

  5. #5
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  6. #6
    VBAX Regular
    Joined
    May 2023
    Posts
    9
    Location
    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •