Charlotte E. wrote:
Hi guys,
Does any of you have a way of reading the first 10 lines of a code module?
To be specific, I need to read the first 10 lines of the code in the module
called 'Module1' into a string-variable.
Currently, I'm doing this by first exporting the module, and then reading it
back in, as a plain text-file.
But, I'm hoping to avoid the external exporting and reading, if possible?
Anyone has a way of reading those code lines directly into a variable?
Thanks,
CE
step 1
Tools - References and add
Microsoft Visual Basic for Application Extensibility library
step 2
Enable trust to visual basic project somewhere in excel options.
step 3
paste this code
Sub TenLines()
Dim
vb As VBE
Dim vpb As VBProject
Set vbp = ThisWorkbook.VBProject
Dim c As VBComponent
For Each c In vbp.VBComponents
If c.Name = "Module1" Then
Dim cm As CodeModule
Set cm = c.CodeModule
Dim code_text As String
code_text = cm.Lines(1, WorksheetFunction.Min(10, cm.CountOfLines))
Debug.Print code_text
End If
Next c
End Sub