Modules are stored in plain text, with exception of the FRX files
corresponding to UserForm. The FRX is binary data (whose format is not
publicly published as far as I know) and is referenced by the FRM file
as a binary large object, or BLOB.
You're probably better reading the code out of a CodeModule object
rather than from a text file, for a couple reasons. First, the
CodeModule will let you easily find the start line and count of lines
for a procedure. If you were to read in a text file, you'd have to do
all the parsing yourself. It isn't rocket science, but if you don't
have to do it, why do it? Also, the exported text file contains
Attribute statements that are used by the VBA compiler but are not
visible in the VBA IDE. You'd have to add the logic to handle
Attributes to the parsing logic. Not that is wildly difficult, but
then again, why do it?
If you want to put the code in an OLE TextBox on a worksheet, use code
like the following:
Sub AAA()
Dim CodeMod As VBIDE.CodeModule
Dim SL As Long
Dim LC As Long
Dim S As String
Dim WS As Worksheet
Dim TBX As MSForms.TextBox
Set WS = Sheet1
Set TBX = WS.OLEObjects("TextBox1").Object
Set CodeMod =
ThisWorkbook.VBProject.VBComponents("Module1").CodeModule
SL = CodeMod.ProcStartLine("MyCode", vbext_pk_Proc)
LC = CodeMod.ProcCountLines("MyCode", vbext_pk_Proc)
With TBX
.MultiLine = True
.WordWrap = True
.Text = CodeMod.Lines(SL, LC)
End With
End Sub
Make the obvious changes.
For a TextBox on a userform, use code like the following:
Dim CodeMod As VBIDE.CodeModule
Dim SL As Long
Dim LC As Long
Dim S As String
Set CodeMod = _
ThisWorkbook.VBProject.VBComponents("module1").CodeModule
SL = CodeMod.ProcStartLine("MyCode", vbext_pk_Proc)
LC = CodeMod.ProcCountLines("MyCode", vbext_pk_Proc)
S = CodeMod.Lines(SL, LC)
Me.TextBox1.WordWrap = True
Me.TextBox1.MultiLine = True
Me.TextBox1.Text = S
This is basically the same thing as the previous code block.
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)