简体   繁体   English

在“ThisWorkbook”下丢失 VBA 代码(私有子 WorkBook_Open())

[英]Losing VBA code under 'ThisWorkbook' (Private Sub WorkBook_Open() )

First three elements of the background:背景的前三个元素:

  1. I have created a Excel Template which is used, in turn, to create a 'personalised template' where user name, user comment, and save data pathnames are embedded in the personalised template.我创建了一个 Excel 模板,该模板又用于创建“个性化模板”,其中用户名、用户评论和保存数据路径名嵌入在个性化模板中。
  2. The personalised template is used to produce monthly workbooks (actually, time sheets)个性化模板用于制作月度工作簿(实际上是时间表)
  3. The template has been created in Office/Excel 2007 running under Vista but the (current) target environment is a corporate network running Office/Excel 2003 under XP Professional.该模板是在 Vista 下运行的 Office/Excel 2007 中创建的,但(当前)目标环境是在 XP Professional 下运行 Office/Excel 2003 的公司网络。 Both templates are therefore created as Excel 2003 templates (which in the development environment run in compatibility mode)因此,这两个模板都创建为 Excel 2003 模板(在开发环境中以兼容模式运行)

So far, so good - the templates work well in the development environment - the 'personalisation' code is in a WorkBook_Open( ) routine under 'ThisWorkbook' - it runs, DELETES ITSELF, and saves as the personalised template.到目前为止,一切都很好——模板在开发环境中运行良好——“个性化”代码位于“ThisWorkbook”下的WorkBook_Open( ) 例程中——它运行、删除并保存为个性化模板。 Both templates have the 'process macros' in the Sheet1 code (the principle here is that the initial template has macros in 'ThisWorkbook' and 'Sheet1'; the personalised template has macros only in 'Sheet1' and the workbooks created from the personalised template have NO MACROS AT ALL.两个模板在Sheet1代码中都有“流程宏”(这里的原则是初始模板在“ThisWorkbook”和“Sheet1”中有宏;个性化模板只在“Sheet1”中有宏,从个性化模板创建的工作簿根本没有宏。

In 'beta testing' I had problems with Excel2000 under XP (fileformats) and on a target machine my WorkBook_Open routine was deleted in the main template (instead of in the personalised template) - even though everything worked OK on Excel2007/Vista.在“beta 测试”中,我在 XP(文件格式)下使用 Excel2000 时遇到了问题,在目标机器上,我的 WorkBook_Open 例程在主模板(而不是个性化模板)中被删除——即使在 Excel2007/Vista 上一切正常。

I felt at the time that the invalid deleting of Workbook_Open was probably a coding error -but whilst researching the issues I found a number of references to 'Macafee deleting VBA modules'.我当时觉得 Workbook_Open 的无效删除可能是一个编码错误 - 但在研究这些问题时,我发现了许多关于“Macafee 删除 VBA 模块”的引用。 I did not look into these - but was conscious that the target machine (and environment) run Macafee whilst all my computers run Norton.我没有研究这些——但我意识到目标机器(和环境)运行 Macafee,而我的所有计算机都运行 Norton。

Having recoded, I successfully retested in the following:重新编码后,我成功地重新测试了以下内容:

a) Excel 2007/Vista/Norton a) Excel 2007/Vista/诺顿
b) Excel 2007/XP Professional/Norton b) Excel 2007/XP 专业版/诺顿
c) Excel 2000/XP Home/Norton c) Excel 2000/XP Home/诺顿

so felt comfortable to re-test in Excel 2003/XP Professional/Macafee所以在 Excel 2003/XP Professional/Macafee 中重新测试感觉很舒服

This time I was watching for it - so, once again, saw the WorkBook_Open routine incorrectly deleted from the main template - unfortunately this was only moments before the USB Memory Key (where the template was running from) was completely destroyed.这一次我正在关注它 - 所以,再次看到 WorkBook_Open 例程被错误地从主模板中删除 - 不幸的是,这只是在 USB Memory 密钥(运行模板的位置)被完全破坏之前的片刻。

So to the question(s) - before I go through the whole process again:所以对于问题 - 在我 go 再次完成整个过程之前:

  1. Is there any objective evidence of Macafee removing 'auto-run' VBA modules?是否有任何客观证据表明 Macafee 删除了“自动运行”VBA 模块?
  2. If yes, is there any work-around (this template will be use by three people in a enterprise of ten thousand - so there is no way I will be able to influence/modify the security policies:!! :( ) - if there is a problem with Macafee then I will have to re-think the whole thing!如果是,是否有任何解决方法(此模板将由一万人企业中的三个人使用 - 所以我无法影响/修改安全策略:!! :( ) - 如果有是 Macafee 的问题,那么我将不得不重新考虑整个事情!

To answer your actual question... YES, many antivirus programs absolutely consider autorun code that runs on opening Excel or Word docs to be unsafe, and deletes it, because for a brief moment ten years ago this was actually the way some worms worked.要回答您的实际问题...是的,许多防病毒程序绝对认为在打开 Excel 或 Word 文档时运行的自动运行代码是不安全的,并将其删除,因为十年前这实际上是一些蠕虫的工作方式。 I don't know if MacAfee in particular does this but I've definitely heard of it happening.我不知道 MacAfee 是否特别这样做,但我肯定听说过这种情况。

I have managed to get to the client machine again - and added a new simple template to prove or otherwise my issue.我已经设法再次到达客户端机器 - 并添加了一个新的简单模板来证明或以其他方式证明我的问题。 The new WorkBook_Open routine is...新的WorkBook_Open例程是...

Private Sub WorkBook_Open()
    MsgBox "Hello World"
    With ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
        .DeleteLines 1, _
        .CountOfLines
    End With
    MsgBox "Goodbye to all that!"
End Sub

...and this works exactly as required ie the routine executes then deletes itself (the second msgbox displays even though the code has been deleted! ...这完全按照要求工作,即例程执行然后删除自身(即使代码已被删除,第二个 msgbox 也会显示!

So, this test invalidates my question - McAfee is NOT deleting the routine as part of AV protection - so I am no longer looking for a workaround!因此,此测试使我的问题无效 - McAfee 不会删除例程作为 AV 保护的一部分 - 所以我不再寻找解决方法!

Now all I need to do is to figure out what my problem really is!现在我需要做的就是弄清楚我的问题到底是什么!

Thanks for the comments感谢您的评论

JUst in case anyone else comes to this thread - I have now fully checked out my template - and re-run it in the target environment - and the Workbook_Open routine was again deleted.万一其他人来到这个线程 - 我现在已经完全检查了我的模板 - 并在目标环境中重新运行它 - Workbook_Open 例程再次被删除。 after doing some digging around on the target machine I found the Macafee event log and lo and behold...在目标机器上进行了一些挖掘之后,我找到了 Macafee 事件日志,你瞧……
Event Type: Warning事件类型:警告
Event Source: McLogEvent事件来源:McLogEvent
Event Category: None事件类别:无
Event ID: 258事件编号:258
Date: 09/07/2011日期:09/07/2011
Time: 15:45:40时间:15:45:40
User: NT AUTHORITY\SYSTEM用户:NT AUTHORITY\SYSTEM
Computer:xxxxxxxxxx电脑:xxxxxxxxx
Description: The file F:\Timesheets\Timesheet Generator.xlt\1.OLE contained X97M/Generic Virus .说明:文件 F:\Timesheets\Timesheet Generator.xlt\1.OLE包含 X97M/Generic Virus The file was successfully cleaned with Scan engine version 5400.1158 DAT version 6400.0000.该文件已使用扫描引擎版本 5400.1158 DAT 版本 6400.0000 成功清除。

So there is the definitive proof.所以有确凿的证据。 Same thing happens with Workbook_Activate() Workbook_Activate() 也会发生同样的事情

The 'offending' lines appear to be “违规”行似乎是

With ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
        .DeleteLines 1, _
        .CountOfLines

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM