Rubberduck icon indicating copy to clipboard operation
Rubberduck copied to clipboard

How to Declare VBA Object Variable with Direct Memory Access?

Open johnaweiss opened this issue 2 years ago • 1 comments

Does Rubberduck provide a function to do this, or functions which can help me do this?

I want to declare a variable by directly writing the declaration directly to memory. i assume we need to write to memory:

  • Variable name and type info. Eg, "MyBook" of type Workbook.

I'm guessing the variable can be created using methods such as StrPtr, VarPtr, ObjPtr, and various API calls, like CopyMemory, PutByte, etc.

Once created, then the variable could be instantiated with normal VBA (not direct memory access), using the variable-name as normal. Eg:

Set MyBook = Workbooks(2)

Or, maybe it's necessary to declare and instantiate the object at the same time, which is also fine.

johnaweiss avatar Jul 23 '21 14:07 johnaweiss

Rubberduck is a static code analysis tool that has some code improvement features. It can change your code to be more efficient but it doesn't change how VBA works.

Your question sounds like a good candidate for a StackOverflow question. I would recommend to describe exactly what you are attempting to achieve and not just this question as it is. The beauty of VBA is that you don't need to manage memory manually. If you don't explain what you are doing in great detail you are likely to get answers that amount to "don't do that".

You can pull out the footgun and call in to Win32 libraries. Here is the documentation on memory management: https://docs.microsoft.com/en-us/windows/win32/memory/memory-management-functions?redirectedfrom=MSDN

All off those can be used from VBA but the question remains: Why?

A9G-Data-Droid avatar Jul 23 '21 15:07 A9G-Data-Droid