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:

HYPERLINK \l "http://www.aspose.com"

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.PushFont()
'builder.Font.Color = Color.Blue
'builder.Font.Underline = Underline.Single
'builder.Font.UnderlineColor = Color.Blue

Try
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)

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

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

builder.MoveToMergeField(sField)

’ Specify font formatting for the hyperlink.
builder.Font.Color = Color.Blue
builder.Font.Underline = Underline.Single
’ Insert the link.
builder.InsertHyperlink("Aspose Website", "<http://www.aspose.com>", True)

’ Revert to default formatting.
builder.Font.ClearFormatting()

Catch ex As Exception
'expected
End Try
Next
Loop

r.Close()
myConnection.Close()
End Using

Catch myException As Exception
End Try

End Sub

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.

Document doc = new Document();
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", "http://www.aspose.com", false);
// Revert to default formatting.
builder.Font.ClearFormatting();
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.