Remove old macro and Create a new macro in visio file programmatically (C#) RRS feed

  • Question

  • Hi Requirement is like this 1. Remove existing macro in the visio document. 2. Add new macro in that document.

            string vdwFileName = @"C:\VisioHeaderXml\HeaderOnly.vdx";
            string StrCode = System.IO.File.ReadAllText(@"C:\VisioHeaderXml\Template Macros.txt");
            Visio.Application app = new Visio.Application();
            app.Visible = false;
            //Visio.Document doc1 = app.Documents.Add(vdwFileName);
            Visio.Document doc1 = app.Documents.Open(vdwFileName);
            foreach (VBA.VBComponent component in doc1.VBProject.VBComponents)
                   component.CodeModule.DeleteLines(1, component.CodeModule.CountOfLines);
            VBA.VBComponent vbc = doc1.VBProject.VBComponents.Add(VBA.vbext_ComponentType.vbext_ct_StdModule);
            vbc = null;

    I managed to delete the lines in the macro  using this code

    component.CodeModule.DeleteLines(1, component.CodeModule.CountOfLines);

    But Is it right approach to delete the macro.

    Also I want to create a new macro (ThisDocument). 

    later i will insert a vba code in that macro.

    Macro experts pls help to sort this issues.


    Thursday, May 23, 2013 2:31 PM

All replies

  • Hi Siva,

    Thank you for posting in the MSDN Forum.

    I'm trying to involve some senior engineers into this issue and it will take some time. Your patience will be greatly appreciated.

    Sorry for any inconvenience and have a nice day!

    Best regards,

    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 24, 2013 10:55 AM
  • Hi Zhang,

    Thanks in Advance. It could be really helpful, if we got a solution.


    Sunday, May 26, 2013 7:07 AM