Hello
Some time ago I ask to Gary how call a Macro Function and he share a great source code, then today i want to share a piece of code to call functions with parameters
I added two clases to manage Macros into region Add-in Manager Implementation of VSTAHookup.cs file as look as the follwing lines
internal Managers.MgrMacrosParameters mMacroParameters = new Managers.MgrMacrosParameters();
internal Managers.MacroMethodProperties mMacroProp = new Managers.MacroMethodProperties();
Clases Definition MacroMethodProperties:
using
System;
using
System.Collections.Generic;
using
System.Text;
namespace
VAO.Managers
{
public class MacroMethodProperties
{
private String mMacroName;
private System.AddIn.Contract.Automation.RemoteParameterData[] mParameters;
private Object[] mAssignedValuesToParameters;
private int mId;
public MacroMethodProperties()
{
mAssignedValuesToParameters =
new Object[0];
}
public String MacroName
{
get
{
return (this.mMacroName);
}
set
{
this.mMacroName = value;
}
}
public System.AddIn.Contract.Automation.RemoteParameterData[] Parameters
{
get
{
return(this.mParameters);
}
set
{
this.mParameters = value;
if (mParameters.Length != mAssignedValuesToParameters.Length)
{
//Initialize the mAssignedValuesToParameters Array
for (int i = 0; i <= mParameters.Length; i++)
{
mAssignedValuesToParameters =
new Object[i];
}
}
}
}
public int Id
{
get
{
return (this.mId);
}
set
{
mId =
value;
}
}
public int ParameterCount
{
get
{
return(mParameters.Length);
}
//set
//{
//}
}
public Object[] AssignedValuesToParameters
{
get
{
return (this.mAssignedValuesToParameters);
}
set
{
this.mAssignedValuesToParameters = value;
}
}
}
}
Clases Definition MacroParameters :
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
namespace
VAO.Managers
{
public partial class MacroParameters : Form
{
private VAO.Managers.MacroMethodProperties MMProp;
public MacroParameters()
{
InitializeComponent();
}
public VAO.Managers.MacroMethodProperties MethodProperties
{
get
{
return (MMProp);
}
set
{
MMProp =
value;
LoadParameters();
}
}
//public VAO.MgrMacrosParameters Parameters
private void LoadParameters()
{
for (int i = 0; i < MMProp.Parameters.Length; i++)
{
int rowIndex = dataGridViewMacros.Rows.Add();
this.dataGridViewMacros[this.ColumnName.Name, rowIndex].Value = MMProp.Parameters[i].Name;
if (MMProp.AssignedValuesToParameters.Length >0 && MMProp.AssignedValuesToParameters.Length >=i )
{
if (MMProp.AssignedValuesToParameters[i] != null)
{
this.dataGridViewMacros[this.ColumnValue.Name, rowIndex].Value = MMProp.AssignedValuesToParameters[i].ToString();
}
}
}
}
private void buttonAccept_Click(object sender, EventArgs e)
{
for (int rowIndex = 0; rowIndex < this.dataGridViewMacros.RowCount; rowIndex++)
{
for (int i = 0; i < MMProp.Parameters.Length; i++)
{
if (this.dataGridViewMacros[this.ColumnName.Name, rowIndex].Value == MMProp.Parameters[i].Name)
{
String Rv = (String)this.dataGridViewMacros[this.ColumnValue.Name, rowIndex].Value;
MMProp.AssignedValuesToParameters[i] = Rv;
}
}
}
this.Dispose();
}
private void buttonCancel_Click(object sender, EventArgs e)
{
}
private void MacroParameters_Load(object sender, EventArgs e)
{
}
}
}
Clases Definition MgrMacrosParameters:
using System;
using
System.Collections.Generic;
using
System.Text;
using
System.Collections;
namespace
VAO.Managers
{
public class MgrMacrosParameters : System.Collections.CollectionBase
{
public MgrMacrosParameters()
{
}
public virtual MacroMethodProperties this[int Index]
{
get
{
//return the Widget at IList[Index]
return (MacroMethodProperties)this.List[Index];
}
set
{
this.List[Index] = value;
}
}
public int Add(MacroMethodProperties aMacroMethodProperties)
{
MacroMethodProperties MPP = new MacroMethodProperties();
if (this.GetItem(aMacroMethodProperties.MacroName) == null)
{
MPP.Id =
this.Count;
MPP.MacroName = aMacroMethodProperties.MacroName;
MPP.Parameters = aMacroMethodProperties.Parameters;
MPP.AssignedValuesToParameters = aMacroMethodProperties.AssignedValuesToParameters;
List.Add(MPP);
}
else
{
MPP.Id = -1;
}
return (MPP.Id);
}
public int Add(String MacroName, System.AddIn.Contract.Automation.RemoteParameterData[] Parameters, Object[] AssignedValuesToParameters)
{
MacroMethodProperties MPP = new MacroMethodProperties();
if (this.GetItem(MacroName) == null)
{
MPP.Id =
this.Count;
MPP.MacroName = MacroName;
MPP.Parameters = Parameters;
MPP.AssignedValuesToParameters = AssignedValuesToParameters;
List.Add(MPP);
}
else
{
MPP.Id = -1;
}
return (MPP.Id);
}
public void Remove(int index)
{
// Check to see if there is a widget at the supplied index.
if (index > Count - 1 || index < 0)
// If no widget exists, a messagebox is shown and the operation
// is cancelled.
{
System.Windows.Forms.
MessageBox.Show("Index not valid!");
}
else
{
List.RemoveAt(index);
}
}
public void Remove(MacroMethodProperties dMacroMethodProperties)
{
List.Remove(dMacroMethodProperties);
}
public MacroMethodProperties GetItem(int Index)
{
// The appropriate item is retrieved from the List object and
// explicitly cast to the Widget type, then returned to the
// caller.
return (MacroMethodProperties)List[Index];
}
public MacroMethodProperties GetItem(String MacroName)
{
MacroMethodProperties MMP;
int i;
bool Found = false;
for (i=0; i < this.List.Count; i++)
{
MMP = (
MacroMethodProperties)List[i];
if (MMP.MacroName == MacroName)
{
Found =
true;
break;
}
}
if(Found == true )
{
return ((MacroMethodProperties)List[i]);
}
else
{
return((MacroMethodProperties)null);
}
}
~ MgrMacrosParameters()
{
// cleanup statements...
}
}
}
Finaly I use the following function to call the macro.
public
void ExecuteMacro(string macroMethod)
{
IEntryPointContract[] hostItems;
int MMC;
if (mMacroContext != null)
{
hostItems = mMacroContext[0][0].GetEntryPoints();
if (hostItems != null)
{
for (int i = 0; i < hostItems.Length; i++)
{
IRemoteObjectContract remoteObjectContract = (IRemoteObjectContract)hostItems[i].GetEntryPointObject().QueryContract(typeof(IRemoteObjectContract).AssemblyQualifiedName);
RemoteObject remoteObject = new RemoteObject(remoteObjectContract, new TypeInfrastructureManager());
//Find the paramter count to this macro method
MMC = 0;
for (int j = 0; j < mMacroParameters.Count; j++)
{
if (mMacroParameters[j].MacroName == macroMethod)
{
MMC = mMacroParameters[j].ParameterCount;
}
}
MethodInfo methodInfo;
if (MMC == 0)
{
//MethodInfo methodInfo = remoteObject.GetType().GetMethod(macroMethod, new Type[0]);
methodInfo = remoteObject.GetType().GetMethod(macroMethod,
new Type[0]);
}
else
{
methodInfo = remoteObject.GetType().GetMethod(macroMethod);
}
if (methodInfo == null)
{
continue;
}
if (methodInfo.GetParameters().Length > 0)
{
//Add code to call method with parameters
for (int j = 0; j < mMacroParameters.Count; j++)
{
if (mMacroParameters[j].MacroName == macroMethod)
{
try
{
methodInfo.Invoke(remoteObject, mMacroParameters[j].AssignedValuesToParameters);
return;
}
catch (System.Runtime.Remoting.RemotingException)
{
// Ignore remoting exception.
return;
}
catch (Exception e)
{
System.Windows.Forms.
MessageBox.Show("Error executing macro\n" + e.Message);
}
}
}
}
else
{
try
{
methodInfo.Invoke(remoteObject,
null);
return;
}
catch (System.Runtime.Remoting.RemotingException)
{
// Ignore remoting exception.
return;
}
catch (Exception e)
{
System.Windows.Forms.
MessageBox.Show("Error executing macro\n" + e.Message);
}
}
}
// Didn't find a method to invoke, so show a message box.
System.Windows.Forms.
MessageBox.Show("Unable to execute macro: " + macroMethod);
}
}
}