How to set value in fields like {DOCVARIABLE SomeName \* MERGEFORMAT}
in the MS Word document via OpenXML?
Asked
Active
Viewed 2,753 times
0

Ashkan Mobayen Khiabani
- 33,575
- 33
- 102
- 171

vovkor
- 1
- 1
- 1
-
https://stackoverflow.com/questions/3647024/filling-in-a-docvariable-in-word-docx-using-c-sharp – FortyTwo Mar 31 '19 at 10:45
-
That solution via Microsoft.Office.Interop. I don't have installed MS Word on a computer (this is a server). – vovkor Mar 31 '19 at 12:05
1 Answers
0
The document variables are stored as DocumentVariable within the DocumentVariables elment inside the Settings element in the DocumentSettingsPart
Thus you can retreive a Documentvariable as follows:
/// <summary>
/// Retrieves a document variable by its name
/// </summary>
/// <param name="name">The name of the document variable (case sensitive)</param>
/// <param name="document">The wordprocessing document</param>
/// <returns>The document variable if found, other wise it returns null</returns>
public DocumentVariable GetVariableByName(string name, WordprocessingDocument document)
{
// Get the document settings part
DocumentSettingsPart documentSettings = document.MainDocumentPart.DocumentSettingsPart;
// Get the settings element
Settings settings = documentSettings.Settings;
// Get the DocumentVariables element
DocumentVariables variables = settings.Elements<DocumentVariables>().FirstOrDefault();
// check if the variables are not null
if(variables != null)
{
return variables.Elements<DocumentVariable>().Where(v => v.Name == name)
.FirstOrDefault();
}
return null;
}
When you retrieved the variable you can change the value of the variable as follows:
/// <summary>
/// Sets the value of a document variable
/// </summary>
/// <param name="variable">The variable</param>
/// <param name="value">The value</param>
public void SetDocumentVariableValue(DocumentVariable variable, string value)
{
variable.Val = value;
}
Putting this all together in a simple console program the value of a document variable inside a WordProcessing document can be changed as follows:
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System.Linq;
namespace ChangeDocVariable
{
class Program
{
/// <summary>
/// Retrieves a document variable by its name
/// </summary>
/// <param name="name">The name of the document variable (case sensitive)</param>
/// <param name="document">The wordprocessing document</param>
/// <returns>The document variable if found, other wise it returns null</returns>
public static DocumentVariable GetVariableByName(string name, WordprocessingDocument document)
{
// Get the document settings part
DocumentSettingsPart documentSettings = document.MainDocumentPart.DocumentSettingsPart;
// Get the settings element
Settings settings = documentSettings.Settings;
// Get the DocumentVariables element
DocumentVariables variables = settings.Elements<DocumentVariables>().FirstOrDefault();
// check if the variables are not null
if(variables != null)
{
return variables.Elements<DocumentVariable>().Where(v => v.Name == name)
.FirstOrDefault();
}
return null;
}
/// <summary>
/// Sets the value of a document variable
/// </summary>
/// <param name="variable">The variable</param>
/// <param name="value">The value</param>
public static void SetDocumentVariableValue(DocumentVariable variable, string value)
{
variable.Val = value;
}
static void Main(string[] args)
{
string path = @"This should be the path to your document";
using(WordprocessingDocument document = WordprocessingDocument.Open(path, true))
{
DocumentVariable variable = GetVariableByName("TestVariable", document);
if(variable != null)
SetDocumentVariableValue(variable, "New Value");
// Or access the value directly
// variable.Val = "New Value";
document.Save();
}
}
}
}

Discobarry
- 64
- 3