Much ado about Office

19 January 2012

Word 2007 ContentControlOnexit bug

Filed under: Uncategorized — snoopen @ 15:52

I came across a bug today to do with the Document_ContentControlOnexit event, bookmarks, and the focus of the ribbon.

Consider you have a document with a Drop Down List content control and a range of text bookmarked. The bookmarked text range needs to be formatted depending on the selection made in the drop down. For the most part the following code should work fine:

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    Select Case ContentControl.Tag
        Case "EntityOption"
            Select Case ContentControl.Range.Text
                Case "Single entity"
                    FormatBookmark "MultipleClients", False
                    FormatBookmark "SingleEntity", True
                Case "Multiple entities"
                    FormatBookmark "SingleEntity", False
                    FormatBookmark "MultipleClients", True
                Case Else
                    FormatBookmark "SingleEntity", True
                    FormatBookmark "MultipleClients", True
            End Select
    End Select
End Sub

However, when and only when the Home tab is selected I see a problem. Word kind of drops into an infinite loop. You see the hourglass flickering on screen a whole lot but you can still eventually click out of the content control and stop it, thats why I say kind of.

The first part of the problem is that when the Home tab has focus the content control exit event breaks and triggers both on enter and exit. This seems to be a fairly well known bug.

The second part of the problem and the infinite loop manifests itself when that incorrectly triggered exit event does something like manipulating a bookmarked range. The change in the document seems to trigger yet another content control exit event. Thus the loop begins. I think the exit event again is being triggered like an enter event, but I’m not sure.

So I did some Googling and found it had been noticed before by a PeterHS on microsoft.public.word.vba.general as posted here. He cried for help but no one seemed to have an intelligent answer. The common suggestion is to activate the developer tab as soon as the event is triggered. A solution that works, but interferes with the tabs, and I don’t like that.

My solution, while not perfect, is to check how quickly the events are being triggered. I’m assuming that human triggered events will probably be spaced by at least half a second. After all you need to click the drop down, find the correct item in the list, click on that, then click out of the content control (actually I hate that there is no on change event but that’s another story).

Private msngLastRun As Single
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
If msngLastRun = 0 Or Timer - msngLastRun > 0.5 Then
msngLastRun = Timer
Select Case ContentControl.Tag
Case "EntityOption"
Select Case ContentControl.Range.Text
Case "Single entity"
FormatBookmark "MultipleClients", False
FormatBookmark "SingleEntity", True
Case "Multiple entities"
FormatBookmark "SingleEntity", False
FormatBookmark "MultipleClients", True
Case Else
FormatBookmark "SingleEntity", True
FormatBookmark "MultipleClients", True
End Select
End Select
End If
End Sub

The event will still trigger once or twice, once on entering the content control when the home tab is active and a second time if the timer variable hadn’t been set before. But at least the loop is avoided.

31 March 2011

Some more issues to watch for in Word 2007

Filed under: Uncategorized — snoopen @ 14:50

Here are a few things I’ve discovered recently.

  1. Strange compatibility issue with Word 2003/2007
  2. Style references sometimes show weird characters
  3. Open and repair works differently from Word 2003
  4. Nested field codes in footers are impossible to update with VBA

(more…)

4 February 2011

When section breaks won’t change

Filed under: Uncategorized — snoopen @ 18:59

I’ve seen a few Word documents recently that contain next section breaks which cannot be changed to continuous  breaks. No matter what I tried the section break didn’t want to change from next to continuous. Deleting the section break would have meant messing up headers, footers etc so was out of the question. My colleague who is also a Word expert couldn’t figure it out either. There just didn’t seem to be anything wrong with the document that would make it unable to change.
(more…)

20 October 2009

Why I don’t like the Watermark gallery in Word 2007

Introduction

Printed watermarks in Word 2003 worked fine for me. They appeared on every page of every section in a document. Regardless of whether or not you have link to previous in the headers. However in Word 2007 (and the Word 2010 Tech Preview) without Link to Previous checked, a watermark applied from the Gallery will only place itself into the section of the current selection. Luckily we still have the Custom Watermark dialog which behaves like it did in Word 2003. To remedy this stupid behaviour of the gallery I decided to write a fix button in VBA and add it to a custom ribbon tab.

(more…)

16 October 2009

Word 2007 VBA – Bug using .Shapes in Section Headers

Intro to the problem

When using .Shapes in the Header to add certain objects such as a Picture the object will only ever appear in the first section when the following sections are not linked. This happens regardless of how you specify the anchor. Fortunately there is a method to get around this flaw which I believe is also present in 2003.

Following is a list of what is and isn’t affected and two workarounds; a short workaround that can be incorporated into a simple code base, and a long workaround which can replace the .AddPicture method altogether.

(more…)

Create a free website or blog at WordPress.com.