#1
|
|||
|
|||
Formatting ID numbers to insert dashes in between some characters
Hello, I am a total newbie to VBA, I am currently studying IT and just took an basic java course with the university. In the company I am working with I have been tasked to create a digital form using MS Word.
So my problem is, when a user enter their specific ID requested, 11 characters long, I wanted it to auto format to @@@@-@@@-@@@@ when the user exits the field I am using word 2010 |
#2
|
|||
|
|||
That would depend on what type of field. If it is a content control in the document itself then something like this
Code:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) Select Case ContentControl.Title Case "ID" If Not ContentControl.Range.Text Like "####-###-####" Then If IsNumeric(ContentControl.Range.Text) And Len(ContentControl.Range.Text) = 11 Then ContentControl.Range.Text = Format(ContentControl.Range.Text, "####-###-####") Else MsgBox "Please enter an eleven digit ID" End If End If End Select End Sub Last edited by macropod; 12-21-2020 at 02:16 PM. Reason: Added code tags |
#3
|
|||
|
|||
Sorry I didn't specify, I thought I specified which text form field I used.
I am using the legacy text field set to Regular text with max length to 11 from the code you provided, that made sense to me, but is there a keyword to change "ContentControl" to legacy text field instead? Thank You |
#4
|
|||
|
|||
For formfileds, you will have to create a custom procedure and call in when you exit the formfield (see the formfield properties dialog):
Private Sub FormatInput() Dim oFF As FormField Set oFF = ActiveDocument.FormFields("Text1") 'Your formfield bookmark name. If Not oFF.Range.Text Like "####-###-####" Then If IsNumeric(oFF.Range.Text) And Len(oFF.Range.Text) = 11 Then oFF.Range.Text = Format(oFF.Range.Text, "####-###-####") Else MsgBox "Please enter an eleven digit ID" End If End If End Sub |
#5
|
|||
|
|||
I tried the code provided, now it gives me a run time error
Run-time error '6124' You are not allowed to edit this selection because it is protected Correct me if I am wrong, the .FormFields() allow the it the field to be access/edit when it is protected. I have the correct form field text option bookmark in the .FormFields() the debugger highlights oFF.Range.Text = " Format(oFF.Range.Text, "####-###-####") from my understanding, this should be able to access the protected document oFF is accessing formFields Just Curious, does removing the IsNumeric on the If statement allows letters/characters to be typed in instead of just numeric digits? |
#6
|
|||
|
|||
My fault, you need to use result (not range.text) and to format using alphacharacters, I think something like this:
Code:
Sub FormatInput() Dim oFF As FormField Set oFF = ActiveDocument.FormFields("Text1") 'Your formfield bookmark name. If Not oFF.Range.Text Like "????-???-????" Then If Len(oFF.Range.Text) = 11 Then oFF.Result = Left(oFF.Result, 4) & "-" & Mid(oFF.Result, 5, 3) & "-" & Right(oFF.Result, 4) Else MsgBox "Please enter an eleven digit ID" End If End If End Sub |
#7
|
|||
|
|||
That works great. Thank You
It has been a good learning experience |
Tags |
autoformat, formating |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How to insert paragraph character after every 500 characters? | aditya_bokade | Word VBA | 28 | 11-13-2021 10:48 PM |
Insert text longer 254 characters in word | Esgrimidor | Word VBA | 7 | 03-16-2017 01:02 PM |
Insert more than 500 characters on FormField Word by VBA | Jow | Word VBA | 4 | 09-15-2016 04:06 AM |
Is there a quick way to remove the dashes and parentheses from phone numbers? | Stacy | Excel | 2 | 09-12-2014 07:03 AM |
Macro to insert WordArt characters | Jennifer Murphy | Word VBA | 1 | 02-25-2014 03:10 AM |