MS Access: Open 2016 Excel and getObject











up vote
0
down vote

favorite












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 FilesMicrosoft OfficerootOffice16EXCEL.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 FilesMicrosoft OfficerootOffice16EXCEL.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 FilesMicrosoft OfficerootOffice16EXCEL.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.










share|improve this question






















  • The approach you probably should take is similar to this answer. Also, I'd build in a substantial wait time between opening Excel and trying to get the class, else the application probably hasn't finished loading yet. A more mature solution would open up the Excel application using WinAPI too to be able to detect it's state and act when subwindows open.
    – Erik von Asmuth
    Nov 8 at 12:08










  • Thanks for the feedback, could you give some more direction for your mature solution. I would like to be able to take advantage of the windows API but not sure where to start with this. Any suggestions on where I can start with this? thanks
    – Thomas Kelly
    Nov 8 at 14:51










  • Well... what I had in mind is: use CreateProcess to create an Excel process and get the process information, then set up a hook to get notified when windows are created. Then, when windows get created you can check if they belong to the just opened Excel process, and you can use the info from the other answer to identify the window that provides the Excel.Application class and retrieve it after creation.
    – Erik von Asmuth
    Nov 8 at 15:04















up vote
0
down vote

favorite












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 FilesMicrosoft OfficerootOffice16EXCEL.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 FilesMicrosoft OfficerootOffice16EXCEL.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 FilesMicrosoft OfficerootOffice16EXCEL.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.










share|improve this question






















  • The approach you probably should take is similar to this answer. Also, I'd build in a substantial wait time between opening Excel and trying to get the class, else the application probably hasn't finished loading yet. A more mature solution would open up the Excel application using WinAPI too to be able to detect it's state and act when subwindows open.
    – Erik von Asmuth
    Nov 8 at 12:08










  • Thanks for the feedback, could you give some more direction for your mature solution. I would like to be able to take advantage of the windows API but not sure where to start with this. Any suggestions on where I can start with this? thanks
    – Thomas Kelly
    Nov 8 at 14:51










  • Well... what I had in mind is: use CreateProcess to create an Excel process and get the process information, then set up a hook to get notified when windows are created. Then, when windows get created you can check if they belong to the just opened Excel process, and you can use the info from the other answer to identify the window that provides the Excel.Application class and retrieve it after creation.
    – Erik von Asmuth
    Nov 8 at 15:04













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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 FilesMicrosoft OfficerootOffice16EXCEL.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 FilesMicrosoft OfficerootOffice16EXCEL.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 FilesMicrosoft OfficerootOffice16EXCEL.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.










share|improve this question













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 FilesMicrosoft OfficerootOffice16EXCEL.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 FilesMicrosoft OfficerootOffice16EXCEL.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 FilesMicrosoft OfficerootOffice16EXCEL.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.







excel vba ms-access






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 11:19









Thomas Kelly

31




31












  • The approach you probably should take is similar to this answer. Also, I'd build in a substantial wait time between opening Excel and trying to get the class, else the application probably hasn't finished loading yet. A more mature solution would open up the Excel application using WinAPI too to be able to detect it's state and act when subwindows open.
    – Erik von Asmuth
    Nov 8 at 12:08










  • Thanks for the feedback, could you give some more direction for your mature solution. I would like to be able to take advantage of the windows API but not sure where to start with this. Any suggestions on where I can start with this? thanks
    – Thomas Kelly
    Nov 8 at 14:51










  • Well... what I had in mind is: use CreateProcess to create an Excel process and get the process information, then set up a hook to get notified when windows are created. Then, when windows get created you can check if they belong to the just opened Excel process, and you can use the info from the other answer to identify the window that provides the Excel.Application class and retrieve it after creation.
    – Erik von Asmuth
    Nov 8 at 15:04


















  • The approach you probably should take is similar to this answer. Also, I'd build in a substantial wait time between opening Excel and trying to get the class, else the application probably hasn't finished loading yet. A more mature solution would open up the Excel application using WinAPI too to be able to detect it's state and act when subwindows open.
    – Erik von Asmuth
    Nov 8 at 12:08










  • Thanks for the feedback, could you give some more direction for your mature solution. I would like to be able to take advantage of the windows API but not sure where to start with this. Any suggestions on where I can start with this? thanks
    – Thomas Kelly
    Nov 8 at 14:51










  • Well... what I had in mind is: use CreateProcess to create an Excel process and get the process information, then set up a hook to get notified when windows are created. Then, when windows get created you can check if they belong to the just opened Excel process, and you can use the info from the other answer to identify the window that provides the Excel.Application class and retrieve it after creation.
    – Erik von Asmuth
    Nov 8 at 15:04
















The approach you probably should take is similar to this answer. Also, I'd build in a substantial wait time between opening Excel and trying to get the class, else the application probably hasn't finished loading yet. A more mature solution would open up the Excel application using WinAPI too to be able to detect it's state and act when subwindows open.
– Erik von Asmuth
Nov 8 at 12:08




The approach you probably should take is similar to this answer. Also, I'd build in a substantial wait time between opening Excel and trying to get the class, else the application probably hasn't finished loading yet. A more mature solution would open up the Excel application using WinAPI too to be able to detect it's state and act when subwindows open.
– Erik von Asmuth
Nov 8 at 12:08












Thanks for the feedback, could you give some more direction for your mature solution. I would like to be able to take advantage of the windows API but not sure where to start with this. Any suggestions on where I can start with this? thanks
– Thomas Kelly
Nov 8 at 14:51




Thanks for the feedback, could you give some more direction for your mature solution. I would like to be able to take advantage of the windows API but not sure where to start with this. Any suggestions on where I can start with this? thanks
– Thomas Kelly
Nov 8 at 14:51












Well... what I had in mind is: use CreateProcess to create an Excel process and get the process information, then set up a hook to get notified when windows are created. Then, when windows get created you can check if they belong to the just opened Excel process, and you can use the info from the other answer to identify the window that provides the Excel.Application class and retrieve it after creation.
– Erik von Asmuth
Nov 8 at 15:04




Well... what I had in mind is: use CreateProcess to create an Excel process and get the process information, then set up a hook to get notified when windows are created. Then, when windows get created you can check if they belong to the just opened Excel process, and you can use the info from the other answer to identify the window that provides the Excel.Application class and retrieve it after creation.
– Erik von Asmuth
Nov 8 at 15:04












1 Answer
1






active

oldest

votes

















up vote
0
down vote













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 2010Office14EXCEL.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





share|improve this answer























  • With excel 2003 being the default excel, this just opens the 2003 version.
    – Thomas Kelly
    Nov 8 at 11:34










  • Sorry, didn't get the question correctly. I tried your second variant with error trapping loop and it worked for me. What do you receive there?
    – Sergey S.
    Nov 8 at 12:32










  • No problem, thanks for the suggestion. When I try the updated solution it opens the excel 2016 as desired but then infinitely loops trying to get the object and never succeeds. If I remove the error catch it comes up with the same ActiveX error as previously. It's worth noting that even stepping through in debug mode it now brings up this error
    – Thomas Kelly
    Nov 8 at 13:45












  • Try to remove reference to Excel library.
    – Sergey S.
    Nov 8 at 14:49











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206713%2fms-access-open-2016-excel-and-getobject%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













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 2010Office14EXCEL.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





share|improve this answer























  • With excel 2003 being the default excel, this just opens the 2003 version.
    – Thomas Kelly
    Nov 8 at 11:34










  • Sorry, didn't get the question correctly. I tried your second variant with error trapping loop and it worked for me. What do you receive there?
    – Sergey S.
    Nov 8 at 12:32










  • No problem, thanks for the suggestion. When I try the updated solution it opens the excel 2016 as desired but then infinitely loops trying to get the object and never succeeds. If I remove the error catch it comes up with the same ActiveX error as previously. It's worth noting that even stepping through in debug mode it now brings up this error
    – Thomas Kelly
    Nov 8 at 13:45












  • Try to remove reference to Excel library.
    – Sergey S.
    Nov 8 at 14:49















up vote
0
down vote













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 2010Office14EXCEL.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





share|improve this answer























  • With excel 2003 being the default excel, this just opens the 2003 version.
    – Thomas Kelly
    Nov 8 at 11:34










  • Sorry, didn't get the question correctly. I tried your second variant with error trapping loop and it worked for me. What do you receive there?
    – Sergey S.
    Nov 8 at 12:32










  • No problem, thanks for the suggestion. When I try the updated solution it opens the excel 2016 as desired but then infinitely loops trying to get the object and never succeeds. If I remove the error catch it comes up with the same ActiveX error as previously. It's worth noting that even stepping through in debug mode it now brings up this error
    – Thomas Kelly
    Nov 8 at 13:45












  • Try to remove reference to Excel library.
    – Sergey S.
    Nov 8 at 14:49













up vote
0
down vote










up vote
0
down vote









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 2010Office14EXCEL.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





share|improve this answer














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 2010Office14EXCEL.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






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 8 at 12:37

























answered Nov 8 at 11:29









Sergey S.

5,5401927




5,5401927












  • With excel 2003 being the default excel, this just opens the 2003 version.
    – Thomas Kelly
    Nov 8 at 11:34










  • Sorry, didn't get the question correctly. I tried your second variant with error trapping loop and it worked for me. What do you receive there?
    – Sergey S.
    Nov 8 at 12:32










  • No problem, thanks for the suggestion. When I try the updated solution it opens the excel 2016 as desired but then infinitely loops trying to get the object and never succeeds. If I remove the error catch it comes up with the same ActiveX error as previously. It's worth noting that even stepping through in debug mode it now brings up this error
    – Thomas Kelly
    Nov 8 at 13:45












  • Try to remove reference to Excel library.
    – Sergey S.
    Nov 8 at 14:49


















  • With excel 2003 being the default excel, this just opens the 2003 version.
    – Thomas Kelly
    Nov 8 at 11:34










  • Sorry, didn't get the question correctly. I tried your second variant with error trapping loop and it worked for me. What do you receive there?
    – Sergey S.
    Nov 8 at 12:32










  • No problem, thanks for the suggestion. When I try the updated solution it opens the excel 2016 as desired but then infinitely loops trying to get the object and never succeeds. If I remove the error catch it comes up with the same ActiveX error as previously. It's worth noting that even stepping through in debug mode it now brings up this error
    – Thomas Kelly
    Nov 8 at 13:45












  • Try to remove reference to Excel library.
    – Sergey S.
    Nov 8 at 14:49
















With excel 2003 being the default excel, this just opens the 2003 version.
– Thomas Kelly
Nov 8 at 11:34




With excel 2003 being the default excel, this just opens the 2003 version.
– Thomas Kelly
Nov 8 at 11:34












Sorry, didn't get the question correctly. I tried your second variant with error trapping loop and it worked for me. What do you receive there?
– Sergey S.
Nov 8 at 12:32




Sorry, didn't get the question correctly. I tried your second variant with error trapping loop and it worked for me. What do you receive there?
– Sergey S.
Nov 8 at 12:32












No problem, thanks for the suggestion. When I try the updated solution it opens the excel 2016 as desired but then infinitely loops trying to get the object and never succeeds. If I remove the error catch it comes up with the same ActiveX error as previously. It's worth noting that even stepping through in debug mode it now brings up this error
– Thomas Kelly
Nov 8 at 13:45






No problem, thanks for the suggestion. When I try the updated solution it opens the excel 2016 as desired but then infinitely loops trying to get the object and never succeeds. If I remove the error catch it comes up with the same ActiveX error as previously. It's worth noting that even stepping through in debug mode it now brings up this error
– Thomas Kelly
Nov 8 at 13:45














Try to remove reference to Excel library.
– Sergey S.
Nov 8 at 14:49




Try to remove reference to Excel library.
– Sergey S.
Nov 8 at 14:49


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206713%2fms-access-open-2016-excel-and-getobject%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)