none
Determine if Visio Document contains macro code in C# RRS feed

  • Question

  • I am trying to determine through a C# console app if a Visio document contains macros and if so whether that document is digitally signed. I  have been able to do this for Word, Excel, Project and PowerPoint but can't figure it out f or Visio. Is this possible?

    Thanks,

    Marc Wiener


    Marc Wiener
    Wednesday, January 5, 2011 7:24 PM

Answers

  • Hi MarcWiener,

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

    After reading your post, I knew that you need a C# console app to tell whether a visio document contains macros and is digitally signed. If I have misunderstood you, please let me know.

    As far as I know, you can use Microsoft.Vbe.Interop to judge whether the document contain macros, bellowing is my code to get the Macro in Visio document:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using VBA = Microsoft.Vbe.Interop;
    using Visio = Microsoft.Office.Interop.Visio;
    using Excel = Microsoft.Office.Interop.Excel;

    namespace JudgeIfVisioContainMacro
    {
        class Program
        {
            static void Main(string[] args)
            {
                string fileDir = Environment.CurrentDirectory;
                if (fileDir.EndsWith("\\bin\\Debug"))
                {
                    fileDir = fileDir.Replace("bin\\Debug""");
                }
                string fileName = "TestMacro.vsd";
                Visio.Application visioApp = new Visio.Application();
                visioApp.Visible = true;
                Visio.Document doc = visioApp.Documents.Open(fileDir + fileName);
                List<string> lstMacros = new List<string>();

     
                lstMacros = GetMacrosFromDoc(doc);
                if (lstMacros.Count > 0)
                {
                    Console.WriteLine("There are " + lstMacros.Count.ToString() + " procedures in " + fileName + ":");
                    foreach (string macro in lstMacros)
                    {
                        Console.WriteLine(macro);
                        Console.WriteLine("-----------------------------------------------------");
                    }
                }
                else
                {
                    Console.WriteLine("There are not procedures in the document");
                }

                doc.Close();
                visioApp.Quit();
            }

            public static List<string> GetMacrosFromDoc(Visio.Document doc)
            {
                List<string> macros = new List<string>();

                VBA.VBProject prj;
                VBA.CodeModule code;
                string composedFile;

                prj = doc.VBProject;
                foreach (VBA.VBComponent comp in prj.VBComponents)
                {
                    code = comp.CodeModule;

                    // Put the name of the code module at the top
                    composedFile = comp.Name + Environment.NewLine;

                    // Loop through the (1-indexed) lines
                    for (int i = 0; i < code.CountOfLines; i++)
                    {
                        composedFile += code.get_Lines(i + 1, 1) + Environment.NewLine;
                    }

                    // Add the macro to the list
                    macros.Add(composedFile);
                }

                return macros;
            }
        }
    }

    I studied from this article and apply it in Visio:http://efreedom.com/Question/1-1742044/Vba-Scan-Documents-Macro-Replace-Macro-Text 

    I hope the code can help you and feel free to follow up after you tried.

    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 MarcWiener Friday, January 7, 2011 2:32 PM
    Friday, January 7, 2011 7:17 AM

All replies

  • Hi MarcWiener,

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

    After reading your post, I knew that you need a C# console app to tell whether a visio document contains macros and is digitally signed. If I have misunderstood you, please let me know.

    As far as I know, you can use Microsoft.Vbe.Interop to judge whether the document contain macros, bellowing is my code to get the Macro in Visio document:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using VBA = Microsoft.Vbe.Interop;
    using Visio = Microsoft.Office.Interop.Visio;
    using Excel = Microsoft.Office.Interop.Excel;

    namespace JudgeIfVisioContainMacro
    {
        class Program
        {
            static void Main(string[] args)
            {
                string fileDir = Environment.CurrentDirectory;
                if (fileDir.EndsWith("\\bin\\Debug"))
                {
                    fileDir = fileDir.Replace("bin\\Debug""");
                }
                string fileName = "TestMacro.vsd";
                Visio.Application visioApp = new Visio.Application();
                visioApp.Visible = true;
                Visio.Document doc = visioApp.Documents.Open(fileDir + fileName);
                List<string> lstMacros = new List<string>();

     
                lstMacros = GetMacrosFromDoc(doc);
                if (lstMacros.Count > 0)
                {
                    Console.WriteLine("There are " + lstMacros.Count.ToString() + " procedures in " + fileName + ":");
                    foreach (string macro in lstMacros)
                    {
                        Console.WriteLine(macro);
                        Console.WriteLine("-----------------------------------------------------");
                    }
                }
                else
                {
                    Console.WriteLine("There are not procedures in the document");
                }

                doc.Close();
                visioApp.Quit();
            }

            public static List<string> GetMacrosFromDoc(Visio.Document doc)
            {
                List<string> macros = new List<string>();

                VBA.VBProject prj;
                VBA.CodeModule code;
                string composedFile;

                prj = doc.VBProject;
                foreach (VBA.VBComponent comp in prj.VBComponents)
                {
                    code = comp.CodeModule;

                    // Put the name of the code module at the top
                    composedFile = comp.Name + Environment.NewLine;

                    // Loop through the (1-indexed) lines
                    for (int i = 0; i < code.CountOfLines; i++)
                    {
                        composedFile += code.get_Lines(i + 1, 1) + Environment.NewLine;
                    }

                    // Add the macro to the list
                    macros.Add(composedFile);
                }

                return macros;
            }
        }
    }

    I studied from this article and apply it in Visio:http://efreedom.com/Question/1-1742044/Vba-Scan-Documents-Macro-Replace-Macro-Text 

    I hope the code can help you and feel free to follow up after you tried.

    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 MarcWiener Friday, January 7, 2011 2:32 PM
    Friday, January 7, 2011 7:17 AM
  • Thanks. This works, I'm not sure what I was missing earlier as this is similar to my other code. It's usually the obvious that I overlook. Much appreciated.

     

    Marc


    Marc Wiener
    Friday, January 7, 2011 2:32 PM
  • Actually one more thing. It doesn't appear as if the Visio object model has an VBASigned property. My goal was to output a warning if the Visio contained unsigned code. Any thoughts on this?

     

    Marc


    Marc Wiener
    Friday, January 7, 2011 3:44 PM
  • Hi MarcWiener,

    It seems that there exist the Protection property of VBProject, and you can judge whether it is protected(signed) via it.

                bool IsProtect = Convert.ToBoolean(doc.VBProject.Protection);
                if (IsProtect)
                {
                    Console.WriteLine("There project was protected, can't access the macro!");
                }

    Hope it can help you and feel free to follow up.

    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.

    Monday, January 10, 2011 11:04 AM
  • Thanks. I had seen the Protection property, but it only relates to protecting the VBA project through the interface, not being digitally signed. The VBASigned method only appears to exist in the object browser for Word, Excel , PowerPoint, and MS Project. It looks like for the time being anyway, we'll have to live without programatically checking Visio.

    Marc


    Marc Wiener
    Monday, January 10, 2011 2:29 PM
  • THIS  CODE IS NOT WORKING PROPERLY . . . . .PLEASE GUIDE ME ABOUT AN ERROR

    Error 1 Cannot implicitly convert type 'object' to 'Microsoft.Vbe.Interop.VBProject'. An explicit conversion exists (are you missing a cast?) C:\Documents and Settings\htahir\My Documents\Visual Studio 2008\Projects\ConsoleApplication5\ConsoleApplication5\Program.cs 55 19 ConsoleApplication5
     

     

    ON THIS LINE

    prj = doc.VBProject;

    WHATS IS THE PROBLEM . . . . PLEASE GUIDE ME AS SOON AS POSSIBLE

    I INCLUDES ALL THE REFERENCES

    Tuesday, July 19, 2011 3:43 PM