我有以下vba代码。它创建新的Excel应用程序并使用它来打开文件。然后它在这个文件中MsgBoxes一些单元格的值。
Sub TestInvis()
Dim ExcelApp As Object
Set ExcelApp = CreateObject("Excel.Application")
Dim WB As Workbook
Set WB = ExcelApp.Application.Workbooks.Open("Y:\vba\test_reserves\test_data\0503317-3_FO_001-2582480.XLS")
Dim title As String
title = WB.Worksheets(1).Cells(5, 4).Value
MsgBox (title)
WB.Save
WB.Close
ExcelApp.Quit
Set ExcelApp = Nothing
End Sub
问题是在MsgBoxing之后它减慢并最终给出Microsoft Excel is waiting for another application to complete an OLE action
窗口。为什么这样做?它不像是有任何硬命令被实现。我该如何处理呢?
答案 0 :(得分:1)
这是因为ExcelApp
中的Excel实例很可能正在等待用户输入。
您可以尝试添加ExcelApp.DisplayAlerts = False
以跳过可能存在的任何弹出窗口。
此外,在排查故障时添加行ExcelApp.Visible = True
,以便您可以查看第二个实例中发生了什么,并在那里进行故障排除。
我在以下情况遇到此问题:
msgBox
之前)请放心,无论如何问题不在于您的代码本身 - 它在这里运行良好。
答案 1 :(得分:1)
适用于我的代码。
您可以从FileDialog中选择文件。在注释中您有代码关闭工作簿而不保存更改。希望它有所帮助。
Option Explicit
Sub Import(Control As IRibbonControl)
Dim fPath As Variant
Dim WB As Workbook
Dim CW As Workbook
On Error GoTo ErrorHandl
Set CW = Application.ActiveWorkbook
fPath = Application.GetOpenFilename(FileFilter:="Excel file, *.xl; *.xlsx; *.xlsm; *.xlsb; *.xlam; *.xltx; *.xls; *.xlt ", Title:="Choose file You want to openn")
If fPath = False Then Exit Sub
Application.ScreenUpdating = False
Set WB = Workbooks.Open(FileName:=fPath, UpdateLinks:=0, IgnoreReadOnlyRecommended:=True)
Set WB = ActiveWorkbook
MsgBox("File was opened.")
'Application.DisplayAlerts = False
'WB.Close SaveChanges:=False
'Application.DisplayAlerts = True
'MsgBox ("File was closed")
Exit Sub
ErrorHandl:
MsgBox ("Error occured. It is probable that the file that You want to open is already opened.")
Exit Sub
End Sub