Find a MERGEFIELD in a Table

Y

Yisman

Hi everyone
im setting up a mailmerge in word, which prints out a contract with some db
info. all is fine
after the doc opens i want to find if there is any table in the doc which
the first cell contains a mergefield. if it does then i want to iterate thru
a recordset and copy -paste the cell content several times in that table,
each time with different strings. as an example here is my current code:


With apWord
.Selection.Find.Execute "«FirstName»"
.Selection.Find.Execute
.Selection.SelectCell
.Selection.Copy
Dim rs As New ADODB.Recordset
rs.Open "select * from qrloanborrowers where loanid=" & IDCode,
CurrentProject.Connection
Do Until rs.EOF
.Selection.MoveRight Unit:=wdCell
.Selection.Paste
.Selection.Find.Execute "«FirstName»", , , , , , , , ,
rs!FirstName
.Selection.SelectCell
.Selection.Find.Execute "«LastName»", , , , , , , , , rs!LastName
.Selection.SelectCell
.Selection.Find.Execute "«NationalID»", , , , , , , , ,
Nz(rs!NationalId)
rs.MoveNext
Loop
End With

there are 2 major problems here.
1) i do not know if the "find" method found the mergefield inside a table cell
2) this is the real problem: the replace method, even though it seems that
it works, it actually only replaces a "cover" for the field, but as soon as
the user touches any mailmerge option, everything changes back to the
original value of the original field.

the goal of all this is, because every contract record has multiple people
involved, and i must have a way to fill multiple records onto 1 mailmerge.
thanks
 
G

Graham Mayor

The following will identify which tables in the document have a mergefield
in the first cell

Dim oCell As Range
Dim i As Long
Dim j As Long
With ActiveDocument
For i = 1 To .Tables.Count
Set oCell = .Tables(i).Cell(1, 1).Range
If oCell.Fields.Count > 0 Then
For j = 1 To oCell.Fields.Count
If oCell.Fields(j).Type = wdFieldMergeField Then
MsgBox "Table " & i & " has a mergefield in cell 1"
End If
Next j
End If
Next i
End With

displayed by a message box. Replace the message box with whatever it is you
want to do having located the table.
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
Y

Yisman

Hi Graham

Thanks a million for your post. it got me rolling on the right track!

heres my final code if anyone will have use of it:

With apWord
Dim oCell As Range
Dim i As Long
Dim j As Long
Dim FieldName As String
For i = 1 To dcMerge.Tables.Count
Set oCell = dcMerge.Tables(i).Cell(1, 1).Range
If oCell.Fields.Count > 0 Then
oCell.Select
.Selection.Copy
Dim rs As New ADODB.Recordset
Dim MainBorrow As Integer
MainBorrow = ESelect("Debtorid", "LoanDebtors", "Loanid=" &
IDCode)
rs.Open "select * from qrLoanDebtors where loanid=" & IDCode
& " and Debtorid<>" & MainBorrow, CurrentProject.Connection
Do Until rs.EOF
.Selection.MoveRight Unit:=wdCell
.Selection.Paste
For j = 1 To .Selection.Fields.Count
.Selection.SelectCell
FieldName = Replace(.Selection.Fields(j).Code,
"MERGEFIELD", "")
FieldName = Trim(Replace(FieldName, Chr(34), ""))
.Selection.Fields(j).Code.Text = " QUOTE " & Chr(34)
& rs.Fields(FieldName) & Chr(34)
Next
rs.MoveNext
Loop
End If
Next i
End With
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top