How to get the field code view of a Word stream to display mergefield code in html

Hello,

Is that possible to load a Word document from a binary tab and get the ALT F9 view of the Word document in a string ?

I achieved to diplay from a stored document :
<<myMergeField>>
but I rather have
{ MERGEFIELD myMergeField \* MERGEFIELD } shown

The main point for I is to be able to show the user the conditionnal IF instead of the view of the mergefield if :
{ IF "1"="HelloWorld" "Test" "Hello" }
instead of 1 1 1

Thanks in advance.
Regards.

Hi Emilien,

Thanks for your inquiry. Please use the following code snippet to get the field as <>. Hope this helps you . Please let us know if you have any more queries.

///

/// Retrieves the field code from a field.
///
/// The field start of the field which to gather the field code from
///
private static string GetFieldCode(FieldStart fieldStart)
{
    StringBuilder builder = new StringBuilder();
    for (Node node = fieldStart; node != null && node.NodeType != NodeType.FieldSeparator &&
        node.NodeType != NodeType.FieldEnd; node = node.NextPreOrder(node.Document))
    {
        // Use text only of Run nodes to avoid duplication.
        if (node.NodeType == NodeType.Run)
            builder.Append(node.GetText());
    }
    return builder.ToString();
}
private static string GetMailMergeField(FieldStart fieldStart)
{
    StringBuilder builder = new StringBuilder();
    Node Separator = fieldStart.NextPreOrder(fieldStart.Document);
    while (Separator.NodeType != NodeType.FieldSeparator)
        Separator = Separator.NextPreOrder(fieldStart.Document);
    for (Node node = Separator; node != null &&
        node.NodeType != NodeType.FieldEnd; node = node.NextPreOrder(node.Document))
    {
        // Use text only of Run nodes to avoid duplication.
        if (node.NodeType == NodeType.Run)
            builder.Append(node.GetText());
    }
    return builder.ToString();
}
Document doc = new Document(MyDir + "in.docx");
FieldStart fStart = (FieldStart) doc.GetChild(NodeType.FieldStart, 0, true);
string field = GetMailMergeField(fStart);

Hello, thanks for your quick reply. Sadly it doesn’t solve my matter.

I got that done already but i would like to get the mergiefield real value :
It’s that possible to get the { MERGEFIELD myMergeField \* MERGEFIELD } string ? Instead of <<myMergeField>> ?

Indead the resultat of << >> for conditionnal mergefield (IF) doesn’t work fine.

My aim is to display a text to my user (in HTML format) like that :
"Dear {MERGEFIELD name \* MERGEFIELD },
hello world {MERGFIELD aText \* MERGEFIELD }
"

(Not
"Dear <<name>>,
hello world <<aText>>")
So my user can edit his word model in a textArea ?

Thanks in advance.

Hi Emilien,

Thanks for your inquiry. I share two method in my last post.

  1. GetFieldCode: This method will return the value as MERGEFIELD myMergeField \* MERGEFORMAT

  2. GetMailMergeField: This method return the value as <<myMergeField>>

So, in your case, I suggest you to use GetFieldCode method. Hope this helps you. If you still face problem, please manually create your expected input and output documents and attach here for reference. I will share the code according to your requirements.

Oh, sorry I didn’t catch that earlier.
I’m going to try it.
Thank you very much.

Hi Emilien,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.