Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-21-2020, 08:41 AM
shaun0406 shaun0406 is offline Formatting ID numbers to insert dashes in between some characters Windows 10 Formatting ID numbers to insert dashes in between some characters Office 2016
Novice
Formatting ID numbers to insert dashes in between some characters
 
Join Date: Dec 2020
Posts: 19
shaun0406 is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 12-21-2020, 01:27 PM
gmaxey gmaxey is offline Formatting ID numbers to insert dashes in between some characters Windows 10 Formatting ID numbers to insert dashes in between some characters Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/

Last edited by macropod; 12-21-2020 at 02:16 PM. Reason: Added code tags
Reply With Quote
  #3  
Old 12-21-2020, 02:09 PM
shaun0406 shaun0406 is offline Formatting ID numbers to insert dashes in between some characters Windows 10 Formatting ID numbers to insert dashes in between some characters Office 2010
Novice
Formatting ID numbers to insert dashes in between some characters
 
Join Date: Dec 2020
Posts: 19
shaun0406 is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 12-21-2020, 02:31 PM
gmaxey gmaxey is offline Formatting ID numbers to insert dashes in between some characters Windows 10 Formatting ID numbers to insert dashes in between some characters Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 12-21-2020, 03:07 PM
shaun0406 shaun0406 is offline Formatting ID numbers to insert dashes in between some characters Windows 10 Formatting ID numbers to insert dashes in between some characters Office 2010
Novice
Formatting ID numbers to insert dashes in between some characters
 
Join Date: Dec 2020
Posts: 19
shaun0406 is on a distinguished road
Default

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?
Reply With Quote
  #6  
Old 12-21-2020, 05:16 PM
gmaxey gmaxey is offline Formatting ID numbers to insert dashes in between some characters Windows 10 Formatting ID numbers to insert dashes in between some characters Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #7  
Old 12-22-2020, 09:36 AM
shaun0406 shaun0406 is offline Formatting ID numbers to insert dashes in between some characters Windows 10 Formatting ID numbers to insert dashes in between some characters Office 2010
Novice
Formatting ID numbers to insert dashes in between some characters
 
Join Date: Dec 2020
Posts: 19
shaun0406 is on a distinguished road
Default

That works great. Thank You
It has been a good learning experience
Reply With Quote
Reply

Tags
autoformat, formating



Similar Threads
Thread Thread Starter Forum Replies Last Post
Formatting ID numbers to insert dashes in between some characters How to insert paragraph character after every 500 characters? aditya_bokade Word VBA 28 11-13-2021 10:48 PM
Formatting ID numbers to insert dashes in between some characters Insert text longer 254 characters in word Esgrimidor Word VBA 7 03-16-2017 01:02 PM
Formatting ID numbers to insert dashes in between some characters Insert more than 500 characters on FormField Word by VBA Jow Word VBA 4 09-15-2016 04:06 AM
Formatting ID numbers to insert dashes in between some characters 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

Other Forums: Access Forums

All times are GMT -7. The time now is 01:24 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft