工作簿打开后运行auto_open

我正在尝试创build打开工作簿时运行的代码。 我尝试使用Auto_open和workbook_open(在ThisWorkbook对象内)但是我对它们都有问题。 问题是代码需要来自我正在打开的工作簿的信息正如你可以看到在这个代码的和平:

Sub Auto_Open() Dim fileNam As String Dim text As String Dim answer As String Dim question As String Dim quesPos As Integer MsgBox "add-in start" 'On Error GoTo GetOut fileNam = ThisWorkbook.FullName jsonFile = Replace(jsonFile, "xls", "survey.descriptor.json") Open jsonFile For Input As #2 pos = 1 ThisWorkbook.Sheets("Result").Select 'The code gives the error here 

由于excel文件尚未打开,因此在获取文件名时会出现错误。 如何在代码打开时执行代码,但在打开代码后?

如果你需要一些代码来运行AFTER工作簿是打开的(而不是“因为它被打开”),一个解决scheme是创build一个定时事件 – 设置定时器5秒钟,在auto_open触发它,并拥有它直到文件“正常打开”。 这可能看起来像这样:

Auto_Open()

 fullyOpenTime = Now + TimeValue("00:00:05") Application.OnTime alertTime, "LetsGo" 

然后创build一个工作簿打开后运行的另一个子工具:

 Sub LetsGo() On Error Resume Next ' loop around until no error was triggered Do DoEvents fileName = Application.ActiveWorkbook.FullName Application.Wait DateAdd("s", 1, Now) ' "cheap trick" to wait one second While Err.Number <> 0 On Error GoTo 0 ' when you get here, you have an active workbook.