Get "StyleName" of MailMerge fieldname

Hi,
I can use “doc.MailMerge.GetFieldNames()” to get the fieldnames, but I want to know what the stylename of each fieldname is.

How can I do this?

I can find the “StyleName” in the font class using:
Font.StyleName

But how can I do this for MergeFields?

Hi Thomas,

Thanks for your inquiry.

Please note that formatting is
applied on a few different levels. For example, let’s consider
formatting of simple text. Text in documents is represented by Run
element and a Run can only be a child of a Paragraph. You can apply
formatting

  1. to Run nodes by using Character Styles e.g. a Glyph Style
  2. to the parent of those Run nodes i.e. a Paragraph node (possibly via paragraph Styles)
  3. you can also apply direct formatting to Run nodes by using Run attributes (Font). In this case the Run will inherit formatting of Paragraph Style, a Glyph Style and then direct formatting.

A field in a Word document is a complex structure consisting of multiple nodes that include field start, field code, field separator, field result and field end. The Start, Separator and End properties point to the field start, separator and end nodes of the field respectively. The content between the field start and separator is the field code. The content between the field separator and field end is the field result.

To get the Font.StyleName of mail merge field, you can use following code snippet. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToMergeField("test", false, false);
FieldStart fStart = (FieldStart)builder.CurrentNode;
Console.WriteLine(fStart.Font.StyleName);
Run run = (Run)builder.CurrentNode.NextSibling.NextSibling;
Console.WriteLine(run.Font.StyleName);

Hi again,
Thanks for getting back to me.
The provided code does not seem to do the trick for me.

The StyleName returned is “Default Paragraph Font”
I was expecting StyleName = “LISTE1”

I have attached the document used, and if you look at the "«K_RåderetHVUdgift_Tekst»" mergefield it has the stylename of “LISTE1”

I use the following piece of code:

Dim tt = New Aspose.Words.Document("C:\Test\TestFlet.docx")
Dim builder As New DocumentBuilder(tt)

Dim okok As List(Of String) = tt.MailMerge.GetFieldNames().ToList()

For Each ff As String In okok
builder.MoveToMergeField(ff, False, False)

Dim fStart As FieldStart = DirectCast(builder.CurrentNode, FieldStart)
Dim asdf = fStart.Font.StyleName

// asdf does not return "LISTE1"
Next

Hope you can help me

Hi Thomas,

Thanks for sharing the document. In this case, please use the ParagraphFormat.StyleName as shown below to get the required output. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "TestFlet.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToMergeField("K_RåderetHVUdgift_Tekst", false, false);
FieldStart fStart = (FieldStart)builder.CurrentNode;
Console.WriteLine(fStart.ParentParagraph.ParagraphFormat.StyleName);

Thanks - that works perfectly

Another question though:

When I have 2 identical MergeFields but each with their own StyleName, eg:

<Person_Name> - stylename is Heading1
<Person_Name> - stylename is Normal

The code you provided is always returning “Heading1”
How can I fix this?

Hi Thomas,

Thanks
for sharing the document. In this case, please move the cursor to the end node of first mail merge field as shown in following code example. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToMergeField("Person_Name", false, false);
FieldStart fStart = (FieldStart)builder.CurrentNode;
Console.WriteLine(fStart.ParentParagraph.ParagraphFormat.StyleName);
builder.MoveTo(fStart.GetField().End);
builder.MoveToMergeField("Person_Name", false, false);
fStart = (FieldStart)builder.CurrentNode;
Console.WriteLine(fStart.ParentParagraph.ParagraphFormat.StyleName);

Hi again,
Your code works, thanks But unfortunatly my previous question wasnt asked correctly.

I have a document with mergefields like this:

<Person_Name> - Header1
<Person_Address> - Normal
<Person_Age> - Normal
<Person_Name> - Normal (this returns Header1)

I tried to move the cursor after each merge field (using MoveTo), thinking the “MoveToMergeField” then would take the second <Person_Name> if the cursor was placed after <Person_Age>, but this is not working.

How can I make it work in this scenario (with 2 other merge fields in between)

Hi Thomas,

Thanks for your inquiry. Could you please share your input document here for testing? I will investigate the issue and provide you more information.

Moreover, it would be great if you please share some more detail about your scenario what exact you want to achieve by using Aspose.Words. We will then provide you more information about your query along with code.

Note that formatting is applied on a few different levels. For example, let’s consider
formatting of simple text. Text in documents is represented by Run element and a Run can only be a child of a Paragraph. You can apply formatting

  1. to Run nodes by using Character Styles e.g. a Glyph Style
  2. to the parent of those Run nodes i.e. a Paragraph node (possibly via paragraph Styles)
  3. you can also apply direct formatting to Run nodes by using Run attributes (Font). In this case the Run will inherit formatting of Paragraph Style, a Glyph Style and then direct formatting.

Sorry, I should have attached the document - i have attached it now

I tried doing something like this (but still the second <Selskab_Navn> returns “LISTE1”

Dim wordFlettefeltList As List(Of String) = doc.MailMerge.GetFieldNames().ToList()
Dim flettefeltList As New List(Of Flettefelt)()

For Each wordFlettefelt As String In wordFlettefeltList
If builder.MoveToMergeField(wordFlettefelt, False, False) Then
Dim fStart As FieldStart = DirectCast(builder.CurrentNode, FieldStart)
Dim typografi = fStart.ParentParagraph.ParagraphFormat.StyleName

If flettefeltList.Exists(Function(f) f.Navn = wordFlettefelt) Then
builder.MoveTo(fStart.GetField().End)
builder.MoveToMergeField(wordFlettefelt, False, False)
fStart = DirectCast(builder.CurrentNode, FieldStart)
typografi = fStart.ParentParagraph.ParagraphFormat.StyleName

flettefeltList.Add(New Flettefelt(wordFlettefelt, typografi))
Else
flettefeltList.Add(New Flettefelt(wordFlettefelt, typografi))
End If
Else
'Merge field not found
End If
Next

Hope you can help

Hi Thomas,

Thanks for your inquiry. Please use the following code example to achieve your requirements and let us know if you have any more queries.

Dim doc As New Document(MyDir & "TestFlet.docx")
Dim builder As New DocumentBuilder(doc)
For Each field As Field In doc.Range.Fields
Dim fStart As FieldStart = field.Start
Console.Write(field.GetFieldCode() & " -- ")
Console.WriteLine(fStart.ParentParagraph.ParagraphFormat.StyleName)
Next