Hanging Code

N

NZ VBA Developer

First, my apologies for the length of this post, but it’s a bit of a
complicated problem and requires some background to explain completely.

I have a series of templates that produce forms-protected documents that
contain unprotected sections. However, Word’s definition of ‘unprotected’ is
a bit… unusual, and is causing me no end of grief.

Now I understand disabling some functions. For example, disabling File |Page
Setup and View | Header and Footer make sense, since this is the kind of
stuff you don’t want users changing in a protected document.

Disabling other functions seems sort of logical if you follow a bit of
convoluted reasoning. For example, disabling Insert | Reference |
Cross-Reference is kind of sensible because it stops users from inserting a
cross-reference to something in a protected section that then can’t be
‘accessed’ by control-clicking on the link; it might result in some odd
behaviour in that focus would then be set to the nearest unprotected location
in the document – although this seems a bit like throwing the baby out with
the bathwater. I mean, why take away a very useful bit of functionality
because it _might_ result in something that requires a bit of simple thought
to understand?

However, I can live with these limitations. The stuff that really makes me
scratch my head is the things like disabling Restart Numbering and Continue
Numbering. (Can anyone explain that one to me?) Getting around these
limitations has resulted in writing code that basically unlocks the document,
calls the standard Word functionality and then locks the document again. I’ve
written several of these procedures and not had any serious problems so far –
just a raw head.

But by far the most problematic area has been working with Inline and
Floating Shapes (which I will refer to collectively as images). Not only are
all of the standard Word functions for working with images disabled, but in
the case of a Floating Shape, you can’t even select it to delete it. And to
make matters worse, Word tells you bugger-all about the images in a document.
Basically, all you can find out is what type of image it is (Inline or
Floating), how many of each type there is in the document and the index of
each image of each type. There’s no easy way to label an image – at least not
whilst inserting it, in spite of the fact that Shapes have a Name property –
and Word won’t even tell you where it’s located in the document.

My solution to the above problems has resulted in something akin to shooting
in the dark: a UserForm that lists all of the images in the document,
identifies what type of image it is (Inline or Floating) and simply
enumerates them as ‘Inline Shape 1’, ‘Inline Shape 2’, ‘Picture 1’, ‘Picture
2’, etc. Options are then provided for performing various operations on the
selected image as follows:

Format – If it’s a Floating Shape, selects the Shape and displays the
standard ‘Format Image’ dialog box. If it’s an Inline Shape, displays a
secondary userform that allows the user to either convert the Inline Shape to
Floating and then ‘edit’ it using the ‘Format Image’
(wdDialogFormatDrawingObject) dialog box or ‘edit’ the Inline Shape itself
using something called the ‘Picture’ (wdDialogFormatPicture) dialog box.
(Note that the ‘Picture’ dialog is virtually useless but it’s the only one
that can be displayed programmatically – in spite of the fact that the
‘Format Image’ dialog box is the one that Word displays if you use any one of
the various ‘Format Picture’ methods that are native to Word. Go figure…)

Delete – Simply deletes the selected image regardless of the type. (A
confirmation message is displayed first as I would hate for a user to
inadvertently delete an image that they had just spent hours formatting using
the ‘close your eyes and pull the trigger’ method described above.)

Caption – Displays the standard Word ‘Caption’ dialog box and allows the
user to add a caption to the selected image.

Of course all of these functions unlock the document first and they all also
display a native Word dialog. The first two work OK – apart from the fact
that trying to do something like position an image using the options in the
‘Format Picture’ dialog box is almost a hopeless proposition – but ‘Caption’
just doesn’t want to play nice.

At this point I’m going to provide a code snippet for the ‘Caption’
functionality and then explain a bit about what it does before describing the
problem.

Private Sub CaptionThis()
Selection.Bookmarks.Add "HERE", Selection.Range
Select Case lstPix.Column(1)
Case "S"
ActiveDocument.Shapes(lstPix.Column(2)).Select
Case "I"
ActiveDocument.InlineShapes(lstPix.Column(2)).Select
End Select
ActiveWindow.ScrollIntoView Selection.Range, True
Dialogs(wdDialogInsertCaption).Show
With ActiveDocument.Bookmarks("HERE")
.Select
.Delete
End With
End Sub

First I insert a bookmark at the current selection. I do this because when
you caption a Floating Shape, it inserts a Text Box somewhere in the vicinity
of the selected Shape and then leaves focus inside the Text Box. This
(apparently) interferes with locking the document again because if I try to
protect the document programmatically at this point I get an error saying
that the ‘ToolsProtectDocument’ statement is currently disabled, and if I
debug it stops on the line that says ‘ActiveDocument.Protect’. Thus the
reason for the bookmark, which I then select again at the end (to get focus
out of the Text Box) and then delete.

Next I look to see what the Shape ‘type’ is that’s associated with the item
selected in the ListBox so I can work out which collection to look in for the
Shape that I want to select – and then I select that Shape.

Then I try to help the user a bit by scrolling the selection into view so
they have _some_ idea of which image they’re working with. Dunno how useful
it is, but at least I tried.

Finally, I display the standard Word ‘Caption’ dialog box and, after the
user has done whatever they want to do in the dialog box, set focus to and
delete the bookmark as discussed above.

So here’s the problem: After all this jiggery-pokery, the code just hangs.
The button on the toolbar that’s clicked to start things off stays
‘depressed’ and if I go into the VBE I can’t do anything – add or edit code,
run a sub or UserForm, reset, nothing. In fact, the Run Sub/UserForm button
(which tooltips as ‘Continue’ at this point) is disabled, and F5/F8 do
nothing. I can click the ‘Break’ button, but it doesn’t really do anything
either. However, if I look for the ‘execution point’ in the code (I’m not
really sure what it’s called but it’s the yellow arrow to the left of a line
of code that displayed when stepping using F8), it’s clear back in the
original sub that’s invoked by the toolbar button sitting on the line that
shows the UserForm. Clicking the ‘Stop’ button does seem to stop execution –
at least the yellow arrow goes away – but it doesn’t ‘un-hang’ the code.

One final interesting bit: If I have multiple documents open, the toolbar
button is ‘depressed’ only in the document that I clicked it in originally.
It is available in other documents and does do what it’s supposed to do;
however, it still hangs the same way in each new document. The only way to
get unstuck is to shut down Word completely and restart. Also the Caption
dialog seems to respond very slowly and gets even slower in subsequent
documents.

I’m a bit hesitant to provide the code-containing template to anyone as it
contains some sensitive stuff and would be very difficult to sanitise.
However, if you would like more information and would prefer not to go
through the posting process, feel free to contact me at the Gmail address
listed in my profile.

--
Cheers!
The Kiwi Koder

Please note: Uninvited email contact will be marked as SPAM and ignored -
unless you want to hire me. ;-)
 
N

NZ VBA Developer

Too hard, eh? That's OK; I'll see if I can get the client to change the
requirement...
--
Cheers!
The Kiwi Koder

Please note: Uninvited email contact will be marked as SPAM and ignored -
unless you want to hire me. ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top