PDA

View Full Version : DisableAutoMacros when opening Word in Excel VBA



Henry64
01-13-2009, 05:50 AM
Hello

When I open a word document from excel I need to disable AutoOpen and AutoClose events. However I cannot get "DisableAutoMacros" or anything else to work.

I tried
wrdApp.WordBasic.DisableAutoMacros 1 'gave an error that the member was not found, I assume that's the DisableAutoMacros


I'm opening word like this

Public wrdApp As Word.Application
Public WordWasNotRunning As Boolean



Set wrdApp = GetObject(, "Word.Application")
If Err Then
Set wrdApp = CreateObject("Word.Application")
WordWasNotRunning = True
End If

' somewhere I need to prevent the auto macros from running

'......

Set wrdDoc = wrdApp.Documents.Open(DocName)
With wrdDoc

' I'm doing some work here

.Save
.AttachedTemplate.Saved = True
.Close ' close the document
End With


If WordWasNotRunning Then
wrdApp.Quit
End If
Set wrdApp = Nothing

Paul_Hossler
01-13-2009, 06:06 AM
http://office.microsoft.com/en-us/word/HP101640101033.aspx

/a
Starts Word and prevents add-ins (javascript:AppendPopup(this,'ofAddIn_3'))and global templates (including the Normal template) from being loaded automatically. The /a switch also locks the setting files

Maybe if you used Shell ( ) to run WinWord with the /a and /t switches, it would avoid the Autorun macros

Paul

Henry64
01-14-2009, 01:32 AM
Thought of that too, however I cannot get access to the "document" object and I need to do some work in the documents.

GTO
01-14-2009, 08:56 PM
Greetings Henry,

Probably a better way, but for some reason I cannot recall how to successfully work w/the app first.

Anyways, presuming you can get into the WORD document(s) and make a minor code change:

In the WORD document:

Option Explicit
Sub AutoOpen()
If Application.UserControl Then

MsgBox "HI from OnOpen"

'...statements...
End If
End Sub

OR if using on open:

Option Explicit

Private Sub Document_Open()
If Application.UserControl Then

MsgBox "HI AGAIN - from Document_Open"

'...statements...
End If
End Sub

Back in the workbook:

Option Explicit
Sub OpenWordEtc()
Dim wrdDoc As Word.Document
Set wrdDoc = GetObject("C:\Documents and Settings\stumpm\Desktop\TEMP\CVS\Doc1.doc")

'// Optional. Since we got the doc open while UserControl was False, we can now //
'// display the app if we want... //
wrdDoc.Application.Visible = True

With wrdDoc

' I'm doing some work here

.Save
.AttachedTemplate.Saved = True

.Close ' close the document

End With
End Sub

Just for the example, I discluded the checking, but believe you should be able to use GetObject to see if Word is there (like you were) and either
wrdDoc.Close or wrdDoc.Application.Quit

Hope this helps?

Mark