Open XML Tutorial clarification - c#

Trying to learn how to use OOXML to replace tokens win a word document by following this tutorial
Everything has been going pretty well until I hit this
Next, create the custom XML file containing the data for this document, and store it in the package.
Store the custom XML in memory and add placeholders for the actual data.
I'm having trouble discerning where to store the custom XML and what he means by storing it in memory.
The author also continues to refer to the file as package, so does that mean I should keep the word Template in .zip format?

Next, create the custom XML file containing the data for this document, and store it in the package.
In context, I would suggest the package is the file containing the xml data, which when you load it or display it becomes a document.
Storing it in memory, means loading an object with the data(i.e. xmldocument)

Related

Where to store json string in word document?

I would like to implement WordAddin.
I need to have json list related in my word document.
My functionality search word document and base on some criteria crate json string.
I would like to store this json string in word document and use it later if needed. It would be perfect to easy store and easy get this json.
I want have all data in word document, so separate files or database are not options.
My question is: where to store this json string?
Possible solutions I can see are :
Save on document as text and then hide and protect this text
Save to file and attach this file to word document (protect and hide)
Is there some better place where could I store data like this in word document?
Each Microsoft Word document has metadatas and properties. I would recommend to store your Json into one of these.
Here's some examples on how to do it:
MSDN
Accessing Excel Custom Document Properties programmatically
How do I add custom properties to a Word doc with .NET 4?
Note that properties can be read by the users (if they know what they are searching for).

Visio Document Variable

I would like to store variable data inside of a Visio file similar to how you can in a Word file, but I am unable to find anything similar for Visio. A word example would be:
WordDocument.Variables("myVar").Value = "myVariable";
Alternatively, can you store a file (xml for instance) inside of a Visio file, then read and write to the file at run time?
First option is already voiced by #Jon Fournier. DocumentSheet is a Visio way to store document-specific values. Check out also this article, which gives more details: http://visualsignals.typepad.co.uk/vislog/2011/11/shapes-with-global-values.html
The second option could be Document.SolutionXmlElement, which allows you to store arbitrary XML fragment in Visio file for a document.
https://msdn.microsoft.com/en-us/library/office/aa218416.aspx
Third option could be (note that this is a bit archaic) to create a hidden master and store document data in there (in it's ShapeSheet).
Note that Visio does not support "CustomDocumentProperties" the way some other Office application do. See more information here: https://social.technet.microsoft.com/Forums/office/en-US/85fbc601-1612-4e63-91f4-b900a59109bd/how-to-access-visio-document-properties-builtindocumentproperties-customdocumentproperties-from?forum=visiogeneral
Although there are also some "normal" office document properties which are exposed via API; likte "Title" and "Company" which can be accessed directly as document properties (i.e. "Document.Company"), user custom ole compound document properties not exposed directly and are not available with Visio API. If you are interested in that you might find out this topic useful: http://visguy.com/vgforum/index.php?topic=6535.0
You can use the Document's DocumentSheet, which is a normal shapesheet object, so you can add User cells and store your data there.

Can an XML file be included in a Visual Studio 2010 project and then referred to in code?

Is it possible to store an actual XML file in a Visual Studio 2010 project and then refer to it in code?
ie. Can I store an XML file (see fragment below) and then refer to data elements within the XML data? How can this be done and how would you refer to data elements deep within the XML data using C# or VB code?
If the XML file is added to the project, will it be embedded into the DLL or EXE when compiled? If not, how can it be embedded into the DLL or EXE?
<readReferenceDataResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<readReferenceDataResult xmlns="http://test.website.com/hi/xsd/providermessages/ReadReferenceData/5.6.9">
<elementReferenceValues>
<elementName>organisationTypeCode</elementName>
<referenceSet>
<referenceCode xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">1678</referenceCode>
<referenceDescription xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">Accupuncture and Eastern Medicine</referenceDescription>
</referenceSet>
<referenceSet>
<referenceCode xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">6454</referenceCode>
<referenceDescription xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">Technical and Laboratory Services</referenceDescription>
</referenceSet>
<referenceSet>
<referenceCode xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">9782</referenceCode>
<referenceDescription xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">Naturopathy and Natural Health</referenceDescription>
</referenceSet>
<referenceSet>
<referenceCode xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9>8557</referenceCode>
<referenceDescription xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">Aged Care Services</referenceDescription>
Just add a resource file (.resx) to your project if one is not already present. Select File as the resource type and drag your XML file into that and give it a name (if you don't like the automatically generated one).
This will create an embedded resource type which you can then access in code by the automatically generated class representing the resource, so for example if your project is called Foo and you add a resource file called Resources and add an XML file resource called MyData, then you can access that resource at Foo.Resources.MyData in code. This will expose the contents of the file as a string, which you can then load into an XmlDocument object when you need to by using XmlDocument.LoadXml.
By the sounds of it, the best solution I can think of would be using XmlDocument
You use the XmlDocument to hold the XML file you want to read and then you can retrieve the elements/nodes from there.
If you wanted something a bit faster you can also use Linq to XML, which is nice and simple to use.
Finally, if you have memory constraints/don't want to load the whole document into memory to read it every so often you can use XmlReader. Which allows you to read it directly from file, node by node. It is however forward only, and is a bit tricky to use.

How to create *.docx files from a template in C#

I have a working ASP.NET MVC web application to manage projects and customers. Now I want to generate a word file for some customers. In this file should be displayed some data about the customer. Every generated file should have the same data and the same design. So I want to craete a new Word Template with the fields and want to fill the placeholders programmatically.
My problem is that I couldn't find a clear way to do that. Does anybody know a good learning resources?
Try this page:
Building Office Open XML Files
Open XML files (docx) are ZIP packages containing XML files. In your case, I would create a copy of your original template, then use the System.IO.Packaging API to open the file and modify it. By opening, the correct XML file and replacing certain placeholders in XML, you should be able to achieve the result you want.
While trying to do the same I have found some libraries to create/edit DOC or DOCX in .Net
GemBox.Document
TemplateEngine.Docx
DocXTemplateEngine
Templater - Nuget
Spire.Doc - Nuget
for the new document generation from a template file (.dot) should be very easy, I think it's a parameter you specify in the word application file open or so, when you pass the path of the .dot file, telling word to create a new .doc based on that file and not edit the actual template document.
for form fields and bookmarks filling, lots of examples online on how to do it from C#, see here:
MS Word Office Automation - Filling Text Form Fields And Check Box Form Fields And Mail Merge

Editing properties of a Word document

I have an byte array of an doc file loaded in memory.
I would like to set custom meta data properties of my Word document then save it back to disk. This will be done on a server so I can’t use VTSO to write an add in.
I need to do this in a 2003 word document. so I can't use open xml.
Have you seen Open XML SDK 2.0? Or you can use System.IO.Packaging API directly.

Categories