In the Microsoft Visio dev documentation, there is this helpful VBA example for using DropMany:
https://docs.microsoft.com/en-us/office/vba/api/visio.page.dropmanyI am trying to convert this to an altered VB.NET example in a VSTO Add-in, but obviously VB.NET does not allow Variants. Trying to create an Array of Objects (references) also raises an error.
I understand the first argument of the DropMany function:
ObjectsToInstance() should be a one-dimensional array of n >= 1 variants
Is a variant containing an array of variants, I guess this is the key issue why this generates an error. Marshaling this back and forth across the .net interop doesn’t just work automatically it seems.
How can I use DropMany from a VB.NET addin?
Did some performance testing and just using Drop for 2000 shapes is MUCH slower than from VBA, so DropMany can certainly help reduce calls across the .NET interop hopefully improving performance. Thank you for your help!
Public Sub DropMany_Example()
On Error GoTo HandleError
Dim vsoMasters As Visio.Masters
Dim intMasterCount As Integer
Set vsoMasters = ThisDocument.Masters
intMasterCount = vsoMasters.Count
ReDim varObjectsToInstance(1 To intMasterCount) As Variant
ReDim adblXYArray(1 To intMasterCount * 2) As Double
Dim intCounter As Integer
For intCounter = 1 To intMasterCount
'Pass name of object to drop to DropMany.
varObjectsToInstance(intCounter) = vsoMasters.Item(intCounter).Name
'Set x components of where to drop to 2,4,6,2,4,6,2,4,6,...
adblXYArray (intCounter * 2 - 1) = (((intCounter - 1) Mod 3) + 1) * 2
'Set y components to 2,2,2,4,4,4,6,6,6,...
adblXYArray (intCounter * 2) = Int((intCounter + 2) / 3) * 2
Next intCounter
Dim aintIDArray() As Integer
Dim intProcessed As Integer
intProcessed = ThisDocument.Pages(1).DropMany(varObjectsToInstance, _
adblXYArray, aintIDArray)
Debug.Print intProcessed
For intCounter = LBound(aintIDArray) To UBound(aintIDArray)
Debug.Print intCounter; aintIDArray(intCounter)
Next intCounter
Exit Sub
HandleError:
MsgBox "Error"
Exit Sub
End Sub
Should I pass a VariantWrapper object as an argument in VB.NET as a replacement for the Variant in VBA?
https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.variantwrapper?view=net-5.0