Accessing a VBA object using C#
I have some C# software controlling an application. The application can be natively called via VBA. The documentation says that it is implemented as an OLE automation server.
When my code was still 32 bit I used to use Interop.MSScriptcontrol and call the VBA commands using the .ExecuteStatement()
. Here is an Example
MSScriptControl S = new MSScriptControl.ScriptControlClass();
S.Language = "VBScript";
S.ExecuteStatement("set obj = CreateObject("End_Application.Application")");
S.ExecuteStatement("set Instance = obj.NewApp");
Executing these 4 lines would create a new instantance of the application and than I would send more S.ExecuteStatements() to control the Application. If I wanted the application to return a value I would use the S.Eval
method.
Now I have to migrate to 64 bit. I found CodeFluent's ScriptEngine
class. I am using the Eval()
method but the Application is not creating a new instance.
Can you please either point me to the correct use of the ScriptEngine
class or how I can call the object directly via C# to create the instance and pass data to the instance i.e. replicate the S.ExecuteCommand()
method.
Is there a way to eliminating the scripting classes and call the object directly?
Thanks beforehand,
c# vba com ole
add a comment |
I have some C# software controlling an application. The application can be natively called via VBA. The documentation says that it is implemented as an OLE automation server.
When my code was still 32 bit I used to use Interop.MSScriptcontrol and call the VBA commands using the .ExecuteStatement()
. Here is an Example
MSScriptControl S = new MSScriptControl.ScriptControlClass();
S.Language = "VBScript";
S.ExecuteStatement("set obj = CreateObject("End_Application.Application")");
S.ExecuteStatement("set Instance = obj.NewApp");
Executing these 4 lines would create a new instantance of the application and than I would send more S.ExecuteStatements() to control the Application. If I wanted the application to return a value I would use the S.Eval
method.
Now I have to migrate to 64 bit. I found CodeFluent's ScriptEngine
class. I am using the Eval()
method but the Application is not creating a new instance.
Can you please either point me to the correct use of the ScriptEngine
class or how I can call the object directly via C# to create the instance and pass data to the instance i.e. replicate the S.ExecuteCommand()
method.
Is there a way to eliminating the scripting classes and call the object directly?
Thanks beforehand,
c# vba com ole
2
dynamic obj = Activator.CreateInstance(Type.GetTypeFromProgID("End_Application.Application", true)); dynamic Instance = obj.NewApp;
?
– GSerg
Nov 22 at 18:06
Can you create the VBA object in VBA and pass it to your C# code as a parameter?
– Mathieu Guindon
Nov 22 at 18:09
It's not clear what "VBA object" you're trying to access. As far as I can tell you don't need a "VBA object", you need to create an instance of a plain COM object, and for that to look likevar foo = new Bar();
then you need an interop assembly for the COM type library involved. This is all about COM, nothing about VBA.
– Mathieu Guindon
Nov 22 at 18:20
1
Yes, ditch the script engine, it makes it too hard to see why this code cannot work in 64-bit mode. When you do it in C# then you get a decent exception. A 64-bit app can't see the registry keys that the app's installer created.
– Hans Passant
Nov 22 at 18:32
add a comment |
I have some C# software controlling an application. The application can be natively called via VBA. The documentation says that it is implemented as an OLE automation server.
When my code was still 32 bit I used to use Interop.MSScriptcontrol and call the VBA commands using the .ExecuteStatement()
. Here is an Example
MSScriptControl S = new MSScriptControl.ScriptControlClass();
S.Language = "VBScript";
S.ExecuteStatement("set obj = CreateObject("End_Application.Application")");
S.ExecuteStatement("set Instance = obj.NewApp");
Executing these 4 lines would create a new instantance of the application and than I would send more S.ExecuteStatements() to control the Application. If I wanted the application to return a value I would use the S.Eval
method.
Now I have to migrate to 64 bit. I found CodeFluent's ScriptEngine
class. I am using the Eval()
method but the Application is not creating a new instance.
Can you please either point me to the correct use of the ScriptEngine
class or how I can call the object directly via C# to create the instance and pass data to the instance i.e. replicate the S.ExecuteCommand()
method.
Is there a way to eliminating the scripting classes and call the object directly?
Thanks beforehand,
c# vba com ole
I have some C# software controlling an application. The application can be natively called via VBA. The documentation says that it is implemented as an OLE automation server.
When my code was still 32 bit I used to use Interop.MSScriptcontrol and call the VBA commands using the .ExecuteStatement()
. Here is an Example
MSScriptControl S = new MSScriptControl.ScriptControlClass();
S.Language = "VBScript";
S.ExecuteStatement("set obj = CreateObject("End_Application.Application")");
S.ExecuteStatement("set Instance = obj.NewApp");
Executing these 4 lines would create a new instantance of the application and than I would send more S.ExecuteStatements() to control the Application. If I wanted the application to return a value I would use the S.Eval
method.
Now I have to migrate to 64 bit. I found CodeFluent's ScriptEngine
class. I am using the Eval()
method but the Application is not creating a new instance.
Can you please either point me to the correct use of the ScriptEngine
class or how I can call the object directly via C# to create the instance and pass data to the instance i.e. replicate the S.ExecuteCommand()
method.
Is there a way to eliminating the scripting classes and call the object directly?
Thanks beforehand,
c# vba com ole
c# vba com ole
edited Nov 22 at 18:20
Mathieu Guindon
40.9k762139
40.9k762139
asked Nov 22 at 17:48
jozamm
161
161
2
dynamic obj = Activator.CreateInstance(Type.GetTypeFromProgID("End_Application.Application", true)); dynamic Instance = obj.NewApp;
?
– GSerg
Nov 22 at 18:06
Can you create the VBA object in VBA and pass it to your C# code as a parameter?
– Mathieu Guindon
Nov 22 at 18:09
It's not clear what "VBA object" you're trying to access. As far as I can tell you don't need a "VBA object", you need to create an instance of a plain COM object, and for that to look likevar foo = new Bar();
then you need an interop assembly for the COM type library involved. This is all about COM, nothing about VBA.
– Mathieu Guindon
Nov 22 at 18:20
1
Yes, ditch the script engine, it makes it too hard to see why this code cannot work in 64-bit mode. When you do it in C# then you get a decent exception. A 64-bit app can't see the registry keys that the app's installer created.
– Hans Passant
Nov 22 at 18:32
add a comment |
2
dynamic obj = Activator.CreateInstance(Type.GetTypeFromProgID("End_Application.Application", true)); dynamic Instance = obj.NewApp;
?
– GSerg
Nov 22 at 18:06
Can you create the VBA object in VBA and pass it to your C# code as a parameter?
– Mathieu Guindon
Nov 22 at 18:09
It's not clear what "VBA object" you're trying to access. As far as I can tell you don't need a "VBA object", you need to create an instance of a plain COM object, and for that to look likevar foo = new Bar();
then you need an interop assembly for the COM type library involved. This is all about COM, nothing about VBA.
– Mathieu Guindon
Nov 22 at 18:20
1
Yes, ditch the script engine, it makes it too hard to see why this code cannot work in 64-bit mode. When you do it in C# then you get a decent exception. A 64-bit app can't see the registry keys that the app's installer created.
– Hans Passant
Nov 22 at 18:32
2
2
dynamic obj = Activator.CreateInstance(Type.GetTypeFromProgID("End_Application.Application", true)); dynamic Instance = obj.NewApp;
?– GSerg
Nov 22 at 18:06
dynamic obj = Activator.CreateInstance(Type.GetTypeFromProgID("End_Application.Application", true)); dynamic Instance = obj.NewApp;
?– GSerg
Nov 22 at 18:06
Can you create the VBA object in VBA and pass it to your C# code as a parameter?
– Mathieu Guindon
Nov 22 at 18:09
Can you create the VBA object in VBA and pass it to your C# code as a parameter?
– Mathieu Guindon
Nov 22 at 18:09
It's not clear what "VBA object" you're trying to access. As far as I can tell you don't need a "VBA object", you need to create an instance of a plain COM object, and for that to look like
var foo = new Bar();
then you need an interop assembly for the COM type library involved. This is all about COM, nothing about VBA.– Mathieu Guindon
Nov 22 at 18:20
It's not clear what "VBA object" you're trying to access. As far as I can tell you don't need a "VBA object", you need to create an instance of a plain COM object, and for that to look like
var foo = new Bar();
then you need an interop assembly for the COM type library involved. This is all about COM, nothing about VBA.– Mathieu Guindon
Nov 22 at 18:20
1
1
Yes, ditch the script engine, it makes it too hard to see why this code cannot work in 64-bit mode. When you do it in C# then you get a decent exception. A 64-bit app can't see the registry keys that the app's installer created.
– Hans Passant
Nov 22 at 18:32
Yes, ditch the script engine, it makes it too hard to see why this code cannot work in 64-bit mode. When you do it in C# then you get a decent exception. A 64-bit app can't see the registry keys that the app's installer created.
– Hans Passant
Nov 22 at 18:32
add a comment |
active
oldest
votes
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436075%2faccessing-a-vba-object-using-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436075%2faccessing-a-vba-object-using-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
dynamic obj = Activator.CreateInstance(Type.GetTypeFromProgID("End_Application.Application", true)); dynamic Instance = obj.NewApp;
?– GSerg
Nov 22 at 18:06
Can you create the VBA object in VBA and pass it to your C# code as a parameter?
– Mathieu Guindon
Nov 22 at 18:09
It's not clear what "VBA object" you're trying to access. As far as I can tell you don't need a "VBA object", you need to create an instance of a plain COM object, and for that to look like
var foo = new Bar();
then you need an interop assembly for the COM type library involved. This is all about COM, nothing about VBA.– Mathieu Guindon
Nov 22 at 18:20
1
Yes, ditch the script engine, it makes it too hard to see why this code cannot work in 64-bit mode. When you do it in C# then you get a decent exception. A 64-bit app can't see the registry keys that the app's installer created.
– Hans Passant
Nov 22 at 18:32