Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As I asked you in our mail conversation: Do you really use a 64Bit office?<br> This is very unlikely, that's why I wanted to check that upfront.</p> <p>Even if you do use a 64Bit Office, it should still work. One thing you have to remember: When you call a DLL function from VBA, the string type that is passed will be an LPStr (pointer to an Ansi Char). AnsiBStr should do it as well. But the classes you define will use the BStr, which is standard for COM.</p> <p>Here's a simplified sample, which does neither require the MSSQL client libs nor ADODB. (So less points of failure) Disclaimer: While I do have a 64Bit Windows, I only have an x86 Office (2007) installed:</p> <pre><code>[ComVisible(true), ClassInterface(ClassInterfaceType.AutoDual)] public class Sample { public string Text { [return: MarshalAs(UnmanagedType.BStr)] get; [param: MarshalAs(UnmanagedType.BStr)] set; } [return: MarshalAs(UnmanagedType.BStr)] public string TestMethod() { return Text + "..."; } } static class UnmanagedExports { [DllExport(CallingConvention = CallingConvention.StdCall)] [return: MarshalAs(UnmanagedType.IDispatch)] static Object CreateDotNetObject([MarshalAs(UnmanagedType.LPStr)] String text) { try { return new Sample { Text = text }; } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Error); return null; } } } </code></pre> <p>And this is how to use it from anything VBA-ish (e.g. Excel or Access)</p> <pre><code>Declare Function CreateDotNetObject Lib "The full path to your assembly or just the assembly if it is accessible from Excel" (ByVal text As String) As Object Sub test() Dim instance As Object Set instance = CreateDotNetObject("Test 1") Debug.Print instance.Text Debug.Print instance.TestMethod instance.text = "abc 123" ' case insensitivity in VBA works as expected Debug.Print instance.Text End Sub </code></pre> <p>If this is working for you, we can get from there to wherever you want to go. But it would be important to know what kind of office version (CPU platform) you have, and whether this simple sample works <strong>first</strong>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload