Word 2007 VSTO wants to intercept certain document changes - c#

I have developed Word 2007 VSTO in VS2010 using C#. I want to be able to receive notifications about some of Document's collection changes, e.g. InlineShapes. I am also eager to intercept (be notified of) Insert/Picture or when a file is being dragged and dropped onto a Document. Is that possible?
I have spent a significant time looking for solution and searching resources everywhere but to no avail. It seems Outlook allows more flexibility in this area.

No, not possible. Word doesn't expose any events like that. The best you can hope for is to inteercept the DocumentChanged event, and try to ferret out was was changed based on the location in the document.
That won't be particularly easy though.

Related

Do I need to use Microsoft.Office.Tools.Word to create a report in Word?

I cannot seem to understand if I need Microsoft.Office.Tools.Word and how to get it. I know that it works together with Microsoft.Office.Interop.Word as seen in this article, for example:
Visual Studio extends the Microsoft.Office.Interop.Word.Bookmark
object by providing the Microsoft.Office.Tools.Word.Bookmark host
control. The Microsoft.Office.Tools.Word.Bookmark host control behaves
like a native Microsoft.Office.Interop.Word.Bookmark, but has
additional events and data-binding capabilities. You can bind data to
a bookmark control on a document in the same way that you bind data to
a text box control on a Windows Form. For more information, see
Bookmark Control.
and that it enables you to add controls to a word document here, but I am not sure what the added value is (or if I need it).
I am developing an application that will generate a report of cyber security risks in a company (with primarily text and tables), and I am wondering what I have to use to make sure I can create my report properly (Microsoft.Office.Interop.Word at the least).
Also, I do not really understand where to get the Microsoft.Office.Tools.Word from. I included the Microsoft.Office.Interop.Word from NuGet easily, but I cannot find the other one in either NuGet or the Reference Manager. Because of this, I can not play around with this extra functionality and determine whether I need it in my project or not.
If you need more detail about my project to answer this question, feel free to ask.
No, you don't need to do Office Interop. Note that doing Office Interop requires you to have Office installed, also on the PC where you finally want to use it.
For a DOCX report, there are libraries like docx by XCeed (formerly a Github repo by the same author of docx on Codeplex) which can do things a lot easier. However, not all features might be supported.

VSTO Office Word Add-In Development Overview

C# expert and ASP.NET developer at heart, I find myself assigned now to an Office Add-In project. Luckily I have worked on several Excel Add-Ins in the past, but it's all blurry in my mind and I never had a chance to learn it in a well structured manner.
I was surprised to notice that SO and the whole Internet is lacking proper documentation for VSTO, Word DOM, OpenXML, etc. Lacking might be an exaggeration, but it's not abundant or well organized, that's for sure.
So what I'm looking for basically it's an overview, jump-start, or cheat-sheet, however you want to call it, containing a high level summary of the important parts of Office development.
PS: I will post my findings as well during my study.
My 2 cents
1) For latest WordArt in word, you would need OpenXML data to insert in Word. For Excel\Powerpoint, things are easier, you can just use interop.
2) Don't always trust the documentation. If you get an int instead of a bool return value, its mostly a value from MsoTriState. e.g. Bold property should return bool as mentioned here. But, instead it returns int. So, you have to cast it like "(MsoTriState)returnedBoldValue" to figure out what that integer actually means.
3) Installation can be a pain, consider using ready-made products like Advanced Installer(I found it cheaper than others). Others might be good or better as well.
4) Table styling can be a pain. Here is a list of the weird IDs you need to use it for Powerpoint.
5) TextFrame2 is still not valid for Word(even if the documentation might say otherwise). Its only available to Excel and Powerpoint. For Word, you have to use TextFrame.
6) WPF is not supported. But, I was able to get it working just fine after some research. You need to make sure to load resource dictionaries and they work fine as well.
7) Window Association was a bit of an issue(Parent - Child). WindowInteropHelper is your friend there.
For now, I will stop here. I will post some links later.
Document Level Architecture
Document
Template (essentially the same as Document from VSTO's perspective)
"Contained" by the Document.
Document contains the relevant information required to load the Add-In. Does not use Registry.
Application Level Architecture
Add-in
Directly plugged into and loaded by Office. Agnostic to Document. It is registered via Registry keys.
Ribbon
Supported by Visual Designer. It handles state automatically for you.
Ribbon X
It allows a much better customization. It's XAML like. No Visual Designer. It doesn't handle state. You need to handle state on your own, using callbacks.
Word DOM - Document Object Model
Application
Documents
StoryRanges
Range
Styles
Style
Windows
Window
Panes
Pane
It is nothing special, just another DOM, quite similar to the HTML one. However, it will take some time to get used with it and the information on the Internet is not as abundant as one might hope.
Manipulating the DOM
Directly
VSTO Controls: Windows Forms Controls, Host Controls
Windows Forms Controls
Powerful (supports .NET databinding for example) however they are not really part of Office so they will feel like a nut in a wall.
Host Controls (wrappers around the Interop Word objects)
Bookmark Control - old method, move on, nothing to see here.
XML Node & XML Nodes - deprecated. Removed in USA due to lawsuit.
Content Controls - The future! Support real data binding to hidden XML data field in the XML parts of the document.
Deployment
Click-Once - simple and fast. It adds the Registry entries for you. But it doesn't allow much customization. For example you can have a Click-Once project installed only to the current user.
Windows Installer, Installshield, etc. You need to add the .manifest and the .vsto files to the output. You need to add the registry keys.

How to automatically update properties after the file is generated by word automation service?

I used Open XML + Word Automation to generate a word file from template and convert it to pdf. Now both word documents and generated PDFs are in the same document library. The word documents has some properties, which is added when they are created/uploaded to the library.
What I try to do now is automatically add properties to the PDFs after they are generated. The PDF will have the same properties as the Word Document that it is generated from.
At first I tried to use Event Receiver but the program doesn't seem to recognize that there's a new item (generated PDF) added into the document library.
Then I tried to use workflow (SharePoint Designer) but it doesn't work either(Failed to Start). The Outcome says "Workflow AddPDFData was canceled by System Account" and "AddPDFData failed to start". The workflow is set to start when an item is created. I know the workflow itself is correct because it works fine when I set it to start when an item is changed.
Please suggest if there's anything that I might do wrong, or if there is any approach that I can use. Any idea/suggestion will be highly appreciated.
Thanks!
It's a real pain that event receivers don't fire when the document is generated, however, you can definitely use a workflow and 'AutoStartOnCreate'. It works, I have done it. At least it does for Visual Studio 2010 created workflows in SharePoint 2010.
I can't say what the problem is with your workflow, but I suggest that if you add any item to the list the workflow will 'failed on start' for that item, not just the WAS generated items. That error usually means that the workflow can't validate correctly - bad correlation tokens, missing required something etc, you can look elsewhere for more detail.

How to write to word document (in a particular format ) From C# windows forms application

I am developing a C# windows application , which captures the user's inputs(in the form of numerous textboxes, radio buttons and drop downs).
I need to write the captured response from User to a word document which fits in the word document which has a predefined template.
Could some one let me know the best way to approach that?
Thanks in advance,
Vijay
Depending upon how complex your needs are and the depth of your pockets there is a great product called Aspose.Words. We use their suite of products for generating office documents at work and they're really easy to work with. It's unfortunately a pretty expensive product so unless you are very committed to your product it might not be right for you.
Otherwise you might want to check out Microsoft's Open XML SDK.
[I have no affiliation with the company, aside from having used their products.]

Create Word 2010 Document Programmatically

Every week I produce a word document with some copy, tables and charts from various sources on our network.
It can take a bit of time and sometimes I make mistakes or forget stuff that should go in. I want to automate this process as much as possible.
So basically I want to make a Word Template or Console App that when I open/run it it goes off and collects all this stuff and links it into the various parts of the document.
Assume I have to insert:
Some copy from another Word document
A PNG (pie chart)
Create a table from a CSV file
Have a standard Header and Footer with page numbering
I usually make a few changes to the copy in the document to highlight the highlight from the week.
Then I check it into a SharePoint where network users can open and view it.
I figure the thing to do is crack open VS2010 and make a Word Template project. I have never done this before and I wondered what traps there are or if its even an appropriate way to solve my problem.
My other option would be some power-shell but i'm no expert there either.
I'd be pretty comfortable writing console apps so I might end up doing it that way.
Help/Advice appreciated.
I would approach this problem by breaking down the individual steps as you would perform them if you were sitting in front of a blank Word document. Then automate it using the object model. There's a little bit of a learning curve, but once you get past that you'll be addicted to automating Office. Office is quite a powerful platform. Most of what you can achieve via the User Interface can be done programatically. I do quite a bit of automation with Excel. The code is fairly intuitive...its things like worksheet.Range["A1"] = "abcd" (setting cell A1 = "abcd").
here are some pointers:
http://support.microsoft.com/kb/316383
http://msdn.microsoft.com/en-us/library/ee861527.aspx
If you go through the tutorial in the first link, you'll get the gist of it. What's great is that you can use the debugger to step through your lines of code. As each line executes, you can see the results on the Word document. I've never used a Word Template project, so I can't speak for the pros/cons there. Going the object model route, you just have to articulate what you want to have happen, break it down into the individual steps as you would perform them, and then code it up. If you get stuck, likely someone else has blogged/posted about how to make something happen. Google will find a solution very quick. Good luck!
You have lots of options:
automate Word
do it as a macro (dotm)
create your document using Open XML SDK
VSTO
You are probably best off doing it using a macro first, though this is old VB6. The techniques you quickly learn there translate pretty well into the other approaches. Open XML SDK has the virtue of not needing Word to create the document.

Categories

Resources