Reading codelines?

C

Charlotte E.

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
 
W

witek

Charlotte said:
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
 
W

witek

witek said:
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



Dim vb As VBE is not needed.
 
C

Charlotte E.

Thanks, Witek,

I'm a little hung up the next few days, but I'll test it as soon as
possible...

CE
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top