ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Reading codelines? (https://www.excelbanter.com/excel-programming/449422-reading-codelines.html)

Charlotte E.[_3_]

Reading codelines?
 
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





witek

Reading codelines?
 
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







witek

Reading codelines?
 
witek wrote:
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









Dim vb As VBE is not needed.



Charlotte E.[_3_]

Reading codelines?
 
Thanks, Witek,

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

CE


"witek" wrote in message
...
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










All times are GMT +1. The time now is 09:37 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com