truly weird Word 2007 vba problem

G

Gary Hillerson

This one's a bit complicated and hard to describe, and a very weird
problem.

I have a template -- call it MyTemplate -- that, at creation time
(AutoNew), allows the user to specify various initial content and
formatting options, and then "sets up" the document according to those
options.

The code does this:
1. AutoNew does some initialization, then opens the DocSetup dialog
2. DocSetup presents options to the user, then calls the
InsertDocumentPages function
3. InsertDocumentPages sets the margins, and inserts some content into
the document (various pages and text)

This code has been working fine since 2000, and continues to work just
fine in Word 2003 and earlier versions.

However, under very specific circumstance, the code fails in Word
2007, with InsertDocumentPages throwing an error.

1. I open any Word document (not based on MyTemplate), call it
Document1
2. I create a document based on MyTemplate (docA)
3. I close docA
4. I open another document based on MyTemplate (docB)

In docB, when my dialog code calls InsertDocumentPages, it throws an
error 6051 "macro cannot be found or has been disabled because of your
Macro security settings". What makes it even weirder is that I
modified my dialog code to effectively say "If InsertDocumentPages
threw an error, then call it again" and that call always works.

When I trace what's happening, the error is thrown in this code
Application.ScreenUpdating = False
With ActiveDocument.PageSetup
.LeftMargin = 108
.RightMargin = 72
.TopMargin = 72
.BottomMargin = 72
End With


This happens whether tell Word to allow all macros, or to only allow
signed code (my code is signed).

It only happens if I
a) have a Word doc open that's not based on my template
b) close the last opened doc that's based on my template (i can open
dozens, and this doesn't happen until i close the last one)
c) open another doc based on my template.

I'm hoping this is some sort of Word 2007 Beta problem with digital
certificates or macro storage, but it's pretty darn perplexing.

Any ideas?
 
J

Jezebel

Can't comment on whether W2007 is the culprit; but PageSetup is notorious
for mysterious bugs in all versions of Word. PageSetup entails calls to the
printer driver, and the communication between Word and some printer drivers
can be problematic. Try running your code with a different printer
installed, and (if that makes a difference) check the printer manufacturer's
website for an updated driver.

Or pre-define your template so you don't need to use PageSetup at all.
 

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