none
Extract VBA code from Document RRS feed

  • Question

  • Hi,

    Is there a way to extract and save VBA/Macro source code from an OpenXML document?

    thx

    K

    Thursday, November 25, 2010 3:05 PM

Answers

  • Hi Kdebe,

    Welcome to MSDN forum and I am glad to help with you.

    After reading your post, I know that you want to extract VBA code from an Open XML document. I assume that you use word document, and get or add VBA code via PIA.

     

    As for your goal, I think the following code can make it: 

      public List<string> GetMacrosFromDoc()
      {
       doc = GetWordDoc(@"test.docm");
    
       List<string> macros = new List<string>();
    
    
       string composedFile;
    
       prj = doc.VBProject;
    
       foreach (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);
       }
    
       CloseDoc(doc);
    
       return macros;
      }
    
      private void CloseDoc(Word.Document doc)
      {
       ((Word._Document)doc).Close();
       wordApp.Quit(Type.Missing,Type.Missing,Type.Missing);
      }
    
      private Word.Document GetWordDoc(string path)
      {
       wordApp = new Word.Application();
    
       return wordApp.Documents.Open(path,
        Type.Missing,
        Type.Missing,
        Type.Missing,
        Type.Missing,
        Type.Missing,
        Type.Missing,
        Type.Missing,
        Type.Missing,
        Type.Missing,
        Type.Missing,
        Type.Missing,
        Type.Missing,
        Type.Missing,
        Type.Missing,
        Type.Missing);
      }
    
      private void button2_Click(object sender, EventArgs e)
      {
       doc = GetWordDoc(@"test.docm");
       prj = doc.VBProject;
       VBComponent module = prj.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule);
       CodeModule code = module.CodeModule;
       module.Name = "Macro4";
       try
       {
        code.InsertLines(1, "sub Test()");
        code.InsertLines(2, "msgbox \"hello\"");
        code.InsertLines(3, "end sub");
       }
       catch (Exception ex)
       {
        throw ex;
       }
       finally
       {
        CloseDoc(doc);
       }
      }  
    

    I hope it can help you and feel free to follow up after you have tried the solution.

    Best Regards,

    Bruce Song


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

    • Proposed as answer by Mike_HelpYou Friday, December 3, 2010 9:51 AM
    • Marked as answer by Bruce Song Tuesday, December 7, 2010 10:36 AM
    Monday, November 29, 2010 6:22 AM