Free Support Forum - aspose.com

Adding bold to a merge field

Hi,

I was wondering it it was possible to achieve the following?

I have a merge field called <>.

I would like it to contain the following text:

The cat sat on the mat.

As you can see, I only want to make part of the text bold. can this be achieved with Words and vb.net?

Thanks in advance

Hi Joe,


Thanks for your inquiry. First of all, please note that Aspose.Words is quite different from the Microsoft Word’s Object Model in that it represents the document as a tree of objects more like an XML DOM tree. When you load a Word document into Aspose.Words, it builds its DOM and all document elements and formatting are simply loaded into memory. Please read the following articles for more information on DOM:
http://www.aspose.com/docs/display/wordsnet/Object+Model+Overview
http://www.aspose.com/docs/display/wordsnet/Composition+Diagrams
http://www.aspose.com/docs/display/wordsnet/Using+DocumentBuilder+to+Modify+a+Document+Easily
http://www.aspose.com/docs/display/wordsnet/Run+Class

Please use IFieldMergingCallback Interface in your code to achieve your requirement as shown below:

<span lang=“EN-GB” style=“font-size:10.0pt;font-family:“Courier New”;color:blue;mso-font-kerning:
0pt;mso-no-proof:yes”>

Dim doc As New Document("D:\in.docx")

doc.MailMerge.FieldMergingCallback = New HandleMergeField()

doc.MailMerge.Execute(New String() {"FieldName"}, New Object() {"Field Value"})

doc.Save("D:\AsposeOut.doc")

Private Class HandleMergeField

Implements IFieldMergingCallback

Private mBuilder As DocumentBuilder

'''

''' This is called when merge field is actually merged with data in the document.

'''

Private Sub IFieldMergingCallback_FieldMerging(ByVal e As FieldMergingArgs) Implements IFieldMergingCallback.FieldMerging

' All merge fields that expect HTML data should be marked with some prefix, e.g. 'html'.

If mBuilder Is Nothing Then

mBuilder = New DocumentBuilder(e.Document)

End If

If e.FieldName.Equals("FieldName") Then

'Here you can modify code according to your requirements.

mBuilder.MoveToMergeField(e.FieldName)

Dim run As New Run(e.Document)

run.Text = "New text with different font style"

Dim font As Aspose.Words.Font = run.Font

font.Size = 12

font.Bold = True

font.Color = System.Drawing.Color.Red

font.Name = "Verdana"

mBuilder.InsertNode(run)

End If

End Sub

Private Sub ImageFieldMerging(ByVal e As ImageFieldMergingArgs) Implements IFieldMergingCallback.ImageFieldMerging

' Do nothing.

End Sub

End Class