How to insert custom control into Office Word document - c#

I want to insert my custom control into the currently opened Word document using VSTO. I have an application which communicates with my already existing VSTO Word plugin over COM. So far i have been using a workaround where i inserted pictures of my custom control as a InlineShape (as images) into the document. This was fine for now, since Word was automatically saving it inside the document, took care of resizing, deleting with the delete key, drag droping image as a singe character and so on... But now i am encountering some problems regarding adding new functionalities (eg. Right click on control (image) click). Now i am looking for an alternatives, how to insert custom control (probably an ActiveX control?) .
I have created an example when i insert a custom control with the Microsoft.Office.Tools.Word.Document.Controls.AddControl() function, but here are some problems:
The control can not be dragged around the range of the document
The control is not saved when the document is closed and reopened
Control does not work when the document zoom is not 100%
See the picture below:
I have also noticed, that i can insert an object into the current word document (insert -> object -> eg. Microsoft Word Document, see picture below)
This inserted object has all the functionality which i need (drag-drop as a single character, saving on document close, resize, functionality when document zoom is not 100%,...) This is exactly what i want to achieve, but instead of another Microsoft Word object, I would like to display my custom control. See the picture below for comparison between MS Word Document object and my custom control inside opened document:
Now i have already spent quite some time googling and doing the research but i am running out of ideas on how to successfully create this to work... If anyone is familiar with the solution of could provide any details on it, it would be extremelly helpful! I have also tried doing something with the new plugins Office add-ins, but keep in mind that i am developing a Windows app, i do not need crossplatform functionality and Word plugin needs to be accessible from the other app (just like current solution, where i access it using COM). Hopefully the explanation was clear enough, i am more than ready to provide additional information if needed!

Related

How can I put an interactive diagram inside of a Word (.docx) document?

I know this question may seem a bit vague, but I'm looking to output a .docx file (using Office Open XML) that contains an interactive diagram that can accept user input and use the entered data to update fields within the document.
I work as a Railway Design Engineer, and I'm experimenting with a system that will be able to automate some of what we do. The image below shows a typical front sheet for a component known as a Crossing...
At the current time, the diagram is static (i.e. it contains no interactive elements), and can't be used with any VBA macros or anything else. The thing is, the values in the table below the diagram are determined by the values within the diagram, but we currently have to enter everything by hand.
So my question is, is there a way to insert a control to substitute this diagram(preferably written using C#) inside of a .docx file and make it easy to update the information within either externally (via Office Open XML) or internally (via VBA macros)? The thing is, whilst I want my system to initially generate the document, I want the user to be able to edit it outside of the program and have it automatically calculate the information within the table from the information within the diagram.

Printing documents contained in a sharepoint document library

I need a way to print selected documents in a library from the ribbon. As I understand it, this will have to be done through scripting. So the main difficulty seems to be the fact that everything will need to be done client-side. Another not so tiny factor is that it has be a generic document printing button, not just for pdf or office documents.
Is there any way to tell the OS/browser to launch for each of the selected items/documents its appropriate program's printing options/window?
Also I want this to be generic to document libraries so I can't just map one as a remote disc.
Another solution that springs to mind is to transfer the selected documents to another document library under the care of an event handler that runs the neccessary c# commands for printing when a new item is added to said(hidden) document library.
Another thing that comes to mind is to basically reiterate what most print drivers already do with the Print to file option where they create a pdf document of the intended material then it would be just a matter of printing the created pdf. But then the printing applications already know the file they have is in the correct/accepted format and merely have to make the convertion to pdf.
Is there any way to use the OS to just open/print-to-file my document with the correct application?
Any help with the script or c# part would be great. Thank you.

How to read metadata information from docx documents?

what I need to achieve is to have a word document template(docx), which will contain Title, Author name, Date, etc.
This template then will be used by users to complete it. I need to create a c# program, that will take in the docx file and read all the information of interest(title, name, date, ..).
So my questions are:
How do I put the metadata into the template saying: this is Title, this is Date, this is Name, etc? (not programatically)
How do I programmatically read that information?
One way to approach this would be to use Content Controls. In Office, you can create your template, and then for each of your respective inputs of interest you can place one of these controls. They're under the Developer tab in Office.
After inserting your controls you'll need for each of them to have a unique name. Office will let them all have the same name, but you'll need to uniquely identify all of them in your template document.
You now need to get the data that's input in to these controls. Again, there's likely to be some better solutions but Eric White has all kinds of great OpenXML stuff, and so here's one of his: Iterating over Content Controls
I think there's problems with finding content controls nested within a table. So, if you do that, then I think you have to specifically loop over the elements of the table to find content controls within.
Also, you're probably going to want to save a .docx from your .doct file, which I don't think there's any built-in "one-liner" method in OpenXML; however, you can create a new Word document, and then write the file stream of the template in to the newly created docx file. Again, of course, there may be better solutions out there.
Have you been here? There's lots of good stuff:
Introduction to OpenXML
Additionally, Eric has been releasing more and more videos on the OpenXML YouTube channel
1) how do I put the metadata into the template saying: this is Title,
this is Date, this is Name, etc? (not programatically)
You could do that on Info tab in MS Word 2010 as shown below:
2) how do I programmatically read that information?
Once you created your document (or template) you could always look inside it with Open XML SDK 2.0 Productivity Tool (wich is installed with OpenXML SDK) to see where (what classes to use) to get/set some information from/to document.
Also I think this post might help you to solve your task:
Add and update custom document properties in a docx
UPDATE:
Hi Dave,
Please have a look at this MSDN Article - Retrieving Application Properties from Word 2010 Documents by Using the Open XML SDK 2.0
Hope this is exactly what you are looking for.
All OpenXML documents have built in core Metadata that will do what you need through System.IO.Packaging. Once you open the word file using the open xml sdk in c#, you can get to these values via the PackageProperties class. There are 11 Properties you can use.
You "encourage" your user to enter the metadata using Word's Document Information Panel (DIP).
You can force this on by default when they open your template, by a setting in the Developer Toolbar for the template. See the following article on how to set this in your template.
I wrote a quick Windows Form app that displays this information using open xml sdk call to the PackageProperties of the Word file that is displayed above.
Here is the full solution with the sample word file included.
Hope this helps.

Temporarily save Word document changes

I'm using Interop to create a "custom word editor", basically I've incorporated MS Word on my application and I allow users to edit a document and insert custom fields from a predefined list.
I provide a "Preview" option to see how the document will appear when the data is added.
The users can start editing a template on my application, and at any time they can hit preview and the preview should appear with the latest changes.
I want the user to be able to preview the document without saving the changes, the problem is that when I invoke SaveAs() on the document (to create a temp file that I can use as the input for the preview generator), the editor opens the temp document.
Is there a way to save a copy of the document being edited but keep the original (open) document with its changes unsaved?
Thanks a lot
I know this can be done in pre-2007 Word, as described in this post.
Unfortunately, that solution does not work in Word 2007.
How about using a temp file from the get-go, and only saving to the "true" file when the user indicates that he or she is done?

Using Word 2007 as CMS page editor

I have been searching for several hours but i couldn't find anything about this... Basically I would like to create a template or plug-in for word 2007 that would allow someone to create new pages for a CMS. What I have in mind is something similar to blog post template. I know how to create a basic template but I can't find a way to publish the created document using a publish button inside the Word.
thnx in advance
I understand what you are trying to achieve, but Word is the wrong starting point. I would start with a much more basic text editor.
Word is horrible, horrible, horrible. Your site will define clear styles, yet Word will output nasty HTML that won't match your website's CSS definitions.
Your best bet therefore is to have a means to drop the Word file into the site, and have code programmatically analyse it and transform it into site-valid HTML. In Java you could use Apache POI, but that's very raw still. Might be a lot easier in a Microsoft centric world.
Far better, in my opinion, is to force people to learn Markdown, or BBCode, or HTML, or to use a Styled HTML Editor in your CMS - cut and paste plain text in, then style with the CMS defined styles.
As you are using Word 2007 you can export the document as XML and then use XSLT to generate the HTML.
If your CMS has an API or import facility you could convert the output from Word to suit that interface.
You can write a Word macro to add a Publish button/menu option to Word that will generate the correct output.
It's not a bad idea since it's all about the end user. If Word produces bad HTML, you should just make it semantic correct before posting it to the CMS.
I've never done this but I'm sure that it's possible to with .NET via the "Word 2007 Addin"-template (assuming Office 2007).
Good luck!
You can do what you want if you use SharePoint 2007 as your CMS. You can set up a blog on SharePoint 2007 and post to the blog from Word. If you use Office 2007 on the client end then you will get some nice buttons like "post to my blog" etc.
If you can't use SharePoint or are talking about an existing CMS, you have a lot of hurdles to jump through. This is a major undertaking and not something you can get a simple answer out of Stack Overflow.
Have you considered using one of the freely available Javascript WYSIWYG Editors such as TinyMCE http://tinymce.moxiecode.com/? When configured with all the options, it has an impressive amount of functionality and the interface is very similar to Word. I realize this doesn't directly answer your question, but as others have pointed out starting from Word is going to be difficult.
I've been on a team that wrote a Word addin for a custom CMS system. It was written in VB6 and was able to take a Word document and turn basic formatting information - lists, bold, italic and even tables into HTML, which was uploaded to the server. It didn't create new pages or manage the site in the addin though.
I would definitely avoid choosing Word as the editor for your CMS from my experience. The biggest issue is each time you want to update the addin you have to redistribute it to the company or companies using it. You can do this is as an IE active-x control but it's far easier just to handicap the user to a limited set of styling options via a Javascript editor.
Word does have a powerful API for manipulating your content with, however we needed to disable so many options in Word to avoid unwanted fonts and so on, it resembled Wordpad more than Word in the end.
If it's a greenfield project and you have the time, I would infact recommend using Silverlight 4.0 over a Javascript editor. Version 4.0 has a richtextbox control built in, plus there is also the excellent Vectorlight one.
May be it helps you, umbraco CMS allow editing with Microsoft Word.
For some reason this is a feature that Excel enjoys but not Word.
Excel can can automatically publish an HTML file version of your document when you save it.
Unfortunately Word seems to only be able to achieve this functionality when using Sharepoint, which is a shame because it can be quite useful.
What you can do, short of creating your own add-in is to add a bit of code to your template to create a HTML copy of your document whenever the user saves it.
First, make sure your template is macro-enabled (saved as .dotm file).
Second, while editing the template in Word, open the VBA code editor (ALT-F11)
In the project list double-click on your document to open its code-behind file.
Add the following bit of code to it, modifying the ActiveDocument.SaveAs path to something more appropriate to you, like a shared network folder where your CMS exposed by your CMS.
Sub FileSave()
' First Save the main document
ActiveDocument.Save
' Now we create a new document based on the current one
Selection.WholeStory
Selection.Copy
Documents.Add
Selection.PasteAndFormat wdPasteDefault
' Save it as HTML and close it
ActiveDocument.SaveAs "c:\temp\mydoc.html", fileformat:=wdFormatHTML
ActiveDocument.Close
End Sub
This will copy the original file into a blank new one that will be saved to HTML and closed before returning to the original file.
You can check some of the options to the Documents.Add if you want to use a different template than the normal one.
Security
because this template contains macros, you will have to install it with the other templates where Word expect them.
If you don't, then you'll get a security warning.
To avoid getting it, you can add the path where your templates are located to the list of Trusted Locations under Word's Options > Trust Center > Trust Center Settings > Trusted Locations.

Categories

Resources