Sub test()
p = "C:\指定文件夹\"
f = Dir(p & "*.xls")
Do While f <> ""
Workbooks.Open p & f
With ActiveWorkbook.VBProject.VBComponents("thisworkbook").CodeModule
.InsertLines 1, "sub test()"
.InsertLines 2, "msgbox ""just a test""" '双引号中的双引号,2个代表1个.
.InsertLines 3, "end sub"
End With
ActiveWorkbook.Close True
f = Dir
Loop
End Sub