none
Run Visio macro from C# RRS feed

Answers

  • Hello Pinggo,

    Thank you for posting and we are glad to help with  you.

    >>Can someone tell me how I can call a macro inside a Visio document from C# code? 

    After doing some research, I think it is not possible to run macro in C# from visio. After testing with the belowing code, I found the Excel works well while Visio does not:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Visio = Microsoft.Office.Interop.Visio;
    using Excel = Microsoft.Office.Interop.Excel;
    namespace RunMacroInViso
    {
      class Program
      {
        static void Main(string[] args)
        {
          string fileDir = Environment.CurrentDirectory;
          if (fileDir.EndsWith("\\bin\\Debug"))
          {
            fileDir = fileDir.Replace("bin\\Debug", "");
          }
          //string fileName = "TestMacro.xlsm";
          //Excel.Application excelApp = new Excel.Application();
          //excelApp.Visible = true;
          // Excel.Workbook wb = excelApp.Workbooks.Open(fileDir + fileName);
          
          //RunMacro(excelApp, new object[] { "Test" });
          string fileName = "TestMacro.vsd";
          Visio.Application visioApp = new Visio.Application();
          visioApp.Visible = true;
          Visio.Document doc = visioApp.Documents.Open(fileDir + fileName);
     
          RunMacro(visioApp, new Object[] { "Test" });
        }
    
        private static void RunMacro(object oApp, object[] oRunArgs)
        {
          oApp.GetType().InvokeMember("Run",
            System.Reflection.BindingFlags.Default |
            System.Reflection.BindingFlags.InvokeMethod,
            null, oApp, oRunArgs);
        }
    
      }
    }
    

    I think the reason is that there does not exist the method Run in Visio Applicaition object model, but in Excel it contains, such like:

    wb.Application.Run(...). So, Microsoft does not support run macro from Visio in C#.

    I hope it can help you.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by pinggo Friday, January 14, 2011 7:44 AM
    Friday, January 14, 2011 6:25 AM