I need to add a bunch of DOCVARIABLES to a word document for my work. The end goal is to use C# to easily ask a user what values need to go into those variables, then automatically generate the word document from the template with empty variables.
Despite having gone through and added all the field codes for the variables, when I save the document it doesn't actually initialize any of the document variables to a state where they exist, and so when I use C#, it can't find any of them. When I check the docx as a zip file, there are no <w:docVars>
tags in the settings.xml file. When I add the DOCVARIABLES using the C# instead of directly through word, those tags are added.
All of the solutions I've found so far involve using a VBA Macro to initialize them to something, but that isn't an option here because, for whatever reason, we aren't allowed to use VBA Macros at all because corporate something something. There is some restriction on the computers that corporate requires them to have that entirely prevents me from running those macros, and trying to bypass that is not an option.
How do I get the DOCVARIABLES to initialize to some initial value that will allow the C# to find them?
DocVariables cannot be blank. If you set one to a null value, it is removed.
As a workaround set an initial value to “Empty” and then have your code check for that value and take appropriate action. Otherwise, just simple check if the DocVariable exists and take appropriate action if it does not.
Update: DocVariables must be created from a macro. The macro can be in the document, a document template, or a global add-in. All require permission to run and if the IT department won’t approve any of those solutions, then DocVariables are not really an automation option.