Visio Document Variable - c#

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.

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).

Cleanest approach to using an Excel file as a template for exporting

I have a (winforms) project which allows users to export to an Excel spreadsheet. This exporting is done via the interop (Microsoft.Office.Interop.Excel) and works off a "template" spreadsheet I have created. The template should remain unchanged during the export as the code programmatically saves to a different file path. I want to know what the cleanest approach to this is.
At the moment I have added the template spreadsheet as a file to my project. My understanding is that if I set it's Build Action property to Embedded Resource, I would still need to at least save the file to the user's hard drive at least temporarily in order for the interop to access it. My understanding is that the interop automates an Excel process to open the file. So is it advisable to copy my template out of the embedded resources into the user's temp folder each time they try and export and work from there (i.e. using System.IO.Path.GetTempFileName())? If so, then how do I actually get hold of the embedded resource? I tried looking for it in Properties.Resources but it is not there. Or is there a smarter way to go about this?
Look at the following link, which explains how to save the embedded ressource to file: https://stackoverflow.com/a/864276/342594
As a side note: I would store the file somewhere in the user's folder (e.g. My Documents). Then you could give the user the opportunity to alter the template. You could then first check if the template exists and create it if not. Otherwise use the existing template. If the user broke the template your export method would probably notice it and you could give the user the opportunity to revert to the original template.
I would recommend to use OpenXML (or its a high-level wrapper like ClosedXML), not Microsoft.Office.Interop.Excel.
You can read your embedded resource as array of bytes (somehow like that)
and feed SpreadsheetDocument.Open with it.
// stream "contains" bytes of your embedded template file.
using (var doc = SpreadsheetDocument.Open(stream, true))
{
...
}
After editing a spreadsheet document you can save it anywhere.

Create a Word document from a template file c#

I am trying to make a word document in an asp.Net MVC application using OpenXML template document .
The main challenges for me are
How can i create a word document as an OpenXML template? In my word
document i have some paragraphs of texts and in every paragraphs i
have to fill information from data base like in word file there are
instances of text like
etc and these should be filled with actual data. But
i dont know how can i convert a normal word document as a OpenXML
template file .
How can i fill the values with data from db ? If i have a model say WordModel in hand with filled values of properties FirstName TotalAmount AmountUnit TotalCopies etc then how can i fill the details to template and allow user to download the file ?
What you want to do is called Mail Merge and means populating word documents that act as templates with your application data. There are some existing (commercial) solutions out there, but if you want to do it yourself using Open XML SDK, you need to set up some kind of tagging mechanism that you will use to tag certain parts of document where you want the data/text from the database should be placed. For Word documents you have the following options: Bookmarks, Content Controls, Merge Fields, or special text (<% FirstName %>). I would personally go with Content Controls as they offer the best user experience and they are pretty easy to parse and replace. So the templates would be ordinary word documents containing these tags and then you could use Open XML SDK to parse these templates, search the tags in them and replace them with your application data according to the tag's name/code/title. This a very abstract, high level picture of a mail merge processor. Of course system like this is not easy to implement and also note that using Open XML requires some learning. There was a similar question answered, but you can probably find many more - just google.

Change blank fields in .doc file. WPF

I have a .doc document. In this document i have some blanks for data. For example:
"car_id" is the best car in "car_country".
I need to open this doc file and change this blanks ("car_id", "car_country") to data from some object.
How to do this?
I would use DocumentFormat.OpenXml.dll. You can find it here. OpenXMLSDKv2.msi will add assembly. You will just need to add reference to DocumentFormat.OpenXml. And the OpenXMLSDKTool.msi will install a usefull tool that will display xml structure of .docx (for example) document.
This web page has some very good samples of Word automation using C#. Specifically, look at section 6 in the web page for a Mail Merge example.
http://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx

How to replace content in template docx document and Open XML SDK 2.0 (Aug 09)?

I have a "template" docx document which contains the desired layout, and wish to insert content using C#, but I cannot find a way to uniquely address specific sections of the document, such as paragraphs or tables.
What is the best way to uniquely identify elements in the document?
Thanks,
Matt Sharpe.
How is your template built? Does it use an underlying XML Schema loaded as part of the *.docx? Or are you using content controls off of the Developer ribbon, in which case each control is uniquely identified by a given tag name? Both of these approaches would make identifying certain sections of your document easier as you could control where tables or paragraphs would be.
Also, you may want to consider using the Open XML SDK 2.0 (uses .NET 3.5). It includes a handy Document Reflector tool that allows you to open up and inspect any Open XML document and shows how to generate the code for any element you click on.
Apart from that, to learn more about content controls you can check these posts:
Generating Microsoft Office Documents with the Open XML SDK
Creating Data-Bound Content Controls using the Open XML SDK and LINQ to XML
Can you use document variables/fields? Just go to Insert->Quick Parts->Fields->Doc Variable, enter name of variable.
example:
http://www.codeproject.com/KB/office/Fill_Mergefields.aspx
You could also just use placeholder text values like "##insert_first_name##" and then do a search and replace for those variables.
example:
Link
I think you are looking for either bookmarks, or content controls (on the Ribbon's Developer tab, code example here)
I've used Named Ranges in Excel for the same purpose.
There are several options to do this.
I have created a simple open-source library that will replace tags by values.
For example Hi {name} with data={name:"John"} will be replaced by Hi John.
Here it is :
https://github.com/edi9999/docxtemplater
Demo: http://javascript-ninja.fr/docxgenjs/examples/demo.html

Categories