none
Return a string array to vba RRS feed

  • Question

  • Hello,

    I am trying to return a string array to vba, using following code. I guess I don't know how to handle the object on vba side.

    Here are my addin in c#:

        [ComVisible(true)]
        public interface IAddInUtilities
        {
            string[] ImportData();
            string OneStringOfStrings();
        }
    
        [ComVisible(true)]
        [ClassInterface(ClassInterfaceType.None)]
        public class AddInUtilities : IAddInUtilities
        {
            public string[] ImportData()
            {
                InternalCls cls = new InternalCls();
                string[] strings = cls.internalstrings();
                return strings;
            }
            public string OneStringOfStrings()
            {
                InternalCls cls = new InternalCls();
                string[] strings = cls.internalstrings();
                return strings[0];
            }
        }
        public class InternalCls
        {
            public string[] internalstrings()
            {
                return new string[] { "firstval", "secondval" };
            }
        }
    

    And here are the vba code:

    Sub mysub()
        Dim addIn As COMAddIn
        Dim automationObject As Object
        Set addIn = Application.COMAddIns("WordAddin1")
        Set automationObject = addIn.Object
        MsgBox automationObject.OneStringOfStrings
        Dim mystring(10) As String
        'Set mystring = automationObject.ImportData
        'Msgbox automationObject.ImportData(1)
    End Sub
    

    The code above runs, but the two last rows doesn't.
    The last three rows is my tries to read the string array. How do I read it?

    BR /Magnus

    Friday, May 26, 2017 9:31 AM

Answers

  • Hello,

    According to Can't assign to an array, you could handle the array like this

    Dim va As Variant
    
    va = automationObject.ImportData
    
    MsgBox va(0)
    
    MsgBox va(1)
    

    Best Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by MBLive Monday, May 29, 2017 8:03 AM
    Monday, May 29, 2017 2:22 AM
    Moderator
  • Hi,

    I found a solution that was great for me.
    In c#:

                    Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
    doc.Variables["ParamName"].Value=myvalue;

    Then in vba:

    Sub mysub()
        Dim addIn As COMAddIn
        Dim automationObject As Object
        Set addIn = Application.COMAddIns("WordAddin1")
        Set automationObject = addIn.Object
        automationObject.GetData
        Dim var As Variable
        For Each var In Variables
            MsgBox var.Name & "=" & var.Value
        Next
    End Sub
    BR /Magnus
    • Marked as answer by MBLive Monday, May 29, 2017 9:14 PM
    Monday, May 29, 2017 9:14 PM

All replies

  • Hello,

    According to Can't assign to an array, you could handle the array like this

    Dim va As Variant
    
    va = automationObject.ImportData
    
    MsgBox va(0)
    
    MsgBox va(1)
    

    Best Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by MBLive Monday, May 29, 2017 8:03 AM
    Monday, May 29, 2017 2:22 AM
    Moderator
  • Thanks alot, worked perfect.

    Unfortunately, after getting forward I realize this is not completely enough for me.
    I am trying to get a list of parameters and values, like Address, Email, Phone etc. This is dynamically collected from a database, so an object would only consists of two string variables in example ParamName and value.

    The code below works easy in C#, but I have no clue how to use this interface in VBA.

    [ComVisible(true)]
        [ClassInterface(ClassInterfaceType.None)]
        public class AddInUtilities : IAddInUtilities
        {
            public IEnumerable<MyClass> myobj()
            {
                List<MyClass> lst = new List<MyClass>();
                for (int i=1; i<=4; i++)
                { lst.Add(new MyClass("obj " + i + " string 1", "obj " + i + " string 2")); }
                return lst;
            }
            
            public string[] ImportData()
            {
                InternalCls cls = new InternalCls();
                string[] strings = cls.internalstrings();
                return strings;
            }
        }
        public class MyClass
        {
            public string mcString1, mcString2;
            public MyClass(string str1,string str2)
            {
                mcString1 = str1;
                mcString2 = str2;
            }
        }
        public class InternalCls
        {
            public string[] internalstrings()
            {
                return new string[] { "firstval", "secondval" };
            }
        }
    BR /Magnus

    Monday, May 29, 2017 10:07 AM
  • Hi,

    I found a solution that was great for me.
    In c#:

                    Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
    doc.Variables["ParamName"].Value=myvalue;

    Then in vba:

    Sub mysub()
        Dim addIn As COMAddIn
        Dim automationObject As Object
        Set addIn = Application.COMAddIns("WordAddin1")
        Set automationObject = addIn.Object
        automationObject.GetData
        Dim var As Variable
        For Each var In Variables
            MsgBox var.Name & "=" & var.Value
        Next
    End Sub
    BR /Magnus
    • Marked as answer by MBLive Monday, May 29, 2017 9:14 PM
    Monday, May 29, 2017 9:14 PM