Hi David,
Let me answer the "How to" part of the questions.
To programmatically add VBA items to your workbook, you can call the VBE OM
exposed by Excel.Application.
The following C# sample code shows how to do that, you need to add a
reference to the Microsoft.Vbe.Interop assembly (from Office PIA) to
compile & run.
private void InstallMacro()
{
try
{
// Get the active VBA project via the Excel.Application object.
Microsoft.Vbe.Interop.VBProject prj =
Application.VBE.ActiveVBProject;
Microsoft.Vbe.Interop.VBComponent mod;
// Add a new module
mod =
prj.VBComponents.Add(Microsoft.Vbe.Interop.vbext_ComponentType.vbext_ct_StdM
odule);
mod.Name = "MyModule";
// Add code from string. To add from a file, call AddFromFile.
mod.CodeModule.AddFromString(
"Sub HelloWorld()\r\n" +
" MsgBox \"Hello World!\"\r\n" +
"End Sub\r\n");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Setup VBA Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
And in your add-in code, you can call the InstallMacro followed by
Application.Run to execute the macro:
InstallMacro();
// It's nightmare to call the Run method because C# doesn't support
// optional parameters.
Application.Run("HelloWorld",
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
There are security settings you need to change before get it working.
The first one is "Trust access to the VBA project". This must be enabled or
you'll get an COMException (HRESULT = 0x800A17B4) when accessing the
ActiveVBProject property. To turn on access, please follow these steps.
Office 2000 doesn't have this check.
Office 2003 and Office XP
1. Open the Office 2003 or Office XP application in question. On the Tools
menu, click Macro, and then click Security to open the Macro Security
dialog box.
2. On the Trusted Sources tab, click to select the Trust access to Visual
Basic Project check box to turn on access.
3. Click OK to apply the setting. You may need to restart the application
for the code to run properly if you automate from a Component Object Model
(COM) add-in or template.
Office 2007
1. Open the 2007 Microsoft Office system application in question. Click the
Microsoft Office button, and then click Application Options.
2. Click the Trust Center tab, and then click Trust Center Settings.
3. Click the Macro Settings tab, click to select the Trust access to the
VBA project object model check box, and then click OK.
4. Click OK.
Or you can programmatically change the registry settings at:
Office 2003: HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\Excel\Security
Office 2007: HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Security
Add or modify the DWORD value named AccessVBOM. Set to 1 means allow
access, 0 means disallow access.
The Macro Security setting is located in:
Office 2003: HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\Excel\Security
Office 2007: HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Security
There is a DWORD value named VBAWarnings. Meaning of the values:
VBAWarnings=1 Enable all macros (not recommended; potentially dangerous
code can run)
VBAWarnings=2 Disable all VBA macros with notification (application
default)
VBAWarnings=3 Disable Trust Bar warning for unsigned VBA macros
(unsigned code will be disabled)
VBAWarnings=4 Disable all macros without notification
For corporate users, this setting can also be controlled via policy, for
more information:
http://technet.microsoft.com/en-us/library/cc178946.aspx
Changing these security settings to a low standard may bring potential
vulnerability into the system. So please take careful consideration over
the changes.
If you have any further questions regarding this issue, please kindly let
me know.
Best regards,
Jie Wang (
[email protected], remove 'online.')
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.