Word automation bypasses macro security

K

ksopher

Version: Word 2000, 2002
OS: Win2K, WinXP

With the macro security set to High, when I try to open a document
containing an AutoOpen macro it does not run and I am propmted with the
usual message about macros not being enabled. This is what I want.

I have a VB.NET application that automates Word using a COM wrapper.
When I open the same document programmatically on the same computers
the AutoOpen macro runs. I do not want this to happen. There is going
to be no user interaction, so holding the shift key and other such
tricks aren't realistic options. I do not have control over the
documents being processed, they are being submitted for processing by
external users.

The code I'm using is pretty rock-solid. It's been in production for 2
years. The line executed where the file is open is:

OpenDoc = WordApp.Documents.Open(FileName:=aFileName, ReadOnly:=True)

Due to security concerns I need to ensure that the auto macros don't
run when the file is opened via automation on the server.

What do I need to do to have the Word automation respect the Word macro
security settings? I've created the necessary Registry keys in HKLC so
that they override anything at the HKCU or application level, but this
isn't stopping it through automation. I've scoured the WROX Word 2000
VBA Programmer's Reference, MSDN online, Google Groups, etc., but can't
find anything. Alternatively, I'd be happy to just remove the AutoOpen
macros (and AutoExec, AutoNew), but it seems like a catch-22 - I can't
delete them unless I open the document first, but by then the macros
have run.

TIA - Keith
 
J

Jonathan West

Precede that line of code with this

WordApp.WordBasic.DisableAutoMacros(1)

To turn auto macros on again at the end of the routine, use this

WordApp.WordBasic.DisableAutoMacros(0)
 
K

ksopher

That worked. Thank you very much. Rather annoying that this doesn't
show up in the Intellisense or the Object Browser.
 
C

Cindy M -WordMVP-

Due to security concerns I need to ensure that the auto macros don't
run when the file is opened via automation on the server.

What do I need to do to have the Word automation respect the Word macro
security settings?
Just for the sake of completeness in this discussion, Word 2002 and later
provide the Application.AutomationSecurity property. With this you can
set how Word (and other Office apps) should behave when you try to open
files containing macros. Since you're also programming for Office 2000,
this won't be immediately relevant for you, however.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

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