0

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?

BigBen
  • 46,229
  • 7
  • 24
  • 40
  • Have you considered CustomDocumentProperties rather than DocVarianbles. – freeflow Jan 07 '21 at 20:42
  • Have you considered using a Custom XML Part and mapped content controls instead of DocVariables? – Timothy Rylatt Jan 08 '21 at 16:13
  • I've considered both of these. Unfortunately the C# package I'm using, Telerik RadWordProcessing, can't read, write, or work with DocProperties in any way. Theres currently an open ticket someone submitted for them to add that functionality. I've ended up going with good old word search. It just looks for the phrase TELERIK in the actual text of the document and then replaces it. – Ilan_Schindler Jan 08 '21 at 17:41

1 Answers1

0

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.

Rich Michaels
  • 1,663
  • 2
  • 12
  • 18
  • How do I set the initial value of the DocVariable in word. Everything I've found says to use macros, which again isn't an option. – Ilan_Schindler Jan 07 '21 at 19:04
  • @Ilan_Schindler how are you going to run something using C# unless they allow macros to run? – Rich Michaels Jan 07 '21 at 19:26
  • The C# is a separate program that uses Telerik RadWordProcessing to access and modify the word document. – Ilan_Schindler Jan 07 '21 at 20:08
  • Then store the data that you would have put manually into the document, if there was a way which there isn’t (you need a macro or an add-in), in a custom document property. When your #C program sees that the DocVariables don’t exist, then check the Custom Properties and create the DocVariables if they are still needed at that time. – Rich Michaels Jan 07 '21 at 21:08
  • Thought of that. Unfortunately Telerik's RadWordProcessing isn't capable of seeing or working with the DocProperties as of now. – Ilan_Schindler Jan 07 '21 at 21:15