Geeks With Blogs

News

Vitaly Dilmukhametov

Programming Office Business Applications (OBA) with VSTO is an extreme -you are don’t ever know, which COM wrappers are realized and work good, and which are not. I have some experience with Bookmark control (included in VSTO 3.0 SP1), which is confirm it :)

I need to do anything in Word 2007 like Intellisense in Visual Studio. User types a few symbols, and Word show the list of variants, which contains in the internal reference book of my app. But small search at MSDN forums help me understand, that it’s impossible with VSTO.

Then I simplify the task – user click to some parts of document, afterwards select the value from WinForms control, hosted in CustomTaskPane. But how to identify theese “parts”?

Because I want to give my users possibility of making custom templates, I decide to use bookmark for marking parts of text in the document. In the VSTO runtime Word bookmarks present as interface Microsoft.Office.Interop.Word.Bookmark and class Microsoft.Office.Tools.Word.Bookmark. Class is more useful, because it bring powerful object model, contains many properties,which allow to customize style of text. And then logic of application is follow: add-in look all created and opened documents for our predefined bookmarks, hook it Selected and Deselected events and open or close TaskPane in the handlers.

But when I open more than one document, NullReferenceException is thrown (details here), and reasons of this behavior is not defined. Then I scan document for predefined bookmark names and insert ContentControl into marked place. Inside ContentControlOnEnter and ContentControlOnExit I switch CustomTaskPane to visible or not.

Few steps you need to do it:

1) Handle WindowActivate and DocumentBeforeClose on start and cancel handling on stop:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
 {
     foreach (Window wnd in Application.Windows)
     {
         Application_WindowActivate(wnd.Document, wnd);
     }
 
     Application.WindowActivate += Application_WindowActivate;
     Application.DocumentBeforeClose += Application_DocumentBeforeClose;
 }
 
 private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
 {
     Application.WindowActivate -= Application_WindowActivate;
     Application.DocumentBeforeClose -= Application_DocumentBeforeClose;
 }

2) Analyze content of the document, and insert ContentControl and handle appropriate events:

bool Check(Document Doc)
{
    bool isOurControlsPresent = false;
    if (Doc.Bookmarks.Count > 0)
    {
        foreach (Bookmark bookmark in Doc.Bookmarks)
        {
            if (BkmIds.Ids.Contains(bookmark.Name))
            {
                if (Doc.ContentControls.Cast<ContentControl>().Where(c => c.Tag == bookmark.Name).Count() == 0)
                {
                    Object rg = bookmark.Range;
                    Word.ContentControl ct =
                        Doc.ContentControls.Add(WdContentControlType.wdContentControlRichText, ref rg);
                    ct.Tag = bookmark.Name;
                }
                isOurControlsPresent = true;
            }
        }
        if (isOurControlsPresent)
        {
            Doc.ContentControlOnEnter += Doc_ContentControlOnEnter;
            Doc.ContentControlOnExit += Doc_ContentControlOnExit;
        }
    }
    return isOurControlsPresent;
}

 

3) Inside OnEnter and OnExit handlers show and hide CustomTaskPane by calling method:

private void SwitchTackPaneVisibility(bool visible)
{
    foreach (CustomTaskPane taskPane in CustomTaskPanes)
    {
        if ((taskPane != null) && (taskPane.Control is DataTaskPane))
        {
            taskPane.Visible = visible;
        }
    }
}

 

That’s all. Some screenshots. When I open my document, I have following:

image

 

When mouse pointer over marked words:

image

 

When I click on any of words (“first” or “second”) CastomTaskPane is shown:

image

And when I click on the button, data is added to marked by bookmark place in document:

image

Posted on Wednesday, April 1, 2009 9:05 PM OBA , Office , VSTO , Word2007 | Back to top


Comments on this post: MS Word 2007: working with bookmarks (VSTO)

# re: MS Word 2007: working with bookmarks (VSTO)
Requesting Gravatar...
Nice post, thanks for sharing this. Only one minor problem is that the sample is not available for download.
Left by Tony on Jul 03, 2009 7:00 AM

# re: MS Word 2007: working with bookmarks (VSTO)
Requesting Gravatar...
Thank you, Tony! Unfortunately, I can't attach sample file to the blog post, but it's here: http://www.sendspace.com/file/ciywkb
Left by Vitus on Jul 04, 2009 5:16 AM

# re: MS Word 2007: working with bookmarks (VSTO)
Requesting Gravatar...
I am unable to download the sample code from sendspace,it's not available.Can you please post the working link.Thanks in advance.
Left by Kman on Apr 26, 2010 6:15 PM

# re: MS Word 2007: working with bookmarks (VSTO)
Requesting Gravatar...
Hi, Kman!

Source code of samples here: http://uploading.com/files/d3c5215b/WordAddIn4_3.zip/
Left by Vitus on Apr 27, 2010 1:33 AM

# re: MS Word 2007: working with bookmarks (VSTO)
Requesting Gravatar...
Thank's for posting the updated link...
Left by Kman on Apr 27, 2010 9:27 AM

# re: MS Word 2007: working with bookmarks (VSTO)
Requesting Gravatar...
Hi Vitus,

I am unable to run this from Visual Studio 2008.I am getting the following errors.Am i missing any references ?
Errors:
--------------
Error 1 'Microsoft.Office.Tools.SmartTagCollection' does not contain a definition for 'BeginInit' and no extension method 'BeginInit' accepting a first argument of type 'Microsoft.Office.Tools.SmartTagCollection' could be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\UIU\My Documents\Downloads\DocumentMgmt\Samples\WordAddIn4_3\WordAddIn4\WordAddIn4\ThisAddIn.Designer.cs 129 32 WordAddIn4
Error 2 'Microsoft.Office.Tools.SmartTagCollection' does not contain a definition for 'EndInit' and no extension method 'EndInit' accepting a first argument of type 'Microsoft.Office.Tools.SmartTagCollection' could be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\UIU\My Documents\Downloads\DocumentMgmt\Samples\WordAddIn4_3\WordAddIn4\WordAddIn4\ThisAddIn.Designer.cs 137 32 WordAddIn4
Error 3 'Microsoft.Office.Tools.SmartTagCollection' does not contain a constructor that takes '5' arguments C:\Documents and Settings\UIU\My Documents\Downloads\DocumentMgmt\Samples\WordAddIn4_3\WordAddIn4\WordAddIn4\ThisAddIn.Designer.cs 148 34 WordAddIn4
Error 4 'Microsoft.Office.Tools.SmartTagCollection' does not contain a definition for 'Dispose' and no extension method 'Dispose' accepting a first argument of type 'Microsoft.Office.Tools.SmartTagCollection' could be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\UIU\My Documents\Downloads\DocumentMgmt\Samples\WordAddIn4_3\WordAddIn4\WordAddIn4\ThisAddIn.Designer.cs 171 32 WordAddIn4
--------------
Any idea..Kman
Left by Kman on Apr 27, 2010 9:44 AM

# re: MS Word 2007: working with bookmarks (VSTO)
Requesting Gravatar...
Hi, Kman!

I can build this sample without any errors. Do you have SP1 for VS2008 installed? Here you can find similar issue discussion: http://www.add-in-express.com/forum/read.php?PAGEN_1=2&FID=5&TID=5356#nav_start
Left by Vitus on Apr 28, 2010 12:01 AM

Your comment:
 (will show your gravatar)


Copyright © Vitus | Powered by: GeeksWithBlogs.net