PDA

View Full Version : Macro to keep formatted form fields after mail merge or replace text with formatted



jer85
04-05-2015, 09:43 AM
Hi,

Could I please ask for a little help? I'm really stuck with a thing and I'm not familiar with VBA in Word, however it would be very important for me. Actually I would have 2 questions. A solution for Problem 1 would be the ideal for me but I realize that it might be a very difficult request. If it's not possible, a solution for Problem 2 would make me also happy and grateful.

Problem 1)
I have a document with empty form fields and I have a database which contains information for some of these fields. I would like to execute a merge mail, my goal is to import data from the database to the relevant fields (leaving intact the remaining fields), but I would like to achieve the following 3 goals at the same time:
- I would like to insert the merge mail fields into the specific form fields (not overwriting them) so as that after merge mail they still could be edited. It might be possible that the database has some data which are not up to date so I would like to give the user the opportunity to edit the imported data in fields (while the overall document would be protected of course).
- after merge mail I would also like the blank form fields to be preserved, so they could be edited after protecting the document. As I experienced, Word deletes the form fields after merge mail by default.
- my form fields have different charcter masks, e.g. allowing only 1 digit numbers, 2 digit numbers or plain texts to be input. I would really like to keep these format rules after merge mail.

Problem 2)
Supposing that it’s not possible to keep form fields when doing merge mail, I would place specific character combinations in the original document as a sign for future wanted form fields (e.g. „ff1”, „ff2”, „fft”). After merge mail is done, I would execute a code to find these character sets and replace them with form fields. What code should I use if I want each instance of „ff1” to be replaced with 1 digit, number only form fields, „ff2” with 2 digit, number only form fields and „fft” with plain text form fields? This way the users unfortunately would not have the opportunity to edit the data imported, but the blank form fields could be still edited (hence I can protect the document) which is a half success.

I really appreciate any help and the time you spend to answer me!
Thanks,
Roland

jer85
04-05-2015, 02:57 PM
Hi! For a solution for Problem 1 I would still be grateful, however I've found a solution for Problem 2:

Sub Makró1()

Dim oFF1 As FormField
Dim oFF2 As FormField
Dim oFFx As FormField
Dim sFindText As String

Selection.HomeKey wdStory
sFindText = "f1"
Selection.Find.Execute sFindText
Do Until Selection.Find.Found = False
Set oFF1 = Selection.FormFields.Add(Range:=Selection.Range, Type:=wdFieldFormTextInput)
With oFF1
With .TextInput
.EditType Type:=wdNumberText, Default:="", Format:=""
.Width = 1
End With
.Enabled = True
End With
ActiveDocument.Fields.Update
Selection.Find.Execute
Loop

Selection.HomeKey wdStory
sFindText = "f2"
Selection.Find.Execute sFindText
Do Until Selection.Find.Found = False
Set oFF2 = Selection.FormFields.Add(Range:=Selection.Range, Type:=wdFieldFormTextInput)
With oFF2
With .TextInput
.EditType Type:=wdNumberText, Default:="", Format:=""
.Width = 2
End With
.Enabled = True
End With
ActiveDocument.Fields.Update
Selection.Find.Execute
Loop

Selection.HomeKey wdStory
sFindText = "fx"
Selection.Find.Execute sFindText
Do Until Selection.Find.Found = False
Set oFFx = Selection.FormFields.Add(Range:=Selection.Range, Type:=wdFieldFormTextInput)
With oFFx
With .TextInput
.EditType Type:=wdRegularText, Default:="", Format:=""
End With
.Enabled = True
End With
ActiveDocument.Fields.Update
Selection.Find.Execute
Loop

End Sub