[英]if Excel Workbook is open then… VBA
How could I write code to say. 我怎么能写代码说。
Dim xlApp As Excel.Application
Dim xlWorkbook As Excel.Workbook
Dim xlWorksheet As Excel.Worksheet
if an excel Workbook 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. 任何帮助都是极好的。
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
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在专家交流帖中指出我将在此重复(因为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
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. 希望有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.