Pass array to macro function from C#












0














I am trying to pass an array variable as a parameter to a macro function but getting the error mismatch on calling this macro file,



My code is as follows, MACRO CODE



Sub arraydef(arr() As Variant)
MsgBox (arr(0))
End Sub


C# Code for calling the Macro function by passing the array :



Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook;
xlWorkBook = xlApp.Workbooks.Open(Server.MapPath("\MacroFile\MYTEFINAL1.xlsm"));
xlApp.Visible = false;
xlApp.Run("arraydef", Countryarr);


Where Countryarr is the array that i am trying to pass



Can someone please suggest on how to pass an array to a macro file as my functionality depends on it?










share|improve this question
























  • {"Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"} getting this error while calling macro
    – Dhivakhar Venkatachalam
    Nov 21 at 8:07










  • does Run take a string as 1st argument? - just checking...
    – JohnB
    Nov 21 at 8:10






  • 1




    show code def of CountryArr
    – JohnB
    Nov 21 at 8:13






  • 1




    Please, don't put code in a comment. Move it to the question.
    – FunThomas
    Nov 21 at 8:22






  • 1




    Hi declaring Sub arraydef(arr As Variant) instead of Sub arraydef(arr() as Variant) works, here () seems to be the problem. Syntax issue.Thanks FunThomas i have been having this issue for a long time
    – Dhivakhar Venkatachalam
    Nov 21 at 9:02


















0














I am trying to pass an array variable as a parameter to a macro function but getting the error mismatch on calling this macro file,



My code is as follows, MACRO CODE



Sub arraydef(arr() As Variant)
MsgBox (arr(0))
End Sub


C# Code for calling the Macro function by passing the array :



Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook;
xlWorkBook = xlApp.Workbooks.Open(Server.MapPath("\MacroFile\MYTEFINAL1.xlsm"));
xlApp.Visible = false;
xlApp.Run("arraydef", Countryarr);


Where Countryarr is the array that i am trying to pass



Can someone please suggest on how to pass an array to a macro file as my functionality depends on it?










share|improve this question
























  • {"Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"} getting this error while calling macro
    – Dhivakhar Venkatachalam
    Nov 21 at 8:07










  • does Run take a string as 1st argument? - just checking...
    – JohnB
    Nov 21 at 8:10






  • 1




    show code def of CountryArr
    – JohnB
    Nov 21 at 8:13






  • 1




    Please, don't put code in a comment. Move it to the question.
    – FunThomas
    Nov 21 at 8:22






  • 1




    Hi declaring Sub arraydef(arr As Variant) instead of Sub arraydef(arr() as Variant) works, here () seems to be the problem. Syntax issue.Thanks FunThomas i have been having this issue for a long time
    – Dhivakhar Venkatachalam
    Nov 21 at 9:02
















0












0








0







I am trying to pass an array variable as a parameter to a macro function but getting the error mismatch on calling this macro file,



My code is as follows, MACRO CODE



Sub arraydef(arr() As Variant)
MsgBox (arr(0))
End Sub


C# Code for calling the Macro function by passing the array :



Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook;
xlWorkBook = xlApp.Workbooks.Open(Server.MapPath("\MacroFile\MYTEFINAL1.xlsm"));
xlApp.Visible = false;
xlApp.Run("arraydef", Countryarr);


Where Countryarr is the array that i am trying to pass



Can someone please suggest on how to pass an array to a macro file as my functionality depends on it?










share|improve this question















I am trying to pass an array variable as a parameter to a macro function but getting the error mismatch on calling this macro file,



My code is as follows, MACRO CODE



Sub arraydef(arr() As Variant)
MsgBox (arr(0))
End Sub


C# Code for calling the Macro function by passing the array :



Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook;
xlWorkBook = xlApp.Workbooks.Open(Server.MapPath("\MacroFile\MYTEFINAL1.xlsm"));
xlApp.Visible = false;
xlApp.Run("arraydef", Countryarr);


Where Countryarr is the array that i am trying to pass



Can someone please suggest on how to pass an array to a macro file as my functionality depends on it?







c# .net excel vba excel-vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 8:07









Pᴇʜ

20.2k42650




20.2k42650










asked Nov 21 at 8:03









Dhivakhar Venkatachalam

183




183












  • {"Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"} getting this error while calling macro
    – Dhivakhar Venkatachalam
    Nov 21 at 8:07










  • does Run take a string as 1st argument? - just checking...
    – JohnB
    Nov 21 at 8:10






  • 1




    show code def of CountryArr
    – JohnB
    Nov 21 at 8:13






  • 1




    Please, don't put code in a comment. Move it to the question.
    – FunThomas
    Nov 21 at 8:22






  • 1




    Hi declaring Sub arraydef(arr As Variant) instead of Sub arraydef(arr() as Variant) works, here () seems to be the problem. Syntax issue.Thanks FunThomas i have been having this issue for a long time
    – Dhivakhar Venkatachalam
    Nov 21 at 9:02




















  • {"Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"} getting this error while calling macro
    – Dhivakhar Venkatachalam
    Nov 21 at 8:07










  • does Run take a string as 1st argument? - just checking...
    – JohnB
    Nov 21 at 8:10






  • 1




    show code def of CountryArr
    – JohnB
    Nov 21 at 8:13






  • 1




    Please, don't put code in a comment. Move it to the question.
    – FunThomas
    Nov 21 at 8:22






  • 1




    Hi declaring Sub arraydef(arr As Variant) instead of Sub arraydef(arr() as Variant) works, here () seems to be the problem. Syntax issue.Thanks FunThomas i have been having this issue for a long time
    – Dhivakhar Venkatachalam
    Nov 21 at 9:02


















{"Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"} getting this error while calling macro
– Dhivakhar Venkatachalam
Nov 21 at 8:07




{"Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"} getting this error while calling macro
– Dhivakhar Venkatachalam
Nov 21 at 8:07












does Run take a string as 1st argument? - just checking...
– JohnB
Nov 21 at 8:10




does Run take a string as 1st argument? - just checking...
– JohnB
Nov 21 at 8:10




1




1




show code def of CountryArr
– JohnB
Nov 21 at 8:13




show code def of CountryArr
– JohnB
Nov 21 at 8:13




1




1




Please, don't put code in a comment. Move it to the question.
– FunThomas
Nov 21 at 8:22




Please, don't put code in a comment. Move it to the question.
– FunThomas
Nov 21 at 8:22




1




1




Hi declaring Sub arraydef(arr As Variant) instead of Sub arraydef(arr() as Variant) works, here () seems to be the problem. Syntax issue.Thanks FunThomas i have been having this issue for a long time
– Dhivakhar Venkatachalam
Nov 21 at 9:02






Hi declaring Sub arraydef(arr As Variant) instead of Sub arraydef(arr() as Variant) works, here () seems to be the problem. Syntax issue.Thanks FunThomas i have been having this issue for a long time
– Dhivakhar Venkatachalam
Nov 21 at 9:02














1 Answer
1






active

oldest

votes


















1














Basically, when passing an array of something to a subroutine in VBA, the easiest solution is to declare the parameter as Variant.



Declaring the parameter as array means that the calling routine must pass exactly that array type. If a subroutine is declared to receive an array of Long, you have to pass an array of Long. Not Integer, not Variant. The same is true if the subroutine is declared to receive an array of Variant - you have to pass an array of Variant.



Now, Variant is a kind of magical data type. A variant can be anything - even an array. When you declare a parameter as Variant and pass a Long (or a String, or...), the VBA Runtime Engine takes care about that a Variant is created, stores the content of the Long (or String or...) and keeps track about what it currently stores.



If you pass an array to that Variant, VBA stores that array in the Variant (probably only a reference) and keeps track not only about the fact that it contains an array but also about the type of the array.



However, if you declare the parameter as an array of Variant, the Runtime cannot do its magic. You tell the Runtime you will get a bunch of Variants, but then you pass a bunch of Integer. The Runtime would have to convert every single element of the array from Integer to Variant - and it simply does not do that. Instead, the VBA compiler complains, or, when calling via Application.Run, throws a "Type mismatch" runtime error.



There are some functions that can help you get information what is (currently) stored in a Variant: IsArray tells you if the Variant contains an error, and VarType will give you the information about the type (for more information about VarType, see https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/vartype-function). LBound and Ubound will give you the size of an array. The only thing you cannot get is the information about the dimensions of an array. In the rare case you don't know about the number of dimensions, https://stackoverflow.com/a/6902088/7599798 can give you a solution.






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407582%2fpass-array-to-macro-function-from-c-sharp%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Basically, when passing an array of something to a subroutine in VBA, the easiest solution is to declare the parameter as Variant.



    Declaring the parameter as array means that the calling routine must pass exactly that array type. If a subroutine is declared to receive an array of Long, you have to pass an array of Long. Not Integer, not Variant. The same is true if the subroutine is declared to receive an array of Variant - you have to pass an array of Variant.



    Now, Variant is a kind of magical data type. A variant can be anything - even an array. When you declare a parameter as Variant and pass a Long (or a String, or...), the VBA Runtime Engine takes care about that a Variant is created, stores the content of the Long (or String or...) and keeps track about what it currently stores.



    If you pass an array to that Variant, VBA stores that array in the Variant (probably only a reference) and keeps track not only about the fact that it contains an array but also about the type of the array.



    However, if you declare the parameter as an array of Variant, the Runtime cannot do its magic. You tell the Runtime you will get a bunch of Variants, but then you pass a bunch of Integer. The Runtime would have to convert every single element of the array from Integer to Variant - and it simply does not do that. Instead, the VBA compiler complains, or, when calling via Application.Run, throws a "Type mismatch" runtime error.



    There are some functions that can help you get information what is (currently) stored in a Variant: IsArray tells you if the Variant contains an error, and VarType will give you the information about the type (for more information about VarType, see https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/vartype-function). LBound and Ubound will give you the size of an array. The only thing you cannot get is the information about the dimensions of an array. In the rare case you don't know about the number of dimensions, https://stackoverflow.com/a/6902088/7599798 can give you a solution.






    share|improve this answer




























      1














      Basically, when passing an array of something to a subroutine in VBA, the easiest solution is to declare the parameter as Variant.



      Declaring the parameter as array means that the calling routine must pass exactly that array type. If a subroutine is declared to receive an array of Long, you have to pass an array of Long. Not Integer, not Variant. The same is true if the subroutine is declared to receive an array of Variant - you have to pass an array of Variant.



      Now, Variant is a kind of magical data type. A variant can be anything - even an array. When you declare a parameter as Variant and pass a Long (or a String, or...), the VBA Runtime Engine takes care about that a Variant is created, stores the content of the Long (or String or...) and keeps track about what it currently stores.



      If you pass an array to that Variant, VBA stores that array in the Variant (probably only a reference) and keeps track not only about the fact that it contains an array but also about the type of the array.



      However, if you declare the parameter as an array of Variant, the Runtime cannot do its magic. You tell the Runtime you will get a bunch of Variants, but then you pass a bunch of Integer. The Runtime would have to convert every single element of the array from Integer to Variant - and it simply does not do that. Instead, the VBA compiler complains, or, when calling via Application.Run, throws a "Type mismatch" runtime error.



      There are some functions that can help you get information what is (currently) stored in a Variant: IsArray tells you if the Variant contains an error, and VarType will give you the information about the type (for more information about VarType, see https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/vartype-function). LBound and Ubound will give you the size of an array. The only thing you cannot get is the information about the dimensions of an array. In the rare case you don't know about the number of dimensions, https://stackoverflow.com/a/6902088/7599798 can give you a solution.






      share|improve this answer


























        1












        1








        1






        Basically, when passing an array of something to a subroutine in VBA, the easiest solution is to declare the parameter as Variant.



        Declaring the parameter as array means that the calling routine must pass exactly that array type. If a subroutine is declared to receive an array of Long, you have to pass an array of Long. Not Integer, not Variant. The same is true if the subroutine is declared to receive an array of Variant - you have to pass an array of Variant.



        Now, Variant is a kind of magical data type. A variant can be anything - even an array. When you declare a parameter as Variant and pass a Long (or a String, or...), the VBA Runtime Engine takes care about that a Variant is created, stores the content of the Long (or String or...) and keeps track about what it currently stores.



        If you pass an array to that Variant, VBA stores that array in the Variant (probably only a reference) and keeps track not only about the fact that it contains an array but also about the type of the array.



        However, if you declare the parameter as an array of Variant, the Runtime cannot do its magic. You tell the Runtime you will get a bunch of Variants, but then you pass a bunch of Integer. The Runtime would have to convert every single element of the array from Integer to Variant - and it simply does not do that. Instead, the VBA compiler complains, or, when calling via Application.Run, throws a "Type mismatch" runtime error.



        There are some functions that can help you get information what is (currently) stored in a Variant: IsArray tells you if the Variant contains an error, and VarType will give you the information about the type (for more information about VarType, see https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/vartype-function). LBound and Ubound will give you the size of an array. The only thing you cannot get is the information about the dimensions of an array. In the rare case you don't know about the number of dimensions, https://stackoverflow.com/a/6902088/7599798 can give you a solution.






        share|improve this answer














        Basically, when passing an array of something to a subroutine in VBA, the easiest solution is to declare the parameter as Variant.



        Declaring the parameter as array means that the calling routine must pass exactly that array type. If a subroutine is declared to receive an array of Long, you have to pass an array of Long. Not Integer, not Variant. The same is true if the subroutine is declared to receive an array of Variant - you have to pass an array of Variant.



        Now, Variant is a kind of magical data type. A variant can be anything - even an array. When you declare a parameter as Variant and pass a Long (or a String, or...), the VBA Runtime Engine takes care about that a Variant is created, stores the content of the Long (or String or...) and keeps track about what it currently stores.



        If you pass an array to that Variant, VBA stores that array in the Variant (probably only a reference) and keeps track not only about the fact that it contains an array but also about the type of the array.



        However, if you declare the parameter as an array of Variant, the Runtime cannot do its magic. You tell the Runtime you will get a bunch of Variants, but then you pass a bunch of Integer. The Runtime would have to convert every single element of the array from Integer to Variant - and it simply does not do that. Instead, the VBA compiler complains, or, when calling via Application.Run, throws a "Type mismatch" runtime error.



        There are some functions that can help you get information what is (currently) stored in a Variant: IsArray tells you if the Variant contains an error, and VarType will give you the information about the type (for more information about VarType, see https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/vartype-function). LBound and Ubound will give you the size of an array. The only thing you cannot get is the information about the dimensions of an array. In the rare case you don't know about the number of dimensions, https://stackoverflow.com/a/6902088/7599798 can give you a solution.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 at 12:49

























        answered Nov 21 at 11:48









        FunThomas

        4,5311523




        4,5311523






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407582%2fpass-array-to-macro-function-from-c-sharp%23new-answer', 'question_page');
            }
            );

            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







            Popular posts from this blog

            Feedback on college project

            Albești (Vaslui)

            Futebolista