Avatar of Flora Edwards
Flora Edwards
Flag for Sweden asked on

VBA code gives error 91

Hi, this question is related to a code provided by Excel Tables Hero Zack.

https://www.experts-exchange.com/questions/28380445/Count-Items-on-Fluent-Ribbon-in-MS-Word-Excel-using-VBA.html?anchorAnswerId=39907831#a39907831

when i run the code in the file uploaded there, the debugger stops at line For
Each oNode In oNodeList.Item(0).ChildNodes.Item(0).ChildNodes.Item(0).ChildNodes.Item(0).ChildNodes

Open in new window

 runtime error 91 Object Variable or With Block Variable not set

the file Test.docm exists,  the XML object library in references already there. i cannot figure out what it causing this.

thanks.
VBAMicrosoft Excel

Avatar of undefined
Last Comment
Martin Liss

8/22/2022 - Mon
crystal (strive4peace) - Microsoft MVP, Access

on the thread you referenced, the last comment (DrTribos) was to add statements to skip errors and he gives an example. Perhaps there is no object there.
Martin Liss

Do you have something like this in the sub?

Dim oNode As IXMLDOMNode
Dim oNodeList As IXMLDOMNodeList
Flora Edwards

ASKER
Hi Martin,

i this is the whole UDF

Public Function CountItemsInXML(ByVal sXML As String) As Long
    Dim oNode                   As MSXML2.IXMLDOMNode
    Dim oNodeChild              As MSXML2.IXMLDOMNode
    Dim oNodeList               As MSXML2.IXMLDOMNodeList
    Dim oXMLDoc                 As MSXML2.DOMDocument
    Dim iAttribute              As Long
    Dim iCount                  As Long
    Set oXMLDoc = New MSXML2.DOMDocument
    oXMLDoc.loadXML sXML
    Set oNodeList = oXMLDoc.SelectNodes("/customUI")
    'Nodes: CustomUI/Ribbon/Tabs/Tab/Groups
    For Each oNode In oNodeList.Item(0).ChildNodes.Item(0).ChildNodes.Item(0).ChildNodes.Item(0).ChildNodes
        For Each oNodeChild In oNode.ChildNodes
            If oNodeChild.Attributes.Length > 1 Then
                iCount = iCount + 1
            End If
        Next
    Next
    CountItemsInXML = iCount
End Function

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
Martin Liss

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Flora Edwards

ASKER
thanks Martin, here it is attached the whole file.
EditOpenXML.xlsm
Martin Liss

I'm sorry but what I was asking for was the XML input file. Also the workbook you attached doesn't contain the CountItemsInXML UDF.
Flora Edwards

ASKER
sorry Martin, here is the file.

please remove .zip from it.

i  could not upload the file ext  .docm so i zipped it.
Test.docm.zip
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Martin Liss

The file you uploaded is not an xml file but rather just a one-page document with the single line "1111111".

You also didn't respond to my stating that the workbook you attached doesn't contain the CountItemsInXML UDF. What should I do about that?
Flora Edwards

ASKER
thanks Martin. i have found the problem by putting breakpoints as you suggestion.
Martin Liss

You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.

Marty - Microsoft MVP 2009 to 2016
              Experts Exchange MVE 2015
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2015
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck