Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 49729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:28:57+00:00 2026-05-10T16:28:57+00:00

Is it possible to extract all of the VBA code from a Word 2007

  • 0

Is it possible to extract all of the VBA code from a Word 2007 ‘docm’ document using the API?

I have found how to insert VBA code at runtime, and how to delete all VBA code, but not pull the actual code out into a stream or string that I can store (and insert into other documents in the future).

Any tips or resources would be appreciated.

Edit: thanks to everyone, Aardvark‘s answer was exactly what I was looking for. I have converted his code to C#, and was able to call it from a class library using Visual Studio 2008.

using Microsoft.Office.Interop.Word; using Microsoft.Vbe.Interop;  ...  public List<string> GetMacrosFromDoc() {     Document doc = GetWordDoc(@'C:\Temp\test.docm');      List<string> macros = new List<string>();      VBProject prj;     CodeModule code;     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; } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T16:28:57+00:00Added an answer on May 10, 2026 at 4:28 pm

    You’ll have to add a reference to Microsoft Visual Basic for Applications Extensibility 5.3 (or whatever version you have). I have the VBA SDK and such on my box – so this may not be exactly what office ships with.

    Also you have to enable access to the VBA Object Model specifically – see the ‘Trust Center’ in Word options. This is in addition to all the other Macro security settings Office provides.

    This example will extract code from the current document it lives in – it itself is a VBA macro (and will display itself and any other code as well). There is also a Application.vbe.VBProjects collection to access other documents. While I’ve never done it, I assume an external application could get to open files using this VBProjects collection as well. Security is funny with this stuff so it may be tricky.

    I also wonder what the docm file format is now – XML like the docx? Would that be a better approach?

    Sub GetCode()      Dim prj As VBProject     Dim comp As VBComponent     Dim code As CodeModule     Dim composedFile As String     Dim i As Integer      Set prj = ThisDocument.VBProject         For Each comp In prj.VBComponents             Set code = comp.CodeModule              composedFile = comp.Name & vbNewLine              For i = 1 To code.CountOfLines                 composedFile = composedFile & code.Lines(i, 1) & vbNewLine             Next              MsgBox composedFile         Next  End Sub 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the Resharper trial and VS2008. Is it possible to extract all classes
I would like to know if it is possible, To extract the userid from
It is possible to extract all tables and rows of each table with one
I have a MySQL table and I want to extract all rows except if
I try to extract all files from .zip containing subfolders in one folder. I
I am wondering if it is possible to extract the index position in a
Although I'm doubtful, I'm curious as to whether it's possible to extract primitive-type template
I need to extract options in ``particular select tag. Is it possible to accomplish
Is there a programmatic way to extract equations (and possibly images) from an MS
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.