0

My windows forms application uses Novacode DocX to write a document from a template. The Novacode portion of the project works perfect and the file saves. The issue is that when I load the document the field/s (Table Of Contents) are not updated when the Novacode portion adds headings and such.

I could, and did, write a macro to update fields on open. This would solve the problem, but not everyone that will use my application will have this macro. I can't save the file as a .docm file with the macro attached for various reasons (assume file must be ".docx").

What I've found is that the Microsoft.office.interop.Word assembly will allow me to call "Fields.Update". My understanding is this will do the trick, but I can't block users that don't have word installed from running my application. My understanding is that if I am "Using Microsoft.Office.Interop.Word", or have it in my references that the application won't run if someone doesn't have word.

So I have code that checks if word is installed. If I run this, and it is installed, can I then use "Late Binding" to run interop code? Other related questions, have replies that point to "NetOffice" as a way to run interop without checking if word is installed.

I'm trying to make this as comprehensive as possible with my research. My question is very similar to this one " how do I easily test the case where my C# application can't find an external assembly?". I would hope this issue can soon be solved for everyone, but I'm not sure it will be.

Side note, if anyone knows a way to update the fields, or even just the existing TOC, of a word document that is saved in the ".docx" format without having word installed that would be awesome to know, and would circumvent my whole issue. Although I would still like to know the answer to the interop question.

Also this is my first real question on StackOverflow, if you have tags to suggest please do so along with your answer. If you have feedback on how I ask my question, I will accept that too, but please don't close/delete the question without any answers. I linked to questions that are similar, but those questions have not gotten responses in a while. I believe I have done everything according to the rules.

Community
  • 1
  • 1
  • 1
    It will run just fine, you *really* should check that yourself. Only the `new Word.Application()` constructor call will fail. You can catch that. – Hans Passant Jul 30 '15 at 18:26
  • I'm not quite sure what you mean by "Check that yourself" anymore. Like ask on MSDN instead of SO or is this just a dumb intro to programming trivial thing that I missed/forgot. Since the Interop.office.word is bound on compile, and you need word to use interop, wouldn't the whole thing fail not just new word.application() ? – Celebrating Octopus Emoji Jul 31 '15 at 13:35
  • If you want to support this scenario then you have to test it so you can be sure it will work well for your clients. So test your program on a machine that does not have Office installed. – Hans Passant Jul 31 '15 at 13:40

2 Answers2

1

This is more of an answer to your "if anyone knows a way to update the fields, or even just the existing TOC, of a word document that is saved in the ".docx" format without having word installed" question, but you might want to look in to the Open XML SDK for Office.

This will let you modify .docx files without having any dependency on having Word installed.

I found this tutorial which I think is doing almost exactly what you are wanting do do using the Open XML SDK.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • [This question](http://stackoverflow.com/a/3983755/4990978) had a pretty good way of quickly updating the TOC, it's the same way I discuss and it uses Office Interop. What I added was a [method that checks if Word is in the registry](http://stackoverflow.com/a/7123265/4990978). If that is true, then it calls the TOC code. I'm told that as long as I don't use code from Office.Interop.Word the app won't crash. Adding that registry check should prevent the program from reaching that code and crashing. – Celebrating Octopus Emoji Aug 07 '15 at 16:06
0

Many things to say, but I think I found my answers

The main question was if I add the reference to "Microsoft.Office.Interop.Word" and the client running the application does not have word, where will the application fail? My understanding now, is that it will not fail on launch if the client does not have word. It will, however, fail when the code that uses the "Office.Interop.Word" is reached.

The way to prevent this, is a simple registry check method. I used a variation of This method to check the registry. Then before any of my code that uses the "Office.Interop.Word" code is run, I check if the client has word in the registry. If they don't have word, I take the proper notification actions for my application. I also surrounded the "Office.Interop.Word" code in a "try catch" exception block as a double safe measure. In my code the exception would mean word is not installed. A variation of the code using "Office.Interop.Word" I used to update fields can be found here.

Novacode DocX can support Docm files if you change the code yourself. I did not want to, and didn't use a docm file. Docm files have security warnings associated to them when emailed. So an auto updating macro is out of the question.

-Octopus Emoji is Celebrating

Community
  • 1
  • 1