8

I'm trying to rename the ThisWorkbook code module of an Excel worksheet using VBA Extensibility.

I accomplish this with the line

ThisWorkbook.VBProject.VBComponents("ThisWorkbook").Name = "wb"

I can see the change in the VB editor and also access the workbook's properties, e.g. Debug.? wb.Name.

However: If I save and close the file and then reopen it, I get strange behavior. If the code module was empty before renaming it, it reverts back to the old empty ThisWorkbook name.

If it was not empty or was populate before the saving, I now have both, an empty ThisWorkbook module (that would fire events if there were any) - and the filled wb module - which does not fire workbook events:

enter image description here

Has anyone seen this behavior - and knows a fix/workaround?

4

1 回答 1

10

快速回答: ThisWorkbook.[_CodeName] = "newName"

详细解答

当我添加引用Microsoft Visual Basic For Applications Extensibility 5.3并运行您的行时

ThisWorkbook.VBProject.VBComponents("ThisWorkbook").Name = "wb"

ThisWorkbookName 属性实际上并没有被修改

在此处输入图像描述

保存并重新打开文件会导致ThisWorkbook对象重复

在此处输入图像描述

这几乎意味着现在我Workbook在一个工作簿中有两个对象,并且都被命名ThisWorkbook


解决方法是使用“属性”窗口将其重命名ThisWorkbookwb

ThisWorkbook.[_CodeName] = "newName"因为ThisWorkbook.CodeName是只读的。

于 2013-11-05T15:29:45.857 回答