The Microsoft Word object model is a complex and varied one. You can really get an appreciation for Word as a piece of software after spending some time slogging it out with the Microsoft Word object model.

Even though the object model can be somewhat daunting it gives you amazing flexibility and power over programmatically bending MS Word to your will. In today’s article we’ll have a look at the Microsoft Word document properties, content controls, quick parts and bookmarks, and how you can access and use these objects in your own add-ins. I will provide C# code examples for each of the following items and you can download the C# Word sample add-in at the end of the article:

Word document properties

Document properties are available for Microsoft Word, Excel and PowerPoint documents and act as metadata for the documents. Office stores a range of standard built-in properties for each document, such as Title, Author and Keywords etc.

Retrieving a list of built-in Word document properties

To retrieve a list of built-in Word document properties, use the DocumentProperties object. This object is shared amongst the other MS Office applications and can be found in the Microsoft.Office.Core namespace.

Changing a built-in Word document property’s value

To change the value of a built-in property is fairly straightforward. You need to get an instance of the DocumentProperties collection and declare a new property to hold a reference to the built-in property you want to update.

Word content controls

Word Content controls are a great way to build easy-to-complete forms in Microsoft Word. Word provides a range of content controls such as the Plain Text, Combo box, Check box and Date Picker content controls.

To manually insert these controls into a Word document, you’ll need to first show the Developer tab and then click on the controls inside the Controls ribbon group.

Inserting a Word content control

Word provides a reference to all the content controls in the document via the ContentControls collection. To create a new content control, use the following code:

Word quick parts

The Word Quick Parts gallery is a collection of reusable content including document properties and AutoText. You are also able to add your own pieces of reusable content to this gallery.

Adding WordArt to a Word document

Adding WordArt to a Word document programmatically can be somewhat complicated, as you need the left and top coordinates of where you would like to insert the text. But, once you’ve established that, adding WordArt is as simple as invoking the AddTextEffect method of the Word Shapes collection.

What procedure do you refer to? And what you mean by “only works up before I save the file”? What occurs if you use it at any other moment? Note that modifying a document after it is saved makes the document non-saved (Document.Saved will return false).

using the first example to receive to BuiltInDocumentProperties causes word to say the document has been modified. So simple accessing the properties modifies the document ? How can i read the property values without changing the document ?

Hi, I am using bookmarks to update them with new fields, my problem is that bookmarks are unique in word. How can programmatically update one bookmark and that bookmark will update the rest of the concerning fields using cross reference or whatever.
I have tried to add {REF YourbookmarkName} but this is not done programmatically, each time I add this field and close the word doc then reopen it disappears.

//This will select the bookmark and update it with the correct value
Microsoft.Office.Interop.Word.Document docu.Bookmarks[MybookMark].Select();
Application oWordApp.Selection.TypeText(MyValue);

Now once the Bookmark is updated, I have a cross reference created and I want to update it with Myvalue. I used this as you suggested but I am sure I am using it wrong.
docu.Range().Fields.Update();
oWordApp.Selection.Fields;

If only the cross reference above was created beforehand, then after you update a bookmark, you need to scan all fields in the current document to find the filed(s) pointing to that bookmark, and update each of these fields via Field.Update (see https://msdn.microsoft.com/en-us/library/office/ff839593(v=office.15).aspx). Alternatively, you can update all fields in the document: get Document.Fields, and call Fields.Update().

Also, you can get and set Bookmark.Range.Text instead of selecting the bookmark and using Application.TypeText().

You could use the Macro Recorder to record a VBA macro while updating a bookmark and updating a field(s) in the Word UI. The macro would show you the classes/members involved in the process.

Do you think it is best to first create a bookmark then create a cross reference using the book mark in order for the update to hit the cross reference. I mean in this order. because all the cross reference I created using word 2013 are not being updated, I wonder why.

Do you think it is best to first create a bookmark then create a cross reference using the book mark in order for the update to hit the cross reference. I mean in this order. because all the cross reference I created using word 2013 are not being updated, I wonder why. but it works if I do it manually meaning, I select a text and then I create a bookmark then when I create a cross reference I push on CTL + SHIFT + F9 then it updates

Are you saying that you create a bookmark, select the text containing the bookmark, press F9, and the bookmark gets deleted? I don’t believe this understanding is correct. Could you provide more details?

Why you do not update the cross reference?

I don’t understand the context when you ask me about the order of creating bookmarks and references. Can you send me a test document so that we are on the same page?

Did you use the macro recorder to find how all this stuff works behind the scenes?

Hi Andrei, sorry for the confusion. Now, I will try to clarify the task I am working on. I have a word document that I am updating its fields using bookmarks all over, so if I want to update three fields with the same information I have used bookmarks, but since bookmarks are unique I have named the three bookmarks as follows : Version_1 Version_2 Version_3. my code in c# works fine and updates them.

Now, I want to use crossReference to update my fields instead of using the bookmarks Version_2 Version_3.

The steps I did take to get the crossreference work are:
I created a word doc with one bookmark Version_1 and two CrossReferences.

I’ve created a document and recorded the VBA macro below while creating a bookmark and three references. Now, if I change the bookmark text, select all references and press F9 in the Word UI, the references are updated with the new text. The code of the reference field is: REF Version_1 \h. The VBA macro is as follows:

You use the very same code that you would use in case of Word or Excel. The only difference is the starting point:
– Excel: Workbook.BuiltinDocumentProperties
– Word: Document.BuiltinDocumentProperties
– MSProject: Project.BuiltinDocumentProperties

Your code works for me when I invoke it in the Click event of a Ribbon button that my test Word add-in creates. Make sure you have no extra WINWORD.EXE hanging in processes. Also turn off all other COM add-ins. Make sure there’s no VBA code in attached template(s).

I am hoping one of you can help me with this. i have a word template with multiple bookmarks in it. I have been successful in populating the bookmarks with the desired data using C#. My challenge is I would like to delete only the bookmarks (just the bookmarks, not the data) from the generated Word doc.

I am coming from Microsoft Access and was successful in accomplishing this using VBA.

This technology is now available for our custom development services only. Based on the Add-in Express for Office core, it is designed for building custom-tailored Office add-ins with far less coding than you usually have to do. Plus, it includes all Add-in Express features such as True RAD, visual designers, Outlook view and form regions, etc.

Get the best platform for building version-neutral, fast and easy deployable plug-ins by using Add-in Express projects templates, visual designers, components and wizards in combination with a perfect Delphi compiler.

This is an extension for Visual Studio that allows developers to quickly create WiX-based setup projects in a familiar Visual Studio way.

The Designer for WiX Toolset lets you forget the plain Windows Installer XML and concentrate on your deployment logic. It integrates several editors with the Visual Studio IDE and provides a set of vdproj designers to configure the file system, registry, user interface, custom actions, launch conditions and more for your setup projects.

The innovative technology for customizing Outlook views and forms. It is included in all Add-in Express for Office products and can be used to extend Outlook views, e-mail, task and appointment windows, To-Do bar, Reading and Navigation panes with your own custom sub-panes.

Microsoft and the Office logo are trademarks or registered trademarks of Microsoft Corporation in
the United States and/or other countries. All other trademarks are property of their respective owners.