I have a classic VB6 project which contains forms and modules. I would like to add a Sub procedure to one of the modules which will print to a file the source VB6 code in all the forms and modules in the project. I didn’t know how to do this so I asked ChatGPT 4.0 to write the code for the Sub procedure. ChatGPT generated the code, and I was told to add a reference to “Visual Basic for Applications Extensibility 5.3” to the project. I added the reference and ran the Sub procedure. The program stopped when it reached the line “Set vbProj = VBIDE.VBE.ActiveVBProject”. “VBE” was highlighted and the error message was “Method or data member not found”. Below is the Sub procedure. I would be grateful if someone could suggest a correction to get this to work. Thank you.
Public Sub ExportAllCode()
Dim fso As Object
Dim fileStream As Object
Dim vbComp As Object
Dim vbProj As Object
Dim vbComps As Object
Dim vbCompItem As Object
Dim lineNum As Long
Dim codeLine As String
' Create a FileSystemObject to handle the file operations
Set fso = CreateObject("Scripting.FileSystemObject")
' Create or open the output file
Set fileStream = fso.CreateTextFile(App.Path & "AllCode.txt", True)
' Get the VB Project
Set vbProj = VBIDE.VBE.ActiveVBProject
Set vbComps = vbProj.VBComponents
' Loop through all the components (Forms, Modules, Classes) in the project
For Each vbComp In vbComps
' Write the component name to the file
fileStream.WriteLine "Component: " & vbComp.Name
fileStream.WriteLine String(50, "=")
' Write the code lines of the component to the file
For lineNum = 1 To vbComp.CodeModule.CountOfLines
codeLine = vbComp.CodeModule.Lines(lineNum, 1)
fileStream.WriteLine codeLine
Next lineNum
' Add a separator between components
fileStream.WriteLine String(50, "=")
fileStream.WriteLine
Next vbComp
' Close the file
fileStream.Close
' Clean up
Set fileStream = Nothing
Set fso = Nothing
Set vbComp = Nothing
Set vbComps = Nothing
Set vbProj = Nothing
MsgBox "Code export complete. The file 'AllCode.txt' has been created in the application path.", vbInformation
End Sub