I struggled for hours to find a code example for this but eventually stumbled across this page: http://support.microsoft.com/kb/262961 which helped me a lot!!! I converted it to C# and modified it slightly. I have not yet fully completed my task but here is a code snippet for now - I will explain the theory behind macros next. Hope this helps someone so long:
You will need to reference the following to get this code working:
using System.Collections.Generic;
using Microsoft.Office.Interop.Word;
using Microsoft.Vbe.Interop;
public static List<string> GetMacroList(Document document)
{
List<string> macroList = new List<string>();
vbext_ProcKind prockind = vbext_ProcKind.vbext_pk_Proc;
string curMacro = string.Empty;
string newMacro = string.Empty;
foreach (VBProject pj in document.Application.VBE.VBProjects)
{
try
{
if (pj.Protection == vbext_ProjectProtection.vbext_pp_none)
{
foreach (VBComponent vbcomp in pj.VBComponents)
{
if (vbcomp.CodeModule.CountOfLines > 0)
{
for (int i = 1; i < vbcomp.CodeModule.CountOfLines - 1; i++)
{
newMacro = vbcomp.CodeModule.get_ProcOfLine(i, out prockind);
if (curMacro != newMacro)
{
curMacro = newMacro;
macroList.Add(curMacro);
}
}
}
}
}
else
{
//do not have permission - tell the user
}
}
catch
{
//catch something...
}
}
return macroList;
}
No comments:
Post a Comment