Using { FILENAME \* MERGEFORMAT }

Hi there,

Not 100% if this is related to the UpdateFields issue (with fields not updating). I have read through the FAQ and the doco with only two properties being updated with this method - just confirming that the Filename merge code actually fits in there.

Basically the problem is when I use the code { FILENAME \\* MERGEFORMAT } in a document, when I do a merge, the actual value in this is the Word doc I did the merge from (template) and not the merged document. When I update fields in Word - yep - I get the right value.

So my question is if this issue is related to UpdateFields. Will calling UpdateFields update that value at some point?

I have got a workaround, but it seems a bit dodge:

Dim WordFile As String = "c:\in\_doc.doc"
Dim OutputFile As String = "c:\out\_doc.doc"
Dim aspDoc As New Aspose.Words.Document(WordFile)
'
For i As Integer = 0 To aspDoc.GetChildNodes(NodeType.Run, True).Count - 1
Dim node As Node = aspDoc.GetChildNodes(NodeType.Run, True)(i)
If (node.GetText.Contains("FILENAME")) Then
' if we're here, we may have a FILENAME merge field.
' get the next node which is the actual file.
Dim nextNode As Node = aspDoc.GetChildNodes(NodeType.Run, True)(i + 1)
Dim onlyNameIn As String = IO.Path.GetFileName(WordFile)
Dim onlyNameOut As String = IO.Path.GetFileName(OutputFile)

' see if the next is the word file.
' do two checks - one for the entire file name (inc dir), one for just the name.
If (nextNode.GetText.Contains(WordFile)) Then
nextNode.Range.Replace(WordFile, OutputFile, False, True)
ElseIf (nextNode.GetText.Contains(onlyNameIn)) Then
nextNode.Range.Replace(IO.Path.GetFileName(WordFile), onlyNameOut, False, True)
End If
End If
Next

Code has been simplified, but is mainly what it does - obviously doesn’t work at all if there is any formatting or anything like that for the field though.

Hi Tyron,

Thanks for your inquiry. Actually, I think, the problem is a bit deeper. Aspose.Words cannot know what will be the name of the output document, because you can specify any file name you like. So FILENAME field should be updated upon saving the document.

I think, as a workaround, you can create custom document property in your document, for example with name FileName. Then you can insert DOCPROPERTY field into your document and update it programmatically. For example, see the attached document and the following code:

// Open input document.
Document doc = new Document(@"Test001\in.doc");
// Update value of teh custom docuemnt property.
doc.CustomDocumentProperties["FileName"].Value = "out.doc";
// Update fields.
doc.UpdateFields();
// Save output document.
doc.Save(@"Test001\out.doc");

Hope this helps.

Best regards.

Hey Alexey,

Thanks for that - just tried it out, and yep - does the trick.
Only bad thing is we would have to change existing documents to use this method, but it’s an option.

Along with the above code - might just work out as a workaround for the moment.

Cheers for the help!

I’ve been using a similar “hack” for a while now:

.code0 { font-size: small; font-family: Consolas, "Courier New", Courier, Monospace; color: #000000; background-color: #ffffff;margin: 0em; } .code1 { color: #0000ff; font-weight: bold; } .code2 { color: #ff0000; } .code3 { color: #191970; font-weight: bold; } .code4 { color: #006400; } .code5 { color: #000000; } .code6 { color: #008b8b; font-weight: bold; } .code7 { color: #008000; } .code8 { color: #000080; } 
public void SetFileName(string filename)
{
	foreach (FieldStart fieldStart in \_document.GetChildNodes(NodeType.FieldStart, true))
	{
		switch(fieldStart.FieldType)
		{
			case FieldType.FieldFileName:
				Run filenameMergeformat = (Run)fieldStart.NextSibling; // Text=FILENAME \\* MERGEFORMAT
				FieldSeparator fieldSeparator = (FieldSeparator)filenameMergeformat.NextSibling; // Separator
				Run filenameCurrentValue = (Run)fieldSeparator.NextSibling; // Run Text=
				filenameCurrentValue.Text = filename; 
				break;
			default:
				break;
		}
	}
}

Thanks for that post Polarbear!
I found something similiar not too long ago at this link:
https://forum.aspose.com/t/123329

Basically we found another issue where auto dates in documents didn’t update (they did when a merge to a doc or docx was done, but not when the doc printed or merged to pdf).

Hi

Thanks for your request. MS Word automatically updated DATE fields in the document, but upon converting to PDF or Printing, Aspose.Words does not update them.

I think, you can try using code provided in the following forum thread to update DATE fields:

https://forum.aspose.com/t/110953

Hope this helps.Best regards