Aspose::Words::Vba::VbaReferenceCollection class

VbaReferenceCollection class

Represents a collection of VbaReference objects. To learn more, visit the Working with VBA Macros documentation article.

class VbaReferenceCollection : public System::Collections::Generic::IEnumerable<System::SharedPtr<Aspose::Words::Vba::VbaReference>>

Methods

MethodDescription
begin()
begin() const
cbegin() const
cend() const
end()
end() const
get_Count()Returns the number of VBA references in the collection.
GetType() const override
idx_get(int32_t)Gets VbaReference object at the specified index.
Is(const System::TypeInfo&) const override
Remove(const System::SharedPtr<Aspose::Words::Vba::VbaReference>&)Removes the first occurrence of a specified VbaReference item from the collection.
RemoveAt(int32_t)Removes the VbaReference element at the specified index of the collection.
static Type()
virtualizeBeginConstIterator() const override
virtualizeBeginIterator() override
virtualizeEndConstIterator() const override
virtualizeEndIterator() override

Typedefs

TypedefDescription
const_iterator
iterator
iterator_holder_type
virtualized_iterator
virtualized_iterator_element

Examples

Shows how to get/remove an element from the VBA reference collection.

void RemoveVbaReference()
{
    const String brokenPath = u"X:\\broken.dll";
    auto doc = MakeObject<Document>(MyDir + u"VBA project.docm");

    SharedPtr<VbaReferenceCollection> references = doc->get_VbaProject()->get_References();
    ASSERT_EQ(5, references->get_Count());

    for (int i = references->get_Count() - 1; i >= 0; i--)
    {
        SharedPtr<VbaReference> reference = doc->get_VbaProject()->get_References()->idx_get(i);
        String path = GetLibIdPath(reference);

        if (path == brokenPath)
        {
            references->RemoveAt(i);
        }
    }
    ASSERT_EQ(4, references->get_Count());

    references->Remove(references->idx_get(1));
    ASSERT_EQ(3, references->get_Count());

    doc->Save(ArtifactsDir + u"VbaProject.RemoveVbaReference.docm");
}

static String GetLibIdPath(SharedPtr<VbaReference> reference)
{
    switch (reference->get_Type())
    {
    case VbaReferenceType::Registered:
    case VbaReferenceType::Original:
    case VbaReferenceType::Control:
        return GetLibIdReferencePath(reference->get_LibId());

    case VbaReferenceType::Project:
        return GetLibIdProjectPath(reference->get_LibId());

    default:
        throw System::ArgumentOutOfRangeException();
    }
}

static String GetLibIdReferencePath(String libIdReference)
{
    if (libIdReference != nullptr)
    {
        ArrayPtr<String> refParts = libIdReference.Split(MakeArray<char16_t>({u'#'}));
        if (refParts->get_Length() > 3)
        {
            return refParts[3];
        }
    }

    return u"";
}

static String GetLibIdProjectPath(String libIdProject)
{
    return libIdProject != nullptr ? libIdProject.Substring(3) : u"";
}

See Also