无法将工作表保存在vba脚本中

我正在将所有.xls文档循环到一个目录中,并在每个文件组件中更改一行“ThisWorkbook”

一切工作正常,代码打开文件,我可以看到在运行时,行“testing”插入到ThisWorkbook。 但保存不起作用。 保存function仅适用于床单吗? 如何保存组件中的更改?

excelfile = Dir(path & "*.xls") Do While excelfile <> "" If excelfile <> "merni.xlsm" Then Set wbResults = Workbooks.Open(Filename:=path & excelfile) wbResults.Unprotect Password:="" DoEvents Set codeModule = wbResults.VBProject.VBComponents("ThisWorkbook").codeModule With codeModule.InsertLines(3, "test") End With wbResults.Save wbResults.Close End If excelfile = Dir Loop 

此代码为我工作

 Sub AddTest() Dim wb As Workbook Dim cm As CodeModule Set wb = Workbooks.Open("C:\Users\dick\Book3.xls") Set cm = wb.VBProject.VBComponents.Item("ThisWorkbook").CodeModule cm.InsertLines 3, "test" wb.Save wb.Close End Sub 

但是,这一线

  With cm.InsertLines(3, "test"): End With 

它不会事件编译。 InsertLines是一个方法,并不返回一个对象,所以我敢肯定,你不能使用With块。