MS Access: Open 2016 Excel and getObject - excel

I've been struggling with this problem for the last few hours and tried multiple solutions to no avail.
I'm trying to open an excel 2016 (64-bit) report in MS Access, the default excel is 2003 and must stay that way for now.
My code is:
Dim xlTmp As excel.Application
Shell ("C:\Program Files\Microsoft Office\root\Office16\EXCEL.exe")
Set xlTmp = GetObject(, "Excel.Application")
This code does exactly what I want only when I am stepping through in debug mode, because excel doesnt fully start up before it tries to grab the object.
If run normally it throws up an error:
Run-Time error '429'
ActiveX component can't create object
I've tried some of the following solutions to no avail:
Function OpenExcel()
x = Shell("C:\Program Files\Microsoft Office\root\Office16\EXCEL.exe", vbNormalFocus)
OpenExcel = x
End Function
Function GetOpenExcel() As excel.Application
Set GetOpenExcel = GetObject(, "Excel.Application.16")
TryAgain:
On Error GoTo NoExcel
Set xlTmp = GetObject(, "Excel.Application.16")
On Error GoTo 0
xlTmp.Visible = True
Set GetOpenExcel = xlTmp
Exit Function
NoExcel:
Resume TryAgain
End Function
And this
Set ie = Nothing
cnt = 0
cnt = xlTmp.Windows.count
sh = Shell("C:\Program Files\Microsoft Office\root\Office16\EXCEL.exe", vbNormalFocus)
Do Until xlTmp.Windows.count = cnt + 1
Loop
Set xlTmp = GetObject("excel.Application.16")
Set sh = xlTmp.Windows(xlTmp.Windows.count - 1)
I've also tried switching around my references a bit, with a little confusion, but here is what I have at the moment:
MS Access References
Thanks in advance and any help would be appreciated.

Just use
Set xlTmp = CreateObject("Excel.Application")
instead of GetObject
UPDATE
This code worked for me, I have default 2016 and not default 2010:
Private Sub Command0_Click()
Dim xlTmp As Object
Shell "D:\Program Files (x86)\Microsoft Office 2010\Office14\EXCEL.EXE", vbNormalFocus
TryAgain:
On Error GoTo NoExcel
Set xlTmp = GetObject(, "Excel.Application")
On Error GoTo 0
'here I received correct xlTmp
Exit Sub
NoExcel:
Resume TryAgain
End Sub

Related

open excel file without creating new instance

i've looked up all topics regarding this and still can't find solution.
My main issue is that any time I'm opening excel file from my application it gets opened in new instance. What i want - detect if there's running excel, if there is - use this instance, if not - create new one.
This is not working:
Dim xlsApp As Object
Dim AllExcelProcess() As Process = System.Diagnostics.Process.GetProcessesByName("Excel")
If AllExcelProcess.Length = 0 Then xlsApp = New Excel.Application Else xlsApp = GetObject(, "Excel.Application")
AllExcelProcess works correctly returning that there's running excel application, yet the GetObject always returns nothing. What am I doing wrong?
This works for me from within Word and an excel instance already open. Microsoft 365.
Sub main()
On Error Resume Next
Set excelapp = GetObject(class:="Excel.Application")
'Clear the error between errors
Err.Clear
If excelapp Is Nothing Then Set excelapp = CreateObject(class:="Excel.Application")
If Err.Number = 429 Then
MsgBox "Excel could not be found, aborting."
Exit Sub
End If
On Error GoTo 0
excelapp.Workbooks("Book2").sheets("Sheet1").Range("j2").Value = 2
End Sub
Easier way to do what I wanted:
On Error Resume Next
Dim xlsApp As Object = GetObject(, "Excel.Application")
On Error GoTo 0
If xlsApp Is Nothing Then xlsApp = New Excel.Application
I had 2 issues you might want to check out:
You have Microsoft Excel library loaded under References. If you can't find it, add existing EXCEL.EXE to it
If still not working, try GetObject on other objects, can VS detect their classes or not. In my case it was not working at all
Solution:
I added MS Excel library, created new solution. GetObject started working there, everything further was easy using above code.

How do I use an Outlook macro to connect to an excel file that's already open?

I'm trying to write a Macro that will refer out to a file I use to track credits(I call it Master6 below). In the code excerpt below I try to do some initial testing like, see if excel is even running, or if the Master6 file is already open. I'm able to get past the line of code where it correctly determines that the Master6 file is open, but then I get a Run Time error 9 at the part where it says "Set xWb = xExcelApp.Workbooks(xExcelFile)". At this line of code I'm trying to specify what xWb(aka my workbook) should be.
Note that I originally asked this question and I got one response saying, "You need to make sure that both applications are run under the same security context (run as admin or same user). There is no way to access/automate application which is run under a different security context." I don't know how to determine what security context a file is open under/how to make it match security contexts, even though I tried researching it.
Sub SearchQueryForCredits()
'Declare some initial variables
Dim xExcelFile As String
Dim xExcelApp As excel.Application
Dim xWb As excel.Workbook
Dim xWs As excel.Worksheet
Dim xExcelRange As excel.Range
Dim Cell As Range
xExcelFile = "C:\Users\USER\Documents\Desktop Credits\MASTER6.xlsm"
'initiate Excel object...Therefore, you can use this to test if there is an Excel instance already open on your machine.
On Error Resume Next
Set ExApp = GetObject(, "Excel.Application")
On Error GoTo 0
If ExApp Is Nothing Then Set ExApp = New excel.Application
'LOGIC TO TELL IF THE MASTER6 FILE IS ALREADY OPEN
Dim Ret
Ret = IsWorkBookOpen(xExcelFile)
'What to do if the Master6 file is open
If Ret = True Then
Set xExcelApp = GetObject(, "Excel.Application")
If Err <> 0 Then
MsgBox "MASTER6 is not running"
Else
MsgBox "MASTER6 is running"
End If
Set xWb = xExcelApp.Workbooks(xExcelFile) <---- THIS IS WHERE I GET "RUN TIME ERROR '9': SUBSCRIPT OUT OF RANGE
Set xWs = xWb.Sheets("Query Run")
Set xExcelRange = xWs.Range("I:I")
'What to do if the Master6 file is NOT open
Else.......'rest of code i don't think is relevant to this issue
Also, my code above uses the function below in order to tell if my Master6 file is open.
Function IsWorkBookOpen(FileName As String)
Dim ff As Long, ErrNo As Long
On Error Resume Next
ff = FreeFile()
Open FileName For Input Lock Read As #ff
Close ff
ErrNo = Err
On Error GoTo 0
Select Case ErrNo
Case 0: IsWorkBookOpen = False
Case 70: IsWorkBookOpen = True
Case Else: Error ErrNo
End Select
End Function
Any assistance would be greatly appreciated! Thanks!
If Excel is run under a different security context your VBA macro will not find the process in the code, so the following code:
Set xExcelApp = GetObject(, "Excel.Application")
Will not be successful. You will have to create a new Excel instance and open the file anew.
' Start Excel and get Application object.
Set oXL = CreateObject("Excel.Application")
oXL.Visible = True
Read more about that in the How to automate Microsoft Excel from Visual Basic article.
Also you may consider using the Open XML SDK if you deal with open XML documents only.

Accessing open workbook in a sub generates Error 1004 "Method of 'Sheets' of Object '_Global' not failed" sometimes

I am getting inconsistent results when I try to refer to an active workbook. About half the time I get the "Method of 'Sheets' of Object '_Global' not failed" error and other times the code works fine. I don't see a pattern.
The VBA code is part of a Word document that allows the user to open a template Excel file and select/copy text from the Word doc into rows on the Excel file.
In a previous sub I successfully open an Excel template file (I call it a RTM template). In the code below I want to activate the "RTM" worksheet, select the first cell where the template could already have data in it from a previous execution and if there is, then count how many rows of data exist. In this way the new data will be posted in the first row which does not have any data. I am using named ranges in my Workbook to refer to the starting cell ("First_Cell_For_Data").
When I run my code sometimes it runs without error and other times it stops on the "Sheets("RTM").Activate" and gives me the "Method...." error. The same result occurs when I change the variable definition of wb_open to Object. I have also tried using "wb_open.Sheets("RTM").Activate" with the same results.
As suggested in the comments below I added "If wb_open is nothing ...." to debug the issue. I also added the sub List_Open_Workbooks which enumerates the open workbooks (of which there is only 1) and activates the one that matches the name of the one with the correct filename. This is successful. But upon returning to Check_Excel_RTM_Template I still get the Method error on the "Sheets("RTM").Activate" line.
Second Update: after more time diagnosing the problem (which still occurs intermittently) I have added some code that may help getting to the root of the problem. In the "List_Open_Workbooks" sub I test for xlApp.Workbooks.Count = 0. So all references to an open Excel workbook will fail. At this point my template workbook is open in Windows. Am I drawing the correct conclusion?
Third Update: I tried Set wb_open = GetObject(str_filename) where str_filename contains the name of the Excel template file I just opened.
I get the following error message.
Also, I noticed that if I start with a fresh launch of Word and Excel it seems to run just fine.
Sub Check_Excel_RTM_Template(b_Excel_File_Has_Data As Boolean, i_rows_of_data As Integer)
Dim i_starting_row_for_data As Integer
Dim wb_open As Object
Set wb_open = ActiveWorkbook
i_rows_of_data = 0
If wb_open Is Nothing Then
MsgBox "RTM Workbook not open in Check_Excel_RTM_Template"
Call List_Open_Workbooks(b_Excel_File_Has_Data, i_rows_of_data)
Else
' On Error GoTo Err1:
' Sheets("RTM").Activate
' range("First_Cell_For_Data").Select
Workbooks(wb_open.Name).Worksheets("RTM").range("First_Cell_For_Data").Select
If Trim(ActiveCell.Value) <> "" Then
b_Excel_File_Has_Data = True
Do Until Trim(ActiveCell.Value) = ""
ActiveCell.Offset(1, 0).Select
i_rows_of_data = i_rows_of_data + 1
Loop
Else
b_Excel_File_Has_Data = False
End If
End If
Exit Sub
Err1:
MsgBox getName(str_Excel_Filename) & " is not a RTM template file."
b_abort = True
End Sub
Sub to enumerate all open workbooks
Sub List_Open_Workbooks(b_Excel_File_Has_Data As Boolean, i_rows_of_data As Integer)
Dim xlApp As Excel.Application
Set xlApp = GetObject(, "Excel.Application")
Dim str_filename As String
Dim xlWB As Excel.Workbook
If xlApp.Workbooks.Count = 0 Then
MsgBox "Error: Windows thinks there are no workbooks open in List_Open_Workbooks"
b_abort = True
Exit Sub
End If
For Each xlWB In xlApp.Workbooks
Debug.Print xlWB.Name
str_filename = getName(str_Excel_Filename)
If Trim(xlWB.Name) = Trim(str_filename) Then
xlWB.Activate
If xlWB Is Nothing Then
MsgBox "Workbook still not active in List_Open_Workbooks"
b_abort = True
Exit Sub
Else
' Sheets("RTM").Activate
Workbooks(xlWB.Name).Worksheets("RTM").range("First_Cell_For_Data").Select
range("First_Cell_For_Data").Select
If Trim(ActiveCell.Value) <> "" Then
b_Excel_File_Has_Data = True
Do Until Trim(ActiveCell.Value) = ""
ActiveCell.Offset(1, 0).Select
i_rows_of_data = i_rows_of_data + 1
Loop
Else
b_Excel_File_Has_Data = False
End If
End If
End If
Next xlWB
Set xlApp = Nothing
Set xlWB = Nothing
End Sub
Function to extract filename from path/filename
Function getName(pf)
getName = Split(Mid(pf, InStrRev(pf, "\") + 1), ".")(0) & ".xlsx"
End Function
I am hoping I found the source of my problem and solved it.
I believe that referring to an open workbook in sub using Dim wb_open As Object & Set wb_open = ActiveWorkbook in the Check_Excel_RTM_Template sub is causing my inconsistent problems....perhaps this is an anomoly (bug) in the VBA implementation in Word.
In the revised code I posted below I am passing the o_Excel object from the calling routine and using oExcel.Activesheet.xxx to reference ranges and values.
Now I next problem is that I am having errors on the form control button code which also uses the Dim wb_open As Object & Set wb_open = ActiveWorkbook approach to referring to the open workbook. But I'll post that as a new question.
Thanks to all who commented and provided suggestions.
Sub Check_Excel_RTM_Template(oExcel As Object)
Dim i_starting_row_for_data As Integer
Dim str_filename As String
i_rows_of_data = 0
On Error GoTo Err1:
oExcel.ActiveSheet.range("First_Cell_For_Data").Select
If Trim(oExcel.ActiveCell.Value) <> "" Then
b_Excel_File_Has_Data = True
Do Until Trim(oExcel.ActiveCell.Value) = ""
oExcel.ActiveCell.Offset(1, 0).Select
i_rows_of_data = i_rows_of_data + 1
Loop
Else
b_Excel_File_Has_Data = False
End If
Exit Sub
Err1:
Documents(str_doc_index).Activate
MsgBox getName(str_Excel_Filename) & " is not a RTM template file."
b_abort = True
End Sub

How should I define an excel object when I get error 462 VBA?

I have an issue with a macro that is located in Outlook. The code is triggered by a "Private Sub" code that creates a "TriggerExcel(1)" or "TriggerExcel(2)" which depends of the mail subject.
When the code is triggered it sometimes got stuck at line: Set ExApp = Excel.Application 'Codes
Where I get the following Error:
My guess is that the code doesn't define the excel object correctly in the code, but I have a hard time to understand how this should be made... Any advice or suggestions are much appreciated.
Public Sub TriggerExcel(Mode As Integer)
‘Activate following tool reference: Tool-References-Microsoft Excel 14.0 Object library -biblioteket
Dim ExApp As Excel.Application
Dim ExWbk As Workbook
Set ExApp = Excel.Application 'Codes
cause error here
ExApp.DisplayAlerts = False
If Mode = 1 Then
On Error Resume Next
Set ExWbk = Workbooks("Nyins.xlsm")
Debug.Print ExWbk.Name
On Error GoTo 0
'Set ExWbk = ExApp.Workbooks.Open("C:\Users\linsten\Desktop\Nyins.xlsm")
ExApp.Visible = False
'ExWbk.Application.Run "mymain.main"
If ExWbk Is Nothing Then
Set ExWbk = ExApp.Workbooks.Open("\\Sca9a\pd-61$\Control\Process\Nyins.xlsm")
End If
ExWbk.Application.Run "MainModule.main"
ElseIf Mode = 2 Then
Set ExApp = Excel.Application
On Error Resume Next
Set ExWbk = Workbooks("Val.xlsm")
Debug.Print ExWbk.Name
On Error GoTo 0
ExApp.Visible = False
If ExWbk Is Nothing Then
Set ExWbk = ExApp.Workbooks.Open("\\Sca9a\pd-61$\Control\Process\Daily\Val.xlsm")
End If
ExWbk.Application.Run "MyMain.Main"
End If
ExWbk.Close
ExApp.Quit
End Sub

Access VBA: working with an existing excel workbook (Run-Time error 9, if file is already open)

I'm writing a macro in Access that (hopefully) will:
create an Excel worksheet
set up and format it based on information in the Access database
after user input, will feed entered data into an existing Excel master file
Opening the blank sheet etc. is working absolutely fine, but I'm stuck trying to set the existing master file up as a variable:
Sub XLData_EnterSurvey()
Dim appXL As Excel.Application
Dim wbXLnew, wbXLcore As Excel.Workbook
Dim wsXL As Excel.Worksheet
Dim wbXLname As String
Set appXL = CreateObject("Excel.Application")
appXL.Visible = True
wbXLname = "G:\[*full reference to file*].xlsm"
IsWBOpen = fnIsWBOpen(wbXLname)
'separate function (Boolean), using 'attempt to open file and lock it' method
'from Microsoft site.
If IsWBOpen = False Then
Set wbXLcore = appXL.Workbooks.Open(wbXLname, True, False)
'open file and set as variable.
ElseIf IsWBOpen = True Then
wbXLcore = appXL.Workbooks("ResultsOverall.xlsm") 'ERROR HERE.
'file is already open, so just set as variable.
End If
Debug.Print wbXLcore.Name
Debug.Print IsWBOpen
Set appXL = Nothing
End Sub
When the file is closed, this works perfectly. However, when it's open I get:
Run-Time error '9':
Subscript out of range
I'm only just starting to teach myself VBA (very trial and error!) and nothing else I've seen in answers here / Google quite seems to fit the problem, so I'm a bit lost...
Considering that it works fine when the file is closed, I suspect I've just made some silly error in referring to the file - perhaps something to do with the 'createobject' bit and different excel instances??
Any suggestions would be much appreciated! Thanks
Thank you #StevenWalker
Here's the working code:
Sub XLData_EnterSurvey()
Dim appXL As Excel.Application
Dim wbXLnew As Excel.Workbook, wbXLcore As Excel.Workbook
Dim wsXL As Excel.Worksheet
On Error GoTo Handler
Set appXL = GetObject(, "Excel.Application")
appXL.Visible = True
Dim wbXLname As String
wbXLname = "G:\ [...] .xlsm"
IsWBOpen = fnIsWBOpen(wbXLname)
If IsWBOpen = False Then
Set wbXLcore = appXL.Workbooks.Open(wbXLname, True, False)
ElseIf IsWBOpen = True Then
Set wbXLcore = appXL.Workbooks("ResultsOverall.xlsm")
End If
Set appXL = Nothing
'-------------------Error handling------------------
Exit Sub
' For if excel is not yet open.
Handler:
Set appXL = CreateObject("Excel.Application")
Err.Clear
Resume Next
End Sub
Sorry I'm on my phone so I can't go in to too much detail or do much with the code but at a glance I think you might need to add an error handler so that if the file is already open, a different line of code is executed.
Add 'On error go to handler' (before creating the excel object) and at the bottom
Of your code add 'handler:'. In the error handler, use get object rather than create object.
You will have to ensure you use exit sub before the error handler or it will run the handler every time you run the code.
You can see an example of what I mean here: How to insert chart or graph into body of Outlook mail
Although please note in this example it's the other way round (if error 'getting' outlook, then create it).
Example in link:
Set myOutlook = GetObject(, "Outlook.Application")
Set myMessage = myOutlook.CreateItem(olMailItem)
rest of code here
Exit Sub
'If Outlook is not open, open it
Handler:
Set myOutlook = CreateObject("Outlook.Application")
Err.Clear
Resume Next
End sub
If you move the appXL.Workbooks statement to the debugging window, you will find that the names of the items in that collection are without extension.
So in your case, I'm guessing the line should read:
wbXLcore = appXL.Workbooks("ResultsOverall")

Resources