Replacing merge fields with our values in code
I have this Text: Hello <<FirstName>> <<LastName>>
in which the <> and <> are merge fields in a document, and I have a list of customers in an array which I want to allocate them开发者_如何学Go to these fields. How do I do that?
I don't know how you have your array structured, but:
Dim text As String = "Hello <<FirstName>> <<LastName>>"
Dim foo() As String
For i As Integer = 0 To UBound(foo)
Dim modifiedText As String = text
modifiedText = Replace(modifiedText, "<<FirstName>>", foo(i,0), 1, -1, vbTextCompare)
modifiedText = Replace(modifiedText, "<<LastName>>", foo(i,1), 1, -1, vbTextCompare)
Console.WriteLine(modifiedText)
Next
It looks like you want to merge the field values in a Word document using VB.NET. If I'm correct, you may try Aspose.Words for .NET. This component provides a comprehensive solution for such merging scenarios.
Here is a simple code snippet as well:
' Open an existing document.
Dim doc As New Document(MyDir & "MailMerge.ExecuteArray.doc")
' Fill the fields in the document with user data.
doc.MailMerge.Execute(New String() {"FullName", "Company", "Address", "Address2", "City"}, New Object() {"James Bond", "MI5 Headquarters", "Milbank", "", "London"})
' Send the document in Word format to the client browser with an option to save to disk or open inside the current browser.
doc.Save(Response, "PersonalizedLetter Out.doc", ContentDisposition.Inline, Nothing)
Disclosure: I work as developer evangelist at Aspose.
精彩评论