XL97: Warning Appears When You Use GetObject to Open Workbook

ID: Q165436

The information in this article applies to:

SYMPTOMS

If you run a Microsoft Visual Basic for Applications macro that uses the GetObject method to open a Microsoft Excel workbook, you may receive the following error message:

   The workbook you are opening contains macros. Some macros may contain
   harmful viruses. If you are sure this workbook is from a trusted
   source, click Yes. If you are not sure and want to prevent any macros
   from running, click No.

The macro pauses until you click Yes, No, or Cancel. Note that if the macro is designed to run unattended, this behavior may cause a problem.

CAUSE

You may receive this error message when the following conditions are true:

WORKAROUND

To prevent this problem from occurring, use either of the following methods.

Method One: Turn Off Macro Virus Protection

If you clear the Macro Virus Protection check box in Microsoft Excel 97, the problem described in this article does not occur. However, making Macro Virus Protection unavailable makes it possible (although unlikely) for a macro virus to infect your Microsoft Excel workbooks.

To make Macro Virus Protection unavailable in Microsoft Excel 97, follow these steps:

1. On the Tools menu, click Options.

2. Click the General tab.

3. Click to clear the Macro Virus Protection check box.

4. Click OK.

Note that you cannot programmatically disable the Macro Virus Protection feature. Also, you cannot programmatically detect whether the feature is available or unavailable; you must do this manually.

Method Two: Use SendKeys to Bypass the Error Message

If you know that the Macro Virus Protection feature is available when you open a Microsoft Excel workbook, you can use the SendKeys method to bypass the error message dialog box. Although this method allows you to use the Macro Virus Protection feature, it may cause an errant "y" character to appear somewhere in one of your programs or open files, so use this method with caution.

To use this method, insert the following line of code into your Visual Basic for Applications macro

   SendKeys "y"

immediately BEFORE the following line of code that opens the Microsoft Excel workbook. The following sample line of code opens the Text.xls workbook in the My Documents folder:

   Set xlWkbk = GetObject("C:\My Documents\Test.xls")

The "y" character that is sent by the SendKeys statement is used to trigger the "Yes" button in the error message dialog box so the macro continues running without pausing. If the dialog box does not appear, the "y" may appear in a program or be inserted in an open file. It is not possible to predict where the "y" will be inserted.

MORE INFORMATION

In Microsoft Excel 97, the Macro Virus Protection feature is designed to prevent macro viruses, such as the ExcelMacro/Laroux virus, from running. If you select the Macro Virus Protection check box, you are prompted to enable or disable macros whenever you open a workbook that contains macros.

If you use the GetObject method of Automation to open a Microsoft Excel workbook that contains macros, and if Macro Virus Protection is available, you receive a warning message when you open the workbook.

Example:

If you make Macro Virus Protection available in Microsoft Excel 97, and the workbook Test.xls contains any macros, you receive the error message when you run the following sample Sub procedure:

   Sub TestOLEAutomation()

      ' Dimension variables.
      Dim xlWkbk As Object

      ' The error message appears when this line is executed.
      Set xlWkbk = GetObject("C:\My Documents\Test.xls")

      ' Display the program and document windows, and then close the
      ' workbook.
      xlWkbk.Parent.Visible = True
      xlWkbk.Parent.Windows(xlWkbk.Name).Visible = True
      xlWkbk.Close SaveChanges:=True

      Set xlWkbk = Nothing

   End Sub

Additional query words: XL97
Keywords          : kbprg kbdta kbdtacode KbVBA 
Version           : WINDOWS:97
Platform          : WINDOWS
Issue type        : kbprb

Last Reviewed: January 7, 1999