简体   繁体   English

ActiveWorkbook Path VBA宏Excel 2013出错?

[英]Error with ActiveWorkbook Path VBA macro Excel 2013?

i want to create a vba macro for excel 2013. this vba macro must be available for all excel files, i make a search in the internet and i found that i must put the code in Excel add-in, so i try to make a code for event opening of excel; 我想为Excel 2013创建一个vba宏。此vba宏必须可用于所有excel文件,我在互联网上进行了搜索,发现我必须将代码放入Excel加载项中,因此我尝试制作一个excel事件打开代码; the code is the following: 代码如下:

Private Sub Workbook_Open()
 If (ActiveWorkbook.Path = "C:\GED\TEMP") Then
     MsgBox "Hello"
 End If
End Sub

the problem is that when i open excel file, vba dont know the active workbook because it opens the file in XLSTART first then my current file, so i have the following error:Run-time error '91 ': Object variable or With block variable not set. 问题是当我打开excel文件时,vba不知道活动的工作簿,因为它先在XLSTART中打开文件,然后在当前文件中打开,所以出现以下错误:运行时错误'91':对象变量或带块变量没有设置。 So any idea please; 所以有什么主意吗? i should check the path of the workbook at the opening 我应该在开头检查工作簿的路径

You'll need Application-level events to trap the opening of any workbook. 您将需要应用程序级事件来捕获任何工作簿的打开。 Replace your code with this: 用以下代码替换代码:

Option Explicit
Private WithEvents app As Excel.Application

Private Sub app_WorkbookOpen(ByVal Wb As Workbook)
    If UCase$(Wb.Path) = "C:\GED\TEMP" Then MsgBox "Hello"
End Sub

Private Sub Workbook_Open()
 Set app = Application
End Sub

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

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