Microsoft.Office.Word.Interop.dll or don't look here if you are lucky and have more interesting things to do

·orientman·1 min read·Posts In English (Wpisy po angielsku)

I spent a day with Microsoft.Office.Word.Interop.dll and... surprisingly it hurt less than I expected. API is strange but does the job and it somehow improved over the years. I needed to support Word templates with following functionality:

  • repeatedly filling the same field e.g., CompanyName could occur multiple times (unfortunately fields/bookmarks in Word's template must have a unique names)
  • adding rows to the existing table
  • checkboxes (easy)
  • export to PDF (again easy - Word 2007+ supports it OOTB)

Not exactly rocket science but could be a time saver: https://github.com/orient-man/WordTemplates

Comments (2)

Comments are from the original WordPress blog. New comments are not supported.

H
huhu78

Panie Marcinie, nie umieszcza Pan komentarzy w kodzie. Jak to tak?

O
orientman

To comment or not to comment? Well, after reading "Clean Code" by Unce Bob I'm in "comments are fails" camp (e.g. https://medium.com/on-coding/a70ca8382884) I know, it's complete opposite of what you learn in the beginning and I would like all junior programmers to write rather more than less comments but... I comment less and less over the years and I'm happy with it. Probably this code wouldn't suffer from more comments. Normally, instead of comments I write unit tests. Here there is only one "smoke test" (works also as an example of usage) and no unit test at all (well, it's hard to write tests for code driven by such elephant API like MSWord). I put two comments inside WordTemplateNavigator only because writing tests for these assumptions was too hard. There should be at least following tests: - "bookmarks are deleted after setting them" - "valid field name consists of alpha numeric characters plus optional _N part where N is..."