views:

37

answers:

1

I have the following VBA code in a module of one of my .xlt files:

Public Sub SetShapeTop(ByVal shapeName As String, ByVal topValue As Single)
  ThisWorkbook.ActiveSheet.Shapes(shapeName).Top = topValue
End Sub

In my VSTO add-in, I have the following C# code which tries to call that VBA function:

m_worksheet.Application.Run("SetShapeTop", kv.Key, kv.Value, 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);

That line throws an exception, as follows:

(Excerpt from my log4net logs)

[Exception: COMException]
{Target: Cluster Report}
{Invoked Method: Macro execution}
{Target Method: Cluster Report}
{Parameters: }
{Message: Cannot run the macro 'SetShapeTop'. The macro may not be available in this workbook or all macros may be disabled.}
{Stack trace: 
Server stack trace: 


Exception rethrown at [0]: 
  at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
  at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
  at Microsoft.Office.Interop.Excel._Application.Run(Object Macro, Object Arg1, Object Arg2, Object Arg3, Object Arg4, Object Arg5, Object Arg6, Object Arg7, Object Arg8, Object Arg9, Object Arg10, Object Arg11, Object Arg12, Object Arg13, Object Arg14, Object Arg15, Object Arg16, Object Arg17, Object Arg18, Object Arg19, Object Arg20, Object Arg21, Object Arg22, Object Arg23, Object Arg24, Object Arg25, Object Arg26, Object Arg27, Object Arg28, Object Arg29, Object Arg30)
  at MSA.Excel.ClusterPresenter.RenderReport(IReportData reportData, IResponseReportParameters reportParams, Worksheet worksheet) in C:\workspace\MSABasketlink\2.0\MSA.Library.UI\Presentation\ClusterPresenter.cs:line 647} 

Am I missing something here? If anyone can point me in the right direction, I'd really appreciate it...!

A: 

This is a security restriction.

Go to Trust Center in Excel and check Trust access to VBA project object model.

SLaks
Thanks for the tip SLaks. However the options are all grayed out! (Fortunately the Trust Access to VBA project model checkbox is checked). Does this indicated anything? BTW, it's a .xlt file (Excel 2003) which I'm running in 2007.
code4life
The settings are probably controlled by your domain admin.
SLaks
Thanks SLaks, will check this out.
code4life