I fugured out what I needed to do.
Every '.nvs' file in the folder 'MyPath' will have the module called
'Tracker' added to it. That module will be populated with code from the
file 'pathtracker.txt'
Here is my solution:
Sub AllFolderFiles()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Dim VBComp As VBComponent
Dim wb As Workbook
Dim TheFile As String
Dim MyPath As String
Dim VBCM As CodeModule
MyPath = "D:\Data\TrackerLayout"
ChDir MyPath
' Does an operation to all .xnv files in a folder
TheFile = Dir("*.xnv")
Do While TheFile <> ""
Set wb = Workbooks.Open(MyPath & "\" & TheFile)
' if the module called tracker already exists, delete it
If ModuleExists("Tracker") = True Then
Set VBComp = ActiveWorkbook.VBProject.VBComponents("Tracker")
ActiveWorkbook.VBProject.VBComponents.Remove VBComp
End If
' add a module called tracker
Set VBComp =
ActiveWorkbook.VBProject.VBComponents.Add(vbext_ct_StdModule)
VBComp.Name = "Tracker"
' in the module called tracker, add the code from the text document
ModuleName = "Tracker"
ImportFromFile = "D:\data\tracker\pathtracker.txt"
Set VBCM = wb.VBProject.VBComponents(ModuleName).CodeModule
VBCM.AddFromFile ImportFromFile
' close the active workbook, and go to the next file in the folder
ActiveWorkbook.SaveAs FileName:=MyPath & "\" & TheFile,
FileFormat:=xlNormal _
, Password:="", WriteResPassword:="",
ReadOnlyRecommended:=False, _
CreateBackup:=False
wb.Close
TheFile = Dir
Loop
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
' tests to see if the module already exists
Function ModuleExists(ModuleName As String) As Boolean
On Error Resume Next
ModuleExists = Len( _
ActiveWorkbook.VBProject.VBComponents(ModuleName).Name) <> 0
End Function