Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.general
external usenet poster
 
Posts: 32
Default How do I prevent AutoRun Macros when Programmatically Opening Workbook?

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


  #2   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.general
external usenet poster
 
Posts: 2,494
Default How do I prevent AutoRun Macros when Programmatically Opening Workbook?

use Private Sub Auto_Open in a general module instead of Private Sub
Workbook_Open() in thisworkbook.



--


Gary


"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




  #3   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.general
external usenet poster
 
Posts: 32
Default How do I prevent AutoRun Macros when Programmatically Opening Workbook?

Hi Gary,

I'm not sure what you are proposing. I don't have access to the internals of
the documents which I am opening. My code which I presented is from my
document handling application which is on the 'outside' of the documents.
I'm simply opening these document by using server instances of Word and
Excel via automation. I'm looking for a way to programmatically open these
documents, while at the same time circumventing any AutoRun (or AutoExecute)
macros which may be encoded into the documents.

Thanks,

- Joseph Geretz

"Gary Keramidas" <GKeramidasATmsn.com wrote in message
...
use Private Sub Auto_Open in a general module instead of Private Sub
Workbook_Open() in thisworkbook.



--


Gary


"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






  #4   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.general
external usenet poster
 
Posts: 45
Default How do I prevent AutoRun Macros when Programmatically Opening Workbook?

Set the AutomationSecurity property to msoAutomationSecurityForceDisable
before opening the file. Remember to set it back when you've finished. Use a
sequence something like --

Dim pWord As Word.Application
Dim pDoc As Word.Document
Dim pSetting As MsoAutomationSecurity

Set pWord = New Word.Application
pSetting = pWord.AutomationSecurity
pWord.AutomationSecurity = msoAutomationSecurityForceDisable
Set pDoc = pWord.Documents.Open([FileName])
pWord.AutomationSecurity = pSetting





"Joseph Geretz" wrote in message
...
Hi Gary,

I'm not sure what you are proposing. I don't have access to the internals
of the documents which I am opening. My code which I presented is from my
document handling application which is on the 'outside' of the documents.
I'm simply opening these document by using server instances of Word and
Excel via automation. I'm looking for a way to programmatically open these
documents, while at the same time circumventing any AutoRun (or
AutoExecute) macros which may be encoded into the documents.

Thanks,

- Joseph Geretz

"Gary Keramidas" <GKeramidasATmsn.com wrote in message
...
use Private Sub Auto_Open in a general module instead of Private Sub
Workbook_Open() in thisworkbook.



--


Gary


"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








  #5   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.general
external usenet poster
 
Posts: 13
Default How do I prevent AutoRun Macros when Programmatically Opening Workbook?


"Jezebel" wrote in message
...
Set the AutomationSecurity property to msoAutomationSecurityForceDisable
before opening the file. Remember to set it back when you've finished. Use
a sequence something like --

Dim pWord As Word.Application
Dim pDoc As Word.Document
Dim pSetting As MsoAutomationSecurity

Set pWord = New Word.Application
pSetting = pWord.AutomationSecurity
pWord.AutomationSecurity = msoAutomationSecurityForceDisable
Set pDoc = pWord.Documents.Open([FileName])
pWord.AutomationSecurity = pSetting


Jezebel,

You're answering the wrong question. You're describing how to open a Word
document from Excel. The question was how to open an Excel workbook from
Word.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org



  #6   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.general
external usenet poster
 
Posts: 13
Default How do I prevent AutoRun Macros when Programmatically Opening Workbook?

The example is in the wrong application - the method works in both
applications so change Word to Excel (and Document to Workbook) and it
should do the trick.

--
Enjoy,
Tony

"Jonathan West" wrote in message
...

"Jezebel" wrote in message
...
Set the AutomationSecurity property to msoAutomationSecurityForceDisable
before opening the file. Remember to set it back when you've finished.

Use
a sequence something like --

Dim pWord As Word.Application
Dim pDoc As Word.Document
Dim pSetting As MsoAutomationSecurity

Set pWord = New Word.Application
pSetting = pWord.AutomationSecurity
pWord.AutomationSecurity = msoAutomationSecurityForceDisable
Set pDoc = pWord.Documents.Open([FileName])
pWord.AutomationSecurity = pSetting


Jezebel,

You're answering the wrong question. You're describing how to open a Word
document from Excel. The question was how to open an Excel workbook from
Word.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org



  #7   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.general
external usenet poster
 
Posts: 733
Default How do I prevent AutoRun Macros when Programmatically Opening Workbook?

Joseph Geretz wrote...
I have an application which uses Word and Excel as automation servers. When
opening a document, I need to prevent macros from running automatically.

....

In Excel only two types of macros would run when opening a workbook
interactively - event handlers (not just Workbook_Open, but also
Calculate and SheetCalculate if there are any volatile functions called
in applicable cells) and the legacy Auto_Open macros. If you open a
workbook using VBA, Excel *won't* run its Auto_Open macros, but if
events are enabled, it will run event handlers in the opened workbook.
So disable event handlers prior to opening workbooks and enable them
afterwards.

On Error Resume Next
Application.EnableEvents = False
Workbooks.Open . . .
Application.EnableEvents = True
On Error Goto 0

  #8   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.general
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




  #9   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.general
external usenet poster
 
Posts: 45
Default How do I prevent AutoRun Macros when Programmatically Opening Workbook?

I don't know about Excel, but Word ones do. Try it: add an AutoOpen macro to
word's normal.dot, then, from Excel, open a Word document ...


"Peter T" <peter_t@discussions wrote in message
...
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






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

I'm confused. You specifically asked about opening an Excel Workbook and
preventing it's open events from running. I tried to clarify that when using
automation they do not automatically fire. Excel does not have an equivalent
of Word's Normal.dot.

In addition other workbooks that normally auto load on Excel startup, such
as "checked" addins and files in the StartupPath (typically xlstart folder),
do not open when starting an instance of Excel with automation.

Regards,
Peter T

"Jezebel" wrote in message
...
I don't know about Excel, but Word ones do. Try it: add an AutoOpen macro

to
word's normal.dot, then, from Excel, open a Word document ...


"Peter T" <peter_t@discussions wrote in message
...
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










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

Oops, thought I was replying to a response from Joseph (OP) hence I was
confused, whereas of course I replied to a post from Jezebel who I think has
misread the OP.

Regards,
Peter T

"Peter T" <peter_t@discussions wrote in message
...
I'm confused. You specifically asked about opening an Excel Workbook and
preventing it's open events from running. I tried to clarify that when

using
automation they do not automatically fire. Excel does not have an

equivalent
of Word's Normal.dot.

In addition other workbooks that normally auto load on Excel startup, such
as "checked" addins and files in the StartupPath (typically xlstart

folder),
do not open when starting an instance of Excel with automation.

Regards,
Peter T

"Jezebel" wrote in message
...
I don't know about Excel, but Word ones do. Try it: add an AutoOpen

macro
to
word's normal.dot, then, from Excel, open a Word document ...


"Peter T" <peter_t@discussions wrote in message
...
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










  #12   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.general
external usenet poster
 
Posts: 32
Default How do I prevent AutoRun Macros when Programmatically Opening Workbook?

I'm confused.

No, maybe I'm confused; but if so it's for good reason. Seemingly, the
development analysts at Redmond made some sort of decision to engineer Excel
differently than Word in this respect? Or maybe no analysis was performed at
all, in which case it was left up to the individual development groups which
explains the statistically expected results (two tosses of the coin).

1. Word runs AutoRun macros automatically when a document is opened via
automation

2. Excel doesn't (?)

I had observed symptom #1 as described in Word and assumed that it
would be the same in Excel. But if it's not, then the problem I had with
Word documents doesn't present itself with Excel documents and so there's
nothing I need to do.

Please confirm, whether or not this is in fact the case.

Thanks!

- Joseph Geretz -

"Peter T" <peter_t@discussions wrote in message
...
Oops, thought I was replying to a response from Joseph (OP) hence I was
confused, whereas of course I replied to a post from Jezebel who I think
has
misread the OP.

Regards,
Peter T

"Peter T" <peter_t@discussions wrote in message
...
I'm confused. You specifically asked about opening an Excel Workbook and
preventing it's open events from running. I tried to clarify that when

using
automation they do not automatically fire. Excel does not have an

equivalent
of Word's Normal.dot.

In addition other workbooks that normally auto load on Excel startup,
such
as "checked" addins and files in the StartupPath (typically xlstart

folder),
do not open when starting an instance of Excel with automation.

Regards,
Peter T

"Jezebel" wrote in message
...
I don't know about Excel, but Word ones do. Try it: add an AutoOpen

macro
to
word's normal.dot, then, from Excel, open a Word document ...


"Peter T" <peter_t@discussions wrote in message
...
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













  #13   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.general
external usenet poster
 
Posts: 13
Default How do I prevent AutoRun Macros when Programmatically Opening Workbook?

There seems to be some confusion here ..

When starting an *Application* (Word, Excel, etc.) via automation,
automacros do NOT run.

When opening a Word *Document* via automation, automacros DO run

It's slightly more complicated in Excel I believe in that Workbook Events DO
run but Auto_Open macros DON'T run.

The Application.AutomationSecurity setting applies, AFAIK, equally to Word
and Excel.

--
Enjoy,
Tony

"Jezebel" wrote in message
...
I don't know about Excel, but Word ones do. Try it: add an AutoOpen macro

to
word's normal.dot, then, from Excel, open a Word document ...


"Peter T" <peter_t@discussions wrote in message
...
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








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

If using the TestOpen/Close demos with late binding and the code is say Word
would need to replace the xl constants as follows:

oWB.RunAutoMacros 1& ' xlAutoOpen

oWB.RunAutoMacros 2& ' xlAutoClose

Regards,
Peter T

"Peter T" <peter_t@discussions wrote in message
...
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






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Disable macros on a programmatically opening .xls file Matt[_2_] Excel Discussion (Misc queries) 2 May 25th 07 04:45 AM
how do I prevent a file from opening if user has disabled macros? mwh1107 Excel Programming 2 May 26th 05 11:50 PM
Disable Macros in workbook programmatically quartz[_2_] Excel Programming 2 April 2nd 05 03:31 AM
How do I turn off macros when programmatically opening a workbook. PhilBerkhof Excel Programming 3 December 10th 04 09:55 PM
autorun code when opening sheet (not workbook) Steve Mutambo Excel Programming 3 April 2nd 04 04:46 PM


All times are GMT +1. The time now is 02:05 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"