Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #2  
Old 01-20-2015, 11:38 PM
gmayor's Avatar
gmayor gmayor is offline Extract VBA code to save in Word document Windows 7 64bit Extract VBA code to save in Word document Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,143
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

How about the following? This will put all the code from the template in which the macro is run into a new document, each module to a separate section. You can change 'ThisDocument' as required.

You will have to set a reference to Microsoft Visual Basic For Applications Extensibility 5.3

For more information, see http://www.cpearson.com/excel/vbe.aspx

Code:
Option Explicit

Sub CopyVBA()
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim oDoc As Document
Dim oRng As Range
    Set oDoc = Documents.Add
    Set VBProj = ThisDocument.VBProject
    For Each VBComp In VBProj.VBComponents
        Set oRng = oDoc.Range
        With oRng
            .Collapse 0
            .Text = VBComp.name & vbCr & vbCr
            .Paragraphs(1).Range.Style = "Heading 1"
            .Collapse 0
            .Text = ReadModule(VBComp.name)
            .Style = "Normal"
            .Collapse 0
            .InsertBreak wdSectionBreakNextPage
        End With
    Next VBComp
lbl_Exit:
    Set oDoc = Nothing
    Set oRng = Nothing
    Set VBProj = Nothing
    Set VBComp = Nothing
    Exit Sub
End Sub


Function ReadModule(strModule) As String
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim oDoc As Document
    Set oDoc = ThisDocument
    Set VBProj = oDoc.VBProject
    Set VBComp = VBProj.VBComponents(strModule)
    ReadModule = VBComp.CodeModule.Lines(1, VBComp.CodeModule.CountOfLines)
lbl_Exit:
    Set oDoc = Nothing
    Set VBProj = Nothing
    Set VBComp = Nothing
    Exit Function
End Function
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract VBA code to save in Word document VBA Code for clean all data from ms word document egyp7 Word VBA 4 05-16-2014 03:59 PM
Extract form fields to Word Document RonNCmale Word VBA 22 01-11-2014 05:06 AM
Word ask to save template whenever i save a derived document jorbjo Word 3 10-04-2012 10:52 AM
Extract VBA code to save in Word document VBA code to extract specific bookmarks from multiple word files Rattykins Word VBA 4 06-27-2012 10:02 PM
Extract Numbers from Zip Code Karen615 Excel 3 09-21-2011 06:54 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:29 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft