Free Support Forum -

Insert Hyperlink at MailMerge Field

My routine to isolate the fields works. The field names returned from the database match ones in the doc but when I use the inserthyperlink method it moves to a random location in the doc and then inserts this:

<!–[if gte mso 9]>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
<![endif]–><span style=“font-size:11.0pt;mso-bidi-font-size:
Calibri;mso-bidi-font-family:“Times New Roman”;mso-ansi-language:EN-CA;
mso-fareast-language:EN-US;mso-bidi-language:AR-SA” lang=“EN-CA”>HYPERLINK \l"

Which is obviously not a working link. And then the text appears in another random location. Regardless the links don’t work. What is the fix?

My insert method:

Public Sub InsertHyperlink(ByVal id_sales As String, ByVal sproc As String, ByVal doc As Document, ByVal builder As DocumentBuilder)

Dim sField As String, sValue As String, sType As String
Dim dt As DataTable = Nothing

'builder.Font.Color = Color.Blue
'builder.Font.Underline = Underline.Single
'builder.Font.UnderlineColor = Color.Blue

Using myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings(“cnn_BC”).ConnectionString)
Dim myCommand As New SqlCommand(sproc, myConnection)
myCommand.CommandType = CommandType.StoredProcedure

myCommand.Parameters.Add(New SqlParameter(”@ID", SqlDbType.Int))
myCommand.Parameters("@ID").Value = CInt(id_sales)

Dim r As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

Do While r.Read
For i As Integer = 0 To r.FieldCount
sField = r.GetName(i).ToString
sValue = r.GetValue(i).ToString.Trim
sType = r.GetFieldType(i).ToString


’ Specify font formatting for the hyperlink.
builder.Font.Color = Color.Blue
builder.Font.Underline = Underline.Single
’ Insert the link.
builder.InsertHyperlink(“Aspose Website”, “”, True)

’ Revert to default formatting.

Catch ex As Exception
End Try

End Using

Catch myException As Exception
End Try

End Sub<!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Calibri","sans-serif"; mso-bidi-font-family:"Times New Roman";}


Hi Rob,

Thanks for your inquiry. DocumentBuilder.InsertHyperlink method inserts a hyperlink into the document. Please use third parameter value as “false” as shown in following code snippet.

Third parameter : isBookmark True if the previous parameter is a name of a bookmark inside the document; false is the previous parameter is a URL.

<span lang=“EN-GB” style=“font-size:10.0pt;font-family:“Courier New”;color:#2B91AF;mso-font-kerning:
0pt;mso-no-proof:yes”>Document<span lang=“EN-GB” style=“font-size:10.0pt;
font-family:“Courier New”;mso-font-kerning:0pt;mso-no-proof:yes”> doc = new Document();<o:p></o:p>

DocumentBuilder builder = new DocumentBuilder(doc);

builder.Write("Please make sure to visit ");

// Specify font formatting for the hyperlink.

builder.Font.Color = Color.Blue;

builder.Font.Underline = Underline.Single;

// Insert the link.

builder.InsertHyperlink("Aspose Website", "", false);

// Revert to default formatting.


builder.Write(" for more information.");

doc.Save(MyDir + "DocumentBuilder.InsertHyperlink Out.doc");

Please let us know if you have any more queries.

tried setting it to false, makes no difference.

Hi Rob,

Thanks for your inquiry. I have tested the scenario and have not found any issue with InsertHyperlink method while using latest version of Aspose.Words for .NET. Please use the latest version of Aspose.Words for .NET. If you still face problem, please share your working environment like Operating System, .NET framework version etc.

I have attached the output document file with this post for your kind reference.