finds the specified text and convert it to mergefield

S

srid

I was able to write a method that finds the specified text and convert it to
mergefield
------------>vb.net code

Dim oApp As New Word.ApplicationClass
Dim appdoc As New Word.Document

Public Sub ChangeTextToMergeField()
'open the document
appdoc = oApp.Documents.Add("C:\FirstName.doc")
oApp.Visible = True

Dim rng As Range = oApp.Selection.Range
'assign range to find
Dim fnd As Word.Find = rng.Find
'merge fields name
Dim fieldname() As String = {"FirstName", "LastName", "Addr1",
"Dear", "SalesAsso"}
Dim fName As String
Dim outerloop As Integer = 1
Dim Innerloop As Integer = 1

'pass the merge fields name
For Each fName In fieldname

'Clear existing formatting.
fnd.ClearFormatting()
'assign the string--{{fieldname}}
fnd.Text = "{{" & fName & "}}"
'seach throught out the document
fnd.Wrap = WdFindWrap.wdFindContinue

' Execute the search--->
Do While fnd.Execute
'slect the range
rng.Select()
'add the merge field
appdoc.MailMerge.Fields.Add(rng, fName)
'increment the counter--optional
Innerloop = Innerloop + 1
Loop

Innerloop = 1
'increment the counter--optional
outerloop = outerloop + 1

Next
'save the file
appdoc.SaveAs("c:\f1.doc")
oApp.Quit()

End Sub

may be useful to someone
 

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