Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using VBA to insert MS Word 2000 Form fields

Status
Not open for further replies.

Worsty

Programmer
Oct 27, 2004
34
US
We have an application in MS Access that merges data from the database into a Word Form. The Word Form contains Word Form Fields. Because we are merging the form fields are lost after the merge. I understand that there is a way to insert Word Form Fields in VBA after the merge. Could anyone give me some guidance as to how to do this? A sample piece of code would be very helpful.

Thanks in advance
 
After merging the fields from Access into the Word Form where there are existing Word form fields the form fields are not there anymore. I've researched this and Microsoft even says that this will happen. What they recommend is inserting the Word form fields after the merge.

I am not sure how to do this. Has anyone had the same issue? How did you resolve?
 
I know what you are talking about its a real pain, heres a article with code here


though also recomend you check out the FAQ section of this forum and look for one by myself headed "Fastest way to populate a word document"

Chance,

Filmmaker, taken gentleman and crunch day + 22
 
When I use the code from the link I'm getting an error at the asterisked location below:

Sub PreserveMailMergeFormFieldsNewDoc()

Dim fFieldText() As String
Dim iCount As Integer
Dim fField As FormField
Dim sWindowMain, sWindowMerge As String

On Error GoTo ErrHandler

' Store Main merge document window name.
sWindowMain = ActiveWindow.Caption

' Because the document contains form fields,
' it should be protected, so unprotect document.
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If

' Loop through all text form fields
' in the main mail merge document.
For Each aField In ActiveDocument.FormFields

' If the form field is a text form field...
If aField.Type = wdFieldFormTextInput Then

' Redim array to hold contents of text field.
ReDim Preserve fFieldText(1, iCount + 1)

' Place content and name of field into array.
fFieldText(0, iCount) = aField.Result
fFieldText(1, iCount) = aField.Name

' Select the form field.
aField.Select

' Replace it with placeholder text.
Selection.TypeText "<" & fFieldText(1, iCount) & "PlaceHolder>"

' Increment icount
iCount = iCount + 1

End If

Next aField

' Perform mail merge to new document.
ActiveDocument.MailMerge.Destination = wdSendToNewDocument
ActiveDocument.MailMerge.Execute


**************************************************
' Find and Replace placeholders with form fields.
doFindReplace iCount, fField, fFieldText()

' Protect the merged document.
ActiveDocument.Protect Password:="", NoReset:=True, _
Type:=WdAllowOnlyFormFields

' Get name of final merged document.
sWindowMerge = ActiveWindow.Caption

' Reactivate the main merge document.
Windows(sWindowMain).Activate

' Find and replace placeholders with form fields.
doFindReplace iCount, fField, fFieldText()

' Reprotect the main mail merge document.
ActiveDocument.Protect Password:="", NoReset:=True, _
Type:=WdAllowOnlyFormFields

' Switch back to the merged document.
Windows(sWindowMerge).Activate

ErrHandler:

End Sub
 
I checked out your code for inserting data with bookmarks. Is there anyway we could use a bookmark to insert a form field?
 
Code:
Sub PreserveMailMergeFormFieldsNewDoc()

Dim fFieldText() As String
Dim iCount As Integer
Dim fField As FormField
Dim sWindowMain, sWindowMerge As String

On Error Resume Next

' Store Main merge document window name.
sWindowMain = ActiveWindow.Caption

' Because the document contains form fields,
' it should be protected, so unprotect document.
If ActiveDocument.ProtectionType <> wdNoProtection Then
   ActiveDocument.Unprotect
End If

' Loop through all text form fields
' in the main mail merge document.
For Each aField In ActiveDocument.FormFields

   ' If the form field is a text form field...
   If aField.Type = wdFieldFormTextInput Then

      ' Redim array to hold contents of text field.
      ReDim Preserve fFieldText(1, iCount + 1)

      ' Place content and name of field into array.
      fFieldText(0, iCount) = aField.Result
      fFieldText(1, iCount) = aField.name

      ' Select the form field.
      aField.Select

      ' Replace it with placeholder text.
      Selection.TypeText fFieldText(0, iCount)

      ' Increment icount
      iCount = iCount + 1

   End If

Next aField

End Sub

Sub doFindReplace(iCount As Integer, fField As FormField, _
   fFieldText() As String)

' Go to top of document.
Selection.HomeKey unit:=wdStory

' Initialize Find.
Selection.Find.ClearFormatting

With Selection.Find
   .Forward = True
   .Wrap = wdFindContinue
   .Format = False
   .MatchCase = False
   .MatchWholeWord = False
   .MatchWildcards = False
   .MatchSoundsLike = False
   .MatchAllWordForms = False

   ' Loop form fields count.
    For i = 0 To iCount

      ' Execute the find.
      Do While .Execute(FindText:="<" & fFieldText(1, i) _
         & "PlaceHolder>") = True

         ' Replace the placeholder with the form field.
         Set fField = Selection.FormFields.Add _
            (Range:=Selection.Range, Type:=wdFieldFormTextInput)

         ' Restore form field contents and bookmark name.
         fField.Result = fFieldText(0, i)
         fField.name = fFieldText(1, i)
      Loop

      ' Go to top of document for next find.
      Selection.HomeKey unit:=wdStory

   Next
End With

End Sub

This is straight out of one of my word add ins give this a go

Chance,

Filmmaker, taken gentleman and crunch day + 22
 
The first function works and replaces all of my form fields with "<Text2PlaceHolder>" but the second sub "Sub doFindReplace" doesn't do anything.

I don't know what I am doing wrong. I tried to call the second sub, nothing.

Any ideas?
 
OK going back to the original article

copy all the following

Code:
Sub PreserveMailMergeFormFieldsNewDoc()

Dim fFieldText() As String
Dim iCount As Integer
Dim fField As FormField
Dim sWindowMain, sWindowMerge As String

On Error GoTo ErrHandler

' Store Main merge document window name.
sWindowMain = ActiveWindow.Caption

' Because the document contains form fields,
' it should be protected, so unprotect document.
If ActiveDocument.ProtectionType <> wdNoProtection Then
   ActiveDocument.Unprotect
End If

' Loop through all text form fields
' in the main mail merge document.
For Each aField In ActiveDocument.FormFields

   ' If the form field is a text form field...
   If aField.Type = wdFieldFormTextInput Then

      ' Redim array to hold contents of text field.
      ReDim Preserve fFieldText(1, iCount + 1)

      ' Place content and name of field into array.
      fFieldText(0, iCount) = aField.Result
      fFieldText(1, iCount) = aField.Name

      ' Select the form field.
      aField.Select

      ' Replace it with placeholder text.
      Selection.TypeText "<" & fFieldText(1, iCount) & "PlaceHolder>"

      ' Increment icount
      iCount = iCount + 1

   End If

Next aField

' Perform mail merge to new document.
ActiveDocument.MailMerge.Destination = wdSendToNewDocument
ActiveDocument.MailMerge.Execute

' Find and Replace placeholders with form fields.
doFindReplace iCount, fField, fFieldText()

' Protect the merged document.
ActiveDocument.Protect Password:="", NoReset:=True, _
   Type:=WdAllowOnlyFormFields

' Get name of final merged document.
sWindowMerge = ActiveWindow.Caption

' Reactivate the main merge document.
Windows(sWindowMain).Activate

' Find and replace placeholders with form fields.
doFindReplace iCount, fField, fFieldText()

' Reprotect the main mail merge document.
ActiveDocument.Protect Password:="", NoReset:=True, _
   Type:=WdAllowOnlyFormFields

' Switch back to the merged document.
Windows(sWindowMerge).Activate

ErrHandler:

End Sub


Sub doFindReplace(iCount As Integer, fField As FormField, _
   fFieldText() As String)

' Go to top of document.
Selection.HomeKey Unit:=wdStory

' Initialize Find.
Selection.Find.ClearFormatting

With Selection.Find
   .Forward = True
   .Wrap = wdFindContinue
   .Format = False
   .MatchCase = False
   .MatchWholeWord = False
   .MatchWildcards = False
   .MatchSoundsLike = False
   .MatchAllWordForms = False

   ' Loop form fields count.
    For i = 0 To iCount

      ' Execute the find.
      Do While .Execute (FindText:="<" & fFieldText(1, i) _
         & "PlaceHolder>") = True

         ' Replace the placeholder with the form field.
         Set fField = Selection.FormFields.Add _
            (Range:=Selection.Range, Type:=wdFieldFormTextInput)

         ' Restore form field contents and bookmark name.
         fField.Result = fFieldText(0, i)
         fField.Name = fFieldText(1, i)
      Loop

      ' Go to top of document for next find.
      Selection.HomeKey Unit:=wdStory

   Next
End With

End Sub

and try the first macro again.

Chance,

Filmmaker, taken gentleman and crunch day + 22
 
This is all I get???

<Text1PlaceHolder>

<Text2PlaceHolder>

<Text3PlaceHolder>

<Text4PlaceHolder>

<Text5PlaceHolder>

<Text6PlaceHolder>

Which is running the first part of the macro and changing the test form fields to the above, but it doesn't then change them back to form fields.
 
I had to split your code into 2 subs because we have some merge code that goes in the middle. What I mean is, I placed the following at the beginning of my code:

Sub PreserveMailMergeFormFieldsNewDoc()

Dim fFieldText() As String
Dim iCount As Integer
Dim fField As FormField
Dim sWindowMain, sWindowMerge As String

On Error GoTo ErrHandler

' Store Main merge document window name.
sWindowMain = ActiveWindow.Caption

' Because the document contains form fields,
' it should be protected, so unprotect document.
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If

' Loop through all text form fields
' in the main mail merge document.
For Each aField In ActiveDocument.FormFields

' If the form field is a text form field...
If aField.Type = wdFieldFormTextInput Then

' Redim array to hold contents of text field.
ReDim Preserve fFieldText(1, iCount + 1)

' Place content and name of field into array.
fFieldText(0, iCount) = aField.Result
fFieldText(1, iCount) = aField.Name

' Select the form field.
aField.Select

' Replace it with placeholder text.
Selection.TypeText "<" & fFieldText(1, iCount) & "PlaceHolder>"

' Increment icount
iCount = iCount + 1

End If

Next aField

******************************************

Inserted my mail merge code and then placed the rest of the code after my mail merge. It never changes the place holders back into form fields.

*********************************************

' Find and Replace placeholders with form fields.
doFindReplace iCount, fField, fFieldText()

' Protect the merged document.
ActiveDocument.Protect Password:="", NoReset:=True, _
Type:=WdAllowOnlyFormFields

' Get name of final merged document.
sWindowMerge = ActiveWindow.Caption

' Reactivate the main merge document.
Windows(sWindowMain).Activate

' Find and replace placeholders with form fields.
doFindReplace iCount, fField, fFieldText()

' Reprotect the main mail merge document.
ActiveDocument.Protect Password:="", NoReset:=True, _
Type:=WdAllowOnlyFormFields

' Switch back to the merged document.
Windows(sWindowMerge).Activate

ErrHandler:

End Sub


Sub doFindReplace(iCount As Integer, fField As FormField, _
fFieldText() As String)

' Go to top of document.
Selection.HomeKey Unit:=wdStory

' Initialize Find.
Selection.Find.ClearFormatting

With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False

' Loop form fields count.
For i = 0 To iCount

' Execute the find.
Do While .Execute (FindText:="<" & fFieldText(1, i) _
& "PlaceHolder>") = True

' Replace the placeholder with the form field.
Set fField = Selection.FormFields.Add _
(Range:=Selection.Range, Type:=wdFieldFormTextInput)

' Restore form field contents and bookmark name.
fField.Result = fFieldText(0, i)
fField.Name = fFieldText(1, i)
Loop

' Go to top of document for next find.
Selection.HomeKey Unit:=wdStory

Next
End With

End Sub


 
Code:
 Get name of final merged document.
sWindowMerge = ActiveWindow.Caption

add a break point here, and make sure the code is switching back to the right document.

Chance,

Filmmaker, taken gentleman and crunch day + 22
 
Thanks for the advice but I still can't get the second portion to work so I'm including the code as we have it to see if possibly I'm missing something.

Where I have the asterisks is where we call the module with the code to change the text fields to placeholers. We end the sub right before this next step:

Find and Replace placeholders with form fields.
doFindReplace iCount, fField, fFieldText()

The Find and Replace is what we are having problems with. No matter where we call this from it doesn't seem to find the placeholders in the merged document and change them to text fields.

Thanks so much for any ideas you can come up with.

Here is the code we have in a module in Access to start the word merge.

Function RidesMergeWord(strDocName As String)

' This code takes a word document that has been setup as a MERGE document.
' This merge document is opened, then mailmerge is executed. The original
' document is then closed. The result is a raw word document with no connectons
' to the merge.txt (a csv source data file).

'Parms:
' strDocName - full path name of word doc (.doc)
' bolSingle - (currently NOT used, but must be set = true).
' if true, then only the first doc in the list is merged
' if False, then printer is assumed, and the "merge" dialog
' box is showen. Thus single is for "one" document, and false
' means we have more then one record to merge.
' - this feature is DISABLED - you must use true!
'

' Albert D. Kallal (c) 2001
' kalla@msn.com
'

Dim wordApp As Object ' running instance of word
Dim WordDoc As Object ' one instance of a word doc
Dim strActiveDoc As String ' doc name (no path)
Dim lngWordDest As Long ' const for dest, 0 = new doc, 1 = printer

clsRidesPBar.ShowProgress
clsRidesPBar.TextMsg = "Launching Word...please wait..."
clsRidesPBar.Pmax = 4 ' 4 steps to inc
clsRidesPBar.IncOne ' step 1....start!

On Error GoTo CreateWordApp
Set wordApp = GetObject(, "Word.Application")
On Error Resume Next

clsRidesPBar.IncOne ' step 2, word is loaded.

Set WordDoc = wordApp.Documents.Open(strDocName)

clsRidesPBar.IncOne ' step 3, doc is loaded

strActiveDoc = wordApp.ActiveDocument.Name

******************************************************

'CALLS CODE TO CHANGE WORD DOC TEXT FIELDS TO PLACEHOLDERS-----THIS WORKS!!!!

Call Step1_PreserveMerge

****************************************************
'Add form fields
With WordDoc.MailMerge

.Destination = 0 ' 0 = new doc
.MailAsAttachment = False
.MailAddressFieldName = ""
.MailSubject = ""
.SuppressBlankLines = True
With .DataSource
.FirstRecord = 1
' .LastRecord = 1
End With

.Execute Pause:=True
End With

clsRidesPBar.IncOne ' step 4, doc is merged

WordDoc.Close (False)

wordApp.Visible = True
wordApp.Windows(wordApp.Windows.Count).Activate


clsRidesPBar.HideProgress

' AppActivate "Microsoft Word"
wordApp.Activate
'wordApp.WindowState = 0 'wdWindowStateRestore

'THIS IS THE KEY LINE TO PROTECT DOCUMENT AFTER MERGE EXECUTES

'wordApp.ActiveDocument.Protect Password:="", NoReset:=True, _
'Type:=wdAllowOnlyFormFields

Set wordApp = Nothing
Set WordDoc = Nothing

DoEvents

' If bolShowMerge = True Then
' WordApp.Dialogs(676).Show 'wdDialogMailMerge
' End If

Exit Function

CreateWordApp:
' this code is here to use the EXISTING copy of
' ms-access running. If getobject fails, then
' ms-word was NOT running. The below will then
' launch word
Set wordApp = CreateObject("Word.Application")
Resume Next

End Function
 
Try declaring

Dim fFieldText() As String
Dim iCount As Integer
Dim fField As FormField

Publicly instead of in the process,



Chance,

Filmmaker, taken gentleman and crunch day + 22
 
I don't know how to do this. Would you explain where to publicly declare them and how, please?
 
At the top of your module

Public fFieldText() As String
Public iCount As Integer
Public fField As FormField



Chance,

Filmmaker, taken gentleman and crunch day + 22
 
Declaring it Public doesn't work either. It changes the text fields to placeholders but never changes them back.

So, I went back to basics and created a test word document with text fields in it and connected it to an Excel workbook as the mail merge data source. Put the original code behind it and ran the macro. It goes into an endless loop. When I break the loop and click debug this is where it is stuck:

fField.Result = fFieldText(0, i)

If I comment out

ActiveDocument.MailMerge.Destination = wdSendToNewDocument
ActiveDocument.MailMerge.Execute

it changes the text fields to placeholders and then back, so it has to be something in the mail merge code.

Any ideas? Thanks for your continued support!
 
Follow these instructions carefully

Open up a new word document

And type the following

The First Field is Here.

The Second Field is Here.

The Third Field is Here.

At the end of each of those sentences put in a Form Field.

Protect the document for forms, and type some text into the fields such as Hello, World, Hello!

Open up VBA and copy the following exactly into a new module

Code:
Dim fFieldText() As String
Dim iCount As Integer
Dim fField As FormField
Dim sWindowMain, sWindowMerge As String


Sub PreserveMailMergeFormFieldsNewDoc()



On Error GoTo ErrHandler

' Store Main merge document window name.
sWindowMain = ActiveWindow.Caption

' Because the document contains form fields,
' it should be protected, so unprotect document.
If ActiveDocument.ProtectionType <> wdNoProtection Then
   ActiveDocument.Unprotect
End If
' Loop through all text form fields
' in the main mail merge document.
For Each aField In ActiveDocument.FormFields
   ' If the form field is a text form field...
   If aField.Type = wdFieldFormTextInput Then
      ' Redim array to hold contents of text field.
      ReDim Preserve fFieldText(1, iCount + 1)
      ' Place content and name of field into array.
      fFieldText(0, iCount) = aField.Result
      fFieldText(1, iCount) = aField.Name

      ' Select the form field.
      aField.Select

      ' Replace it with placeholder text.
      Selection.TypeText "<" & fFieldText(1, iCount) & "PlaceHolder>"

      ' Increment icount
      iCount = iCount + 1
   End If
Next aField


Stop

' Find and Replace placeholders with form fields.
doFindReplace iCount, fField, fFieldText()
' Protect the merged document.
ActiveDocument.Protect Password:="", NoReset:=True, _
   Type:=wdAllowOnlyFormFields
' Get name of final merged document.
sWindowMerge = ActiveWindow.Caption
' Reactivate the main merge document.
Windows(sWindowMain).Activate
' Find and replace placeholders with form fields.
doFindReplace iCount, fField, fFieldText()
' Reprotect the main mail merge document.
ActiveDocument.Protect Password:="", NoReset:=True, _
   Type:=wdAllowOnlyFormFields
' Switch back to the merged document.
Windows(sWindowMerge).Activate

ErrHandler:

End Sub


Sub doFindReplace(iCount As Integer, fField As FormField, _
   fFieldText() As String)
' Go to top of document.
Selection.HomeKey Unit:=wdStory
' Initialize Find.
Selection.Find.ClearFormatting
With Selection.Find
   .Forward = True
   .Wrap = wdFindContinue
   .Format = False
   .MatchCase = False
   .MatchWholeWord = False
   .MatchWildcards = False
   .MatchSoundsLike = False
   .MatchAllWordForms = False
   ' Loop form fields count.
    For i = 0 To iCount
      ' Execute the find.
      Do While .Execute(FindText:="<" & fFieldText(1, i) _
         & "PlaceHolder>") = True
         ' Replace the placeholder with the form field.
         Set fField = Selection.FormFields.Add _
            (Range:=Selection.Range, Type:=wdFieldFormTextInput)
         ' Restore form field contents and bookmark name.
         fField.Result = fFieldText(0, i)
         fField.Name = fFieldText(1, i)
      Loop
      ' Go to top of document for next find.
      Selection.HomeKey Unit:=wdStory

   Next
End With

End Sub

Now i have put a Stop command in the code above, when you run the sub, it will stop the module. Take a look at the word document and you will notice

that HEllo, World Hello! now says <text placeholder1> etc etc

Press F5 to continue the sub and the fielsd should go back to Hello, world Hello!

Where that Stop line is, is where you need to call you mail merge code from.

I absolutely hate mail merge word, my normally approach is what is outlined in the FAQ section on populating data in word documents.



Chance,

Filmmaker, taken gentleman and He tan e epi tas
 
We are so close>>>>>We got the below code to work. We still have the "stop" commands throughout, but that was just to test. We removed the stops and it works fine --- ONCE! ---. If you try to run it again without getting completely out of the database it doesn't work.

Any suggestions?

Thank you so much! You've been great!

****Word Module*********

Function RidesMergeWord(strDocName As String)

' This code takes a word document that has been setup as a MERGE document.
' This merge document is opened, then mailmerge is executed. The original
' document is then closed. The result is a raw word document with no connectons
' to the merge.txt (a csv source data file).

'Parms:
' strDocName - full path name of word doc (.doc)
' bolSingle - (currently NOT used, but must be set = true).
' if true, then only the first doc in the list is merged
' if False, then printer is assumed, and the "merge" dialog
' box is showen. Thus single is for "one" document, and false
' means we have more then one record to merge.
' - this feature is DISABLED - you must use true!
'

' Albert D. Kallal (c) 2001
' kalla@msn.com
'

Dim wordApp As Object ' running instance of word
Dim WordDoc As Object ' one instance of a word doc
Dim strActiveDoc As String ' doc name (no path)
Dim lngWordDest As Long ' const for dest, 0 = new doc, 1 = printer

clsRidesPBar.ShowProgress
clsRidesPBar.TextMsg = "Launching Word...please wait..."
clsRidesPBar.Pmax = 4 ' 4 steps to inc
clsRidesPBar.IncOne ' step 1....start!

On Error GoTo CreateWordApp
Set wordApp = GetObject(, "Word.Application")
On Error Resume Next

clsRidesPBar.IncOne ' step 2, word is loaded.

Set WordDoc = wordApp.Documents.Open(strDocName)

clsRidesPBar.IncOne ' step 3, doc is loaded

strActiveDoc = wordApp.ActiveDocument.Name
'PreserveMailMergeFormFieldsNewDoc CHANGE TO PLACEHOLDERS
Call PreserveMailMergeFormFieldsNewDoc

'Add form fields
With WordDoc.MailMerge

.Destination = 0 ' 0 = new doc
.MailAsAttachment = False
.MailAddressFieldName = ""
.MailSubject = ""
.SuppressBlankLines = True
With .DataSource
.FirstRecord = 1
' .LastRecord = 1
End With

.Execute Pause:=True
End With
' TEST1 Find and Replace placeholders with form fields.
Call Test1

clsRidesPBar.IncOne ' step 4, doc is merged

WordDoc.Close (False)

wordApp.Visible = True
wordApp.Windows(wordApp.Windows.Count).Activate


clsRidesPBar.HideProgress

' AppActivate "Microsoft Word"
wordApp.Activate


wordApp.WindowState = 2 'wdWindowStateRestore

'THIS IS THE KEY LINE TO PROTECT DOCUMENT AFTER MERGE EXECUTES
'DO FIND REPLACE HERE
wordApp.ActiveDocument.Protect Password:="", NoReset:=True, _
Type:=wdAllowOnlyFormFields

Set wordApp = Nothing
Set WordDoc = Nothing

DoEvents

' If bolShowMerge = True Then
' WordApp.Dialogs(676).Show 'wdDialogMailMerge
' End If

Exit Function

CreateWordApp:
' this code is here to use the EXISTING copy of
' ms-access running. If getobject fails, then
' ms-word was NOT running. The below will then
' launch word
Set wordApp = CreateObject("Word.Application")

Resume Next

End Function





*****TextField Modules Below

Public fFieldText() As String
Public iCount As Integer
Public fField As FormField
Public sWindowMain, sWindowMerge As String


Function PreserveMailMergeFormFieldsNewDoc()



On Error GoTo ErrHandler

' Store Main merge document window name.
sWindowMain = ActiveWindow.Caption

' Because the document contains form fields,
' it should be protected, so unprotect document.
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If
' Loop through all text form fields
' in the main mail merge document.
For Each aField In ActiveDocument.FormFields
' If the form field is a text form field...
If aField.Type = wdFieldFormTextInput Then
' Redim array to hold contents of text field.
ReDim Preserve fFieldText(1, iCount + 1)
' Place content and name of field into array.
fFieldText(0, iCount) = aField.Result
fFieldText(1, iCount) = aField.Name

' Select the form field.
aField.Select

' Replace it with placeholder text.
Selection.TypeText "<" & fFieldText(1, iCount) & "PlaceHolder>"

' Increment icount
iCount = iCount + 1
End If
Next aField


Stop





ErrHandler:

End Function


Function doFindReplace(iCount As Integer, fField As FormField, _
fFieldText() As String)
' Go to top of document.
Selection.HomeKey Unit:=wdStory
' Initialize Find.
Selection.Find.ClearFormatting
With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
' Loop form fields count.
For i = 0 To iCount
' Execute the find.
Do While .Execute(FindText:="<" & fFieldText(1, i) _
& "PlaceHolder>") = True
' Replace the placeholder with the form field.
Set fField = Selection.FormFields.Add _
(Range:=Selection.Range, Type:=wdFieldFormTextInput)
' Restore form field contents and bookmark name.
fField.Result = fFieldText(0, i)
fField.Name = fFieldText(1, i)
Loop
' Go to top of document for next find.
Selection.HomeKey Unit:=wdStory

Next
End With
Stop
End Function

Sub Test1()
doFindReplace iCount, fField, fFieldText()
' Protect the merged document.
ActiveDocument.Protect Password:="", NoReset:=True, _
Type:=wdAllowOnlyFormFields
' Get name of final merged document.
sWindowMerge = ActiveWindow.Caption
' Reactivate the main merge document.
Windows(sWindowMain).Activate
' Find and replace placeholders with form fields.
doFindReplace iCount, fField, fFieldText()
' Reprotect the main mail merge document.
ActiveDocument.Protect Password:="", NoReset:=True, _
Type:=wdAllowOnlyFormFields
' Switch back to the merged document.
Windows(sWindowMerge).Activate
Stop
End Sub
 
If you try to run it again without getting completely out of the database it doesn't work.

SOunds like a different issue, wheres the code for your data source ?

Chance,

Filmmaker, taken gentleman and He tan e epi tas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top