如果 Excel 工作簿已打开,则...... VBA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7772986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
if Excel Workbook is open then...... VBA
提问by Russell Saari
How could I write code to say.
我怎么能写代码说。
Dim xlApp As Excel.Application
Dim xlWorkbook As Excel.Workbook
Dim xlWorksheet As Excel.Worksheet
if an excelWorkbook is already open then....
如果excel工作簿已经打开,那么......
Set xlApp = GetObject(, "Excel.Application")
elseif xlApp is nothing then
Set xlApp = New Excel.Application
xlApp.Visible = True
Set xlWorkbook = xlApp.Workbooks.Open("E:\InspectionCreator\InspectionSheet.xlsx")
End if
I don't want it to have to be a specific workbook just any workbook I can't seem to find anything on the internet. Any help would be awesome.
我不希望它必须是一个特定的工作簿,只是我在互联网上找不到任何工作簿。任何帮助都是极好的。
回答by Tim Williams
First try using getobject: if it throws an error then use createobject:
首先尝试使用 getobject:如果它抛出错误,则使用 createobject:
Dim xlApp As Excel.Application
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
On Error GoTo 0
If xlApp Is Nothing Then
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
End If
回答by brettdj
I used to run code very similar to Tim's until Kevin Jones pointed out in an Experts-Exchange post that I will repeat here (as the EE post is behind the paywall)
我曾经运行过与 Tim 非常相似的代码,直到 Kevin Jones 在 Experts-Exchange 帖子中指出我将在这里重复(因为 EE 帖子是付费墙后面)
"Be aware that when launching Excel through automation using the CreateObject function, Excel does not load any Add-Ins or other workbooks normally loaded automatically. This is not a good way to start an Excel session that will be used by the user. To start an Excel application instance without using automation from any application other than Excel, the Excel application must be launched using non-automation means. The code below illustrates the steps to do this. The code first tries to obtain an automation handle to an existing application instance. If an existing instance is not found then a new instance is started using the Shell command."
“请注意,当使用 CreateObject 函数通过自动化启动 Excel 时,Excel 不会加载任何加载项或其他通常会自动加载的工作簿。这不是启动用户将使用的 Excel 会话的好方法。启动一个 Excel 应用程序实例,不使用 Excel 以外的任何应用程序的自动化,Excel 应用程序必须使用非自动化方式启动。下面的代码说明了执行此操作的步骤。该代码首先尝试获取现有应用程序实例的自动化句柄. 如果未找到现有实例,则使用 Shell 命令启动一个新实例。”
Dim ExcelApplication As Object
Dim TimeoutTime As Long
On Error Resume Next
Set ExcelApplication = GetObject(, "Excel.Application")
On Error GoTo 0
If ExcelApplication Is Nothing Then
Shell "Excel.exe"
TimeoutTime = Timer + 5
On Error Resume Next
Do
DoEvents
Err.Reset
Set ExcelApplication = GetObject(, "Excel.Application")
Loop Until Not ExcelApplication Is Nothing Or Timer > TimeoutTime
On Error GoTo 0
End If
If ExcelApplication Is Nothing Then
MsgBox "Unable to launch Excel."
Else
' Do something with the Excel instance...
End If
回答by TheFuzzyGiggler
This might be going in the wrong direction but here's something I've used in the past..
这可能朝错误的方向发展,但这是我过去使用过的东西..
If Workbooks.Count > 1 Then 'Or in your case = 0
'Do Something Here'
Else
'Do Something Else'
End If
That way it will tell you if you have more than one workbook open. Otherwise it sounds like you ARE looking to see if something specific is open.
这样它就会告诉您是否打开了多个工作簿。否则,听起来您正在寻找特定的东西是否打开。
Hope that helps.
希望有帮助。