View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.general
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default How do I prevent AutoRun Macros when Programmatically Opening Workbook?

Hi Joseph,

I might be missing something in your question but with automation the auto
open/close events do not run unless you explicitly make them. In other words
nothing to do to prevent them running automatically. Put open/close events
in a saved book and try following with / without RunAutoMacros.

Dim oXL As Object ' Excel.Application
Dim oWB As Object ' Excel.Workbook

Sub TestOpen()
Dim s As String
s = "C:\Temp\Book2.xls"
'Set oXL = New Excel.Application 'early binding with As excel.Application
Set oXL = CreateObject("Excel.application")

Set oWB = oXL.Workbooks.Open(s)
oXL.Visible = True

oWB.RunAutoMacros xlAutoOpen

End Sub
Sub TestClose()

oWB.RunAutoMacros xlAutoClose
oWB.Close
Set oWB = Nothing

oXL.Quit
Set oXL = Nothing

End Sub

Regards,
Peter T

"Joseph Geretz" wrote in message
...
I have an application which uses Word and Excel as automation servers.

When
opening a document, I need to prevent macros from running automatically.

For Word documents I can do this as follows:

m_AppWord.WordBasic.DisableAutoMacros 1
Set m_DocWord = m_AppWord.Documents.Open(CStr(m_Document), False,

True,
False)

What is the equivalent statement for Excel which I should be coding

directly
before opening the file?

Set m_DocExcel = m_AppExcel.Workbooks.Open(CStr(m_Document))

Thanks for your help!

- Joseph Geretz