i have a Word Doc in a directory and a .dotm file in the same.
word doc has not any macro in it, but all macro are in .dotm file.
so the question is this how can i run macros which are in .dotm file from my c# code?
i have tried so far:
private void btnStart_Click(object sender, EventArgs e)
{
object oMissing = System.Reflection.Missing.Value;
Word.ApplicationClass oWord = new Word.ApplicationClass();
oWord.Visible = true;
Word.Documents oDocs = oWord.Documents;
object oFile = txtFileName.Text;
Word._Document oDoc = oDocs.Open(ref oFile, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
RunMacro(oWord, new Object[] { "ListTagAll" });
RunMacro(oWord, new Object[] { "TableTag" });
RunMacro(oWord, new Object[] { "HyperlinkTag" });
RunMacro(oWord, new Object[] { "PictureReplace" });
RunMacro(oWord, new Object[] { "ReplaceParagraph" });
RunMacro(oWord, new Object[] { "CleanTag" });
oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc);
oDoc = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(oDocs);
oDocs = null;
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord);
oWord = null;
MessageBox.Show("Process Completed...");
}
private void btnOpen_Click(object sender, EventArgs e)
{
OpenFileDialog fd = new OpenFileDialog();
fd.ShowDialog();
if (fd.FileName != "")
{
txtFileName.Text = fd.FileName.ToString();
}
}
private void RunMacro(object oApp, object[] oRunArgs)
{
try
{
oApp.GetType().InvokeMember("Run",
System.Reflection.BindingFlags.Default |
System.Reflection.BindingFlags.InvokeMethod,
null, oApp, oRunArgs);
}
catch(Exception ex)
{
MessageBox.Show(ex.InnerException.ToString());
}
}