Share via

Force Entry in Text Field and Unprotect Template

Anonymous
2020-08-12T22:23:38+00:00

I’m creating a template in Word 2016, and I have a couple of issues. Most of the fields have an on entry macro associated with them to force text input for the prior field. A message box comes up if the user tabs out of the field without inputting text.

My first issue is this: The last field, E-mail, has an on entry macro for the field before it, Extension, and an on exit macro so if the user tabs out of the E-mail field a message box comes up.

When the user completes the Extension field and tabs to the E-mail field, if they tab out of the field without entering text a message box comes up letting the user know they need to enter text into that field. However, when the user clicks the OK button the cursor advances to the first input field (Dropdown1). I would like it to stay on the Extension field, Text 13. Below is my coding:

Sub ForceEmail()

Dim Msg, Style, Title, Response

On Error GoTo fError

If ActiveDocument.FormFields("Text13").Result = "" Then

   Msg = "You must enter text in the E-mail field."

   Style = vbOKOnly + vbCritical

   Title = "Input Text Warning"

   Response = MsgBox(Msg, Style, Title)

   Selection.GoTo wdGoToBookmark, Name:="Text13"

End If

Exit Sub

fError:

   MsgBox Err.Description

End Sub

My second issue is when the user has entered text into the E-mail field and they tab out of it, I need the template to unprotect and delete the header of the template. The coding below is what I have been using as an on exit macro on many templates and it has worked before, but now I am thinking I need to add the below coding to the ForceEmail coding since I have placed that as an on exit macro in the E-mail field. I have tried adding the coding below to the coding above but cannot get it to work.

Sub DocUnPro()

'If document is protected, unprotect it.

   If ActiveDocument.ProtectionType <> wdNoProtection Then

       ActiveDocument.Unprotect Password:=""

   End If

   With ActiveDocument.Sections(1)

       .Headers(wdHeaderFooterPrimary).Range.Text = ""

   End With

   ActiveDocument.Bookmarks("Dropdown1").Select

End Sub

Any help you can give me on these two issues is greatly appreciated. Jamie

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2020-08-12T23:59:10+00:00

    Declare 

    blnEmail as Boolean

    In the Entry macro for Text13, include the command

    blnEmail = True

    and create\add an Entry macro for the first Input field that contains the command

    If blnEmail = False then

       ActiveDocument.Bookmarks("Text13").Range.Select

    End If

    and modify the Sub ForceEmail as shown below

    Sub ForceEmail()

    Dim Msg, Style, Title, Response

    On Error GoTo fError

    If ActiveDocument.FormFields("Text13").Result = "" Then

       Msg = "You must enter text in the E-mail field."

       Style = vbOKOnly + vbCritical

       Title = "Input Text Warning"

       Response = MsgBox(Msg, Style, Title)

      blnEmail = False

       Selection.GoTo wdGoToBookmark, Name:="Text13"

    Else

    DocUnPro

    End If

    Exit Sub

    fError:

       MsgBox Err.Description

    End Sub

    Was this answer helpful?

    0 comments No comments