Free Support Forum - aspose.com

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]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-CA</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<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”/>
</m:mathPr></w:WordDocument>
<![endif]–><span style=“font-size:11.0pt;mso-bidi-font-size:
10.0pt;line-height:115%;font-family:“Calibri”,“sans-serif”;mso-fareast-font-family:
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
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<!–[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";}

<![endif]–>

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", "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.