Word Object Model: Getting Information from Word Dialogs (McLean Schofield)

Although VSTO provides many features that help simplify Office development (especially UI development), nearly every VSTO solution needs to dip into the underlying Office object model to get some work done. Because information about using the Office object models can be spread out in many different places, I thought it might help some readers who are just getting started with Office development using VSTO to talk about a Word object model question that came across my desk recently.

A developer on an internal alias inside Microsoft asked about the best way to programmatically show the Insert Picture dialog box in a Word add-in, and how to perform some logic based upon the full name of the picture that the user selects. The developer had no trouble displaying this dialog box, but couldn’t figure out how to get the file name of the chosen picture.

As the developer noticed, displaying the Insert Picture dialog box (or any built-in Word dialog box, for that matter), is pretty straightforward. The trickiest part is determining which WdWordDialog enum value to pass as an index to the ThisAddIn.Application.Dialogs property in the VSTO project. By referring to the VBA documentation for the WdWordDialog enum at http://msdn2.microsoft.com/en-us/library/bb214033.aspx and scanning through the list of values for something that matches the name of the Insert Picture dialog box, you will eventually find the value wdDialogInsertPicture.

So, the code to display this dialog box looks like:


    Dim dialogBox As Word.Dialog = Globals.ThisAddIn.Application.Dialogs(Word.WdWordDialog.wdDialogInsertPicture)


    // C#

    Word.Dialog dialogBox = Globals.ThisAddIn.Application.Dialogs[Word.WdWordDialog.wdDialogInsertPicture];


Now comes the hard part: how to use the Dialog object to get or set data contained in the corresponding dialog box. For example, when the end user uses the Insert Picture dialog box to select a picture file, how can I programmatically determine which file they selected?

The Dialog object (reference topic at http://msdn2.microsoft.com/en-us/library/bb211882.aspx) doesn’t provide much help. A quick look through its members doesn’t show anything obviously useful or pertinent, like a “FileName” property anything like that. However, the reference topic for the WdWordDialog enum (http://msdn2.microsoft.com/en-us/library/bb214033.aspx) says something interesting (emphasis mine):

“Indicates the Microsoft Office Word dialog boxes with which you can work and specifies arguments, if applicable, that you can use to get or set values in a dialog box.”

This helps. So, for each WdWordDialog enum value, the table in this topic specifies the properties you can use to get or set information in the dialog box. For wdDialogInsertPicture, there are Name, LinkToFile, New, and FloatOverText properties. Of these, Name looks the most promising. So, how do we use the Name property if the Dialog class doesn’t have this property?

The answer, as is often the case when using the Office object models, is late binding. The Name property is accessible only as a late-bound property, so the way that you use it in a VSTO project depends on the type of project you are developing:

  • You are using Visual Basic with Option Strict set to Off.
  • You are using C# or Visual Basic with Option Strict set to On.

If you’re using VB with Option Strict set to Off, then you can use the following relatively simple code, which relies on late binding to access the Name property on the Dialog object at run time. After the Insert Picture dialog box is displayed, if the user selects a picture file and clicks Insert, then this code gets the full path of the selected file and displays the path in a message box:


    Dim DialogResultOK As Integer = -1

    With Globals.ThisAddIn.Application.Dialogs(Word.WdWordDialog.wdDialogInsertPicture)

        If .Show() = DialogResultOK Then

            System.Windows.Forms.MessageBox.Show(“The name of the inserted picture is: ” & .Name)

        End If

    End With

If you’re using C# or Visual Basic with Option Strict set to On, you’ll have use reflection:

    // C#

    Word.Dialog dialogBox = Globals.ThisAddIn.Application.Dialogs[Word.WdWordDialog.wdDialogInsertPicture];

    System.Type dialogType = typeof(Word.Dialog);

    int DialogResultOK = -1; 

    if (dialogBox.Show(ref missing) == DialogResultOK)


        string pictureName = (string)dialogType.InvokeMember(“Name”, System.Reflection.BindingFlags.GetProperty |

            System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance, null, dialogBox, null);

        System.Windows.Forms.MessageBox.Show(“The name of the inserted picture is: ” + pictureName);


This is one of those areas where the late-bound features of Visual Basic help make it a natural fit for Office programming. Granted, anything you can do in Visual Basic in terms of Office development is possible in C#, but it just isn’t always as straightforward or elegant.

Some of you reading this might be wondering if they have to go digging around in the VBA reference documentation every time they need to figure out how to do something with the Office object models in VSTO. Not always – the VSTO documentation provides a set of “how to” topics with code examples for some of the most common programming tasks using the Excel and Word object models (see the topics under http://msdn2.microsoft.com/en-us/library/kw65a0we.aspx for Word and http://msdn2.microsoft.com/en-us/library/bb386293.aspx for Excel). In fact, most of the information in this post is covered in the topic “How to: Use Built-In Dialog Boxes in Word” at http://msdn2.microsoft.com/en-us/library/ahzbkf8e.aspx.