Excel加载项 - 获取“本工作簿”的工作簿名称 [英] Excel add-in - get workbook name of "thisworkbook"

查看:231
本文介绍了Excel加载项 - 获取“本工作簿”的工作簿名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试开发一个每次运行的加载项您打开Excel并读取打开文件的文件名。



如果文件名为 XCFIL.SKV 然后执行某些东西...



这段代码应该这样做,但是没有。我缺少什么?

代码停止,如果我调试并按F8它可以正常工作,但它不会运行在它自己的。

  Private Sub Workbook_Open()
如果ThisWorkbook.Name =XCFIL.SKV然后
MsgBoxy
如果

End Sub


解决方案



基于此语句代码停止,如果我调试并按F8,它的工作正常,但它不会运行在它自己的。 >我假设问题依赖于与代码不同步的处理器的速度(自己的经验)。

解决方案:


由于它是Excel,而且问题似乎只依赖于实例本身的打开,可能会使用



正如您所看到的那样应用程序是Excel应用程序的第一个循环,然后是打开,之后是工作簿对象,可能会发生在生活这个生命周期中,打开开始时没有创建worbook,因此,你需要等到excel解决这个问题。


I can't get the name of the workbook when I use a add-in.

I'm trying to develop a add-in that runs each time you open Excel and reads the filename of the open file.

If the file name is XCFIL.SKV then do something...

This code should do it, but it doesn't. What am I missing?
The code stops and if I debug and press F8 it works fine, but it won't run on it's own.

Private Sub Workbook_Open()
    If ThisWorkbook.Name = "XCFIL.SKV" Then
        MsgBox "y"
    End If

End Sub

解决方案

Background:
Based in this statement The code stops and if I debug and press F8 it works fine, but it won't run on it's own. I assume the problem relies on the speed of the processor that is not sync with the code (own experience).
Solution:

Since it is Excel and the problem seems to rely only in the opening of the instance itself, you may use Application wait or any of the other functions for this matter.
Further thoughts:

Life cycle comes to my mind in these kind of scenarios. This web page has a neat Lifecycle diagram of excel instance (attached since I will try to explain the scenario)

As you may see "Application" is the first cycle of the Excel application, followed by "Open" and after that "Workbook" object, it may happen that in this life cycle "worbook" has not been created when "Open" comes to play,hence, you need to wait until excel solves this.

这篇关于Excel加载项 - 获取“本工作簿”的工作簿名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆