I’m trying to update an Excel file from Outlook (Office 2010). How do I reference and access Excel?
As a simple test I’m trying to count the number of open workbooks. When I run this I get 0, even though there are 2 open.
Sub Test()
Dim xlApp As Excel.Application
Dim xlWBook As Excel.Workbook
Set xlApp = New Excel.Application
Debug.Print "xlApp.Workbooks.Count = " & xlApp.Workbooks.Count
On Error Resume Next
Set xlWBook = xlApp.Workbooks("Data.xlsx")
Err.Clear 'Clear error and open File Index
If xlWBook Is Nothing Then
Set xlWBook = xlApp.Workbooks.Open("C:\Users\Chris\Desktop\Data.xlsx")
End If
End Sub
This is what I use to detect Excel:
once xlApp is set, you can use
xlApp.Workbooks.Countto count the worksheetsNote: This code will get the first one opened, if there are more than one instance of Excel to find
If you have to find a specific workbook, from this page,
Set xlApp = GetObject("Book2").Applicationwill find the workbook, even if it’s not in the first instance. If the workbook is from a file, or already saved, replacebook2with the full path and filename – Side effect – this will also open the file if it is not already openMore usage info: http://msdn.microsoft.com/en-us/library/aa164798%28v=office.10%29.aspx