PDA

View Full Version : [SOLVED:] Export VBA Macros to bas, text, into document, whatever



AshZM
07-05-2021, 06:37 PM
Hi, I'm trying to export all macros that are in Word.
I've tried tweaking what worked in Excel, to no avail.

Then, I found the below code (author not listed). BUT, I'd like text to be saved instead of being shown in Msg boxes. The below code shows a Msg box for each macro.

Can someone please help? I'd like to either have each macro pasted into one word document, or have each saved as a .bas file. Using a Mac as far as paths...

Thanks!!
-Ashley

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


[-this is where I'd like a file save or paste code. Obviously, nothing with selection.copy worked ;-)]


MsgBox composedFile
Next


End Sub

gmayor
07-05-2021, 08:26 PM
The following will put all the code in a document each module to a new page.


Sub GetCode()
Dim oComp As Object
Dim oCode As Object
Dim i As Integer
Dim oDoc As Document
Dim oRng As Range
If MsgBox("This could take a while!", _
vbInformation + vbOKCancel) = vbCancel Then Exit Sub
Set oDoc = Documents.Add
For Each oComp In ThisDocument.VBProject.VBComponents
Set oRng = oDoc.Range
If Len(oRng) > 2 Then
oRng.Collapse 0
oRng.InsertBreak wdPageBreak
End If
Set oCode = oComp.CodeModule
oDoc.Range.InsertAfter "' " & oComp.Name & vbCr & vbCr
For i = 1 To oCode.CountOfLines
oDoc.Range.InsertAfter oCode.Lines(i, 1) & vbCr
DoEvents
Next
DoEvents
Next
MsgBox "Completed!", vbInformation
End Sub

Paul_Hossler
07-05-2021, 09:02 PM
One way




Sub ExportModules()
Dim sPath As String, sDoc As String, sFile As String
Dim i As Long, iFile As Long, iComponentCount As Long
Dim oComponent As VBComponent


'get document info
sPath = ActiveDocument.Path
sDoc = ActiveDocument.Name
i = InStrRev(sDoc, ".")
sDoc = Left(sDoc, i - 1)


'create output file name
sFile = sPath & "\" & sDoc & ".txt"

'delete if output file exists
On Error Resume Next
Kill sFile
On Error GoTo 0

'create new output file for write
iFile = FreeFile
Open sFile For Output As #iFile




For Each oComponent In ActiveDocument.VBProject.VBComponents
Print #iFile, "*********************************************************************"
Print #iFile, "Start " & oComponent.Name
Print #iFile, "*********************************************************************"

For iComponentCount = 1 To oComponent.CodeModule.CountOfLines
Print #iFile, oComponent.CodeModule.Lines(iComponentCount, 1)
Next

Print #iFile,
Print #iFile, "*********************************************************************"
Print #iFile, "End " & oComponent.Name
Print #iFile, "*********************************************************************"
Print #iFile,
Print #iFile,
Next
Close #iFile


End Sub




PS -- the zip file is really a txt file, just change the extension

AshZM
07-06-2021, 01:44 PM
Thank you so much! Hilarious though, the first page only had two lines of text. I thought, bummer, did not work. Well, it did work, I just had to scroll down...

I've been learning VBA and have made just a mess of everything. The resulting file will help me organize what works and trash my failed efforts. I try to put a 'YAY it works" comment at the top of successful macros ;-)

Chas Kenyon
07-06-2021, 01:53 PM
You may want to visit the Word MVP vba pages (http://wordmvp.com/FAQs/MacrosVBA.htm).

AshZM
07-06-2021, 01:53 PM
Thank you both! The macros work slightly differently and are both exactly what I wanted.
I do not know why so many resources (including other forums) simply say it can't be done.

Anyway, thank you for taking the time to help.