Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 04-05-2015, 08:53 AM
jer85's Avatar
jer85 jer85 is offline Windows 7 32bit Office 2010 32bit
Novice
 
Join Date: Mar 2015
Location: Budapest, Hungary
Posts: 2
jer85 is on a distinguished road
Default Macro to keep formatted form fields after mail merge or replace text with formatted form fields

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
Reply With Quote
  #2  
Old 04-05-2015, 02:59 PM
jer85's Avatar
jer85 jer85 is offline Windows 7 32bit Office 2010 32bit
Novice
 
Join Date: Mar 2015
Location: Budapest, Hungary
Posts: 2
jer85 is on a distinguished road
Default

Hi! For Problem 1 solution I'd 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
Reply With Quote
  #3  
Old 04-05-2015, 10:00 PM
gmayor's Avatar
gmayor gmayor is offline Windows 7 64bit Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 2,719
gmayor is just really nicegmayor is just really nicegmayor is just really nicegmayor is just really nice
Default

Mail merge and form fields are mutually exclusive, however if your data is in Excel (or can be put into Excel format) you could use http://www.gmayor.com/ManyToOne.htm.

In One to One mode, the add-in will allow you to retain the form fields (though it will not fill them) alongside merged data.

As the aim of the game appears to be to get users to validate the data you hold for them, you could create a table that contains the mail merged data you hold with form fields for any new data alongside and merge to e-mail.

In fact with a table you don't even need the form fields as it is easy enough to read the data back from a table to Excel - you'll find lots of help with that too on my web site.
__________________
Graham Mayor - MS MVP (Word)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply

Tags
form field, mail merge

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple Text Fields in Form robstark Word 1 08-11-2014 11:26 AM
Spellcheck macro for protected form fields needed rharvey1215 Word 12 03-31-2014 06:47 PM
Word Form / VBA Solution for Formatted Document elmousa68 Word VBA 5 10-15-2013 05:10 PM
Form Fields - Create blank form text field with if statement? LAssist2011 Word 5 12-14-2011 03:02 PM
Insert formatted table into word (mail merge) manojbmsce Mail Merge 0 09-25-2008 02:25 AM


All times are GMT -7. The time now is 10:13 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft