Using LINQ in word 2007 with VSTO and VB 2008 in VB.NET

Introduction

Language Integrated Query (LINQ) is one of the main features of Microsoft .NET Framework 3.5.

As you perhaps already know, LINQ lets you execute queries onto a larger number of data sources. Among the big amount of data sources we can query, we can also, in example, query Microsoft Word 2007 document properties. This is possible when developing a document-level solution with Visual Studio 2008 Tools for Office System.

VSTO are now part of Visual Studio 2008 IDE (Professional edition or greater is needed). Until 2005 edition, you had to download and install VSTO as separate components, but now this is no more necessary.

First of all, open Visual Studio 2008 and create a new Word 2007 Document project (you can find this project template in the Office folder). You can choose to create a new document or import an existing one.

In this article we're going to consider a Word 2007 document which contains revisions applied from two different people (in this case, two user accounts on my pc). Each of the reviewer applied his own corrections. As you can see in the following figure, first reviewers revisions are red marked, while second reviewers' revisions are blue marked:

visual-studio-administrator-in-windows8.gif

First of all, you should add a button onto the document. You can drag a Windows Forms button from the toolbox onto the document surface, as you can see in the above figure. Now we decide to extract and count only revisions applied by the user "Alessandro". The Click event handler could be written in the following way:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 

        Dim allRevisions = From rev As Word.Revision In Me.Revisions _Where rev.Author = "Alessandro" _Select rev.Range.Text 

        Dim result As String = String.Empty 

        For Each singleRevision In allRevisions

            result += String.Concat(singleRevision, Environment.NewLine)

        Next 

        MessageBox.Show(result, "Alessandro has applied the following revisions", MessageBoxButtons.OK,MessageBoxIcon.Information)

    End Sub

The sample expression you saw in the above code snippet queries the Revisions collection. For each revision, the query expression selects its text if the author of the revision itself is the user "Alessandro". Finally, a dialog box informs about the revisions made by this user.

If you think that using this programming technique is quite boring and not useful at all, just think that by the same way you can query not only all the collections which a Microsoft Word document exposes but also data contained in an Excel 2007 workbook. In this case, you could also use the Join keyword to take pieces of data from different collections and then join them.

Up Next
    Ebook Download
    View all
    Learn
    View all