Overwrite Object Property with Another Object

123 Views Asked by At

I am working in VBA, and I need to set the .Interior property of a Range object, to the value of its own .DisplayFormat.Interior. More generally, I want to paste the formatted appearance of copied cells, without pasting the conditional formatting rules that contributed (alongside any manual formatting) to that appearance. Unfortunately, I can't find a way to mutate an objective property like .Interior.


More generally: let x and y be two objects, each with the objective property my_obj of the same class obj; where obj has n scalar properties {scal_1, scal_2, ..., scal_n}. I want to mutate x.my_obj such that it is identical to y.my_obj.

Obviously, this can be done "manually" by

x.my_obj.scal_1 = y.my_obj.scal_1 
x.my_obj.scal_2 = y.my_obj.scal_2
'       ⋮        ⋮         ⋮
x.my_obj.scal_n = y.my_obj.scal_n 

but this manual approach is obviously untenable for objects of arbitrary structure. Problems would especially arise for self-referential objects like Application, which have properties of their own class: Application.Application.

I had considered a recursive algorithm that iterates over an object's properties, and either performs normal assignment on scalar properties or recurses on objective properties. However, this would run forever (until crashing) on self-referential objects. To top it off, I can't even find a way to iterate over the properties of an arbitrary VBA object.


Is there ultimately any way to implement the following psuedocode in VBA? Unfortunately, this code seems to be invalid for anything but scalar assignments.

' General case.
x.my_obj = y.my_obj

' Use case.
MyRange.Interior = MyRange.DisplayFormat.Interior
0

There are 0 best solutions below