I am struggling on this for days! I am trying to insert/modify a link in my template document to insert links to network folders and network files.
Everything I try, it converts the network location to a web link and it adds "https://localhost:44344/" in front of my folder location
public void MergeFieldEvent(object sender, MergeFieldEventArgs args)
{
if (args.FieldName.EndsWith("_Link"))
{
InsertHyperlink_MergeField(sender, args);
if (true == false)
{
WParagraph paragraph = args.CurrentMergeField.OwnerParagraph;
int paraIndex = paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph);
int paraItemIndex = paragraph.ChildEntities.IndexOf(args.CurrentMergeField);
string html_string = "<a rel='nofollow' href=\"" + args.FieldValue + "\" >test</a>";
paragraph.OwnerTextBody.InsertXHTML(html_string, paraIndex, paraItemIndex);
}
}
}
private static void InsertHyperlink_MergeField(object sender, MergeFieldEventArgs args)
{
// Applies the hyperlink style to the merge field.
IEntity entity = args.CurrentMergeField.NextSibling;
WFieldMark separator = null;
//Iterates to the separator following the merge field.
while (entity != null && !(entity.EntityType == EntityType.FieldMark))
{
//Get next sibling item.
entity = (entity as ParagraphItem).NextSibling;
}
separator = entity as WFieldMark;
WFieldMark nextSibling = separator.NextSibling as WFieldMark;
//If need to preserve formats,
//then sets format for resultant text, i.e., sibling item of separator.
if ((args.CurrentMergeField.Document.Settings.MaintainFormattingOnFieldUpdate
|| args.CurrentMergeField.FieldCode.Contains("\\* MERGEFORMAT"))
&& (separator != null && separator.Type == FieldMarkType.FieldSeparator)
&& !(nextSibling != null && nextSibling.Type == FieldMarkType.FieldEnd))
(separator.NextSibling as ParagraphItem).ApplyStyle("Hyperlink");
//If not need to preserve formats while updating,
//then set formats for merge field directly.
else
(args.CurrentMergeField as WMergeField).ApplyStyle("Hyperlink");
// Inserts hyperlink during Mail merge.
ParagraphItemCollection items = null;
int index = 0;
if (args.CurrentMergeField.Owner is WParagraph)
{
WParagraph paragraph = args.CurrentMergeField.OwnerParagraph;
items = paragraph.Items;
index = items.IndexOf(args.CurrentMergeField);
}
else if (args.CurrentMergeField.Owner is InlineContentControl)
{
InlineContentControl inlineContentControl = args.CurrentMergeField.Owner as InlineContentControl;
items = inlineContentControl.ParagraphItems;
index = items.IndexOf(args.CurrentMergeField);
}
if (items != null)
{
//foreach(Entity en in items)
//{
// if(en.EntityType == EntityType.Field)
// {
// if ((en as WField).FieldType == FieldType.FieldHyperlink)
// {
// Hyperlink hyperlink = new Hyperlink(en as WField);
// string FilePath = @"\\xxxxxxxxxxxx\yyyyyyyyyyyyyy\zzzzzzzzzzzz";
// //hyperlink.Uri = (string)args.FieldValue;
// hyperlink.FilePath = FilePath.Replace("\\", "\\\\");
// //hyperlink.Uri = (string)args.FieldValue;
// hyperlink.TextToDisplay = "testing new name";
// break;
// }
// }
//}
// Creates new hyperlink field.
WField hyperlinkField = new WField(args.Document);
hyperlinkField.FieldType = FieldType.FieldHyperlink;
Hyperlink hyperlink = new Hyperlink(hyperlinkField);
hyperlink.Type = HyperlinkType.FileLink;
hyperlink.FilePath = @"\\xxxxxxxxxxx\yyyyyyyyyyyyyy\zzzzzzzzzzzz";
hyperlink.TextToDisplay = "test text display";
items.Insert(index, hyperlinkField);
index++;
WTextRange textRange = new WTextRange(args.Document);
//textRange.Text = " HYPERLINK \"" + ((string)args.FieldValue).Replace("\\", "\\\\") + "\" ";
textRange.Text = " " + args.FieldValue;
items.Insert(index, textRange);
index++;
// Creates field separator mark for hyperlink field.
WFieldMark fieldSeparator = args.Document.CreateParagraphItem(ParagraphItemType.FieldMark) as WFieldMark;
fieldSeparator.Type = FieldMarkType.FieldSeparator;
items.Insert(index, fieldSeparator);
index += 5;
// Creates field end mark for hyperlink field.
WFieldMark fieldEnd = args.Document.CreateParagraphItem(ParagraphItemType.FieldMark) as WFieldMark;
fieldEnd.Type = FieldMarkType.FieldEnd;
items.Insert(index, fieldEnd);
}
}