Solved

VBA code gives error 91

Posted on 2016-09-02
10
61 Views
Last Modified: 2016-09-09
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#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.
0
Comment
Question by:Flora
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 20
ID: 41782026
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.
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 41782149
Do you have something like this in the sub?

Dim oNode As IXMLDOMNode
Dim oNodeList As IXMLDOMNodeList
0
 
LVL 6

Author Comment

by:Flora
ID: 41783264
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

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 41783286
Put a breakpoint on line 10 and in the Immediate window type ?onodelist.count and press return. What do you get?


Can you post the XML file?
0
 
LVL 6

Author Comment

by:Flora
ID: 41789399
thanks Martin, here it is attached the whole file.
EditOpenXML.xlsm
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 41789599
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.
0
 
LVL 6

Author Comment

by:Flora
ID: 41791259
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
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 41791315
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?
0
 
LVL 6

Author Closing Comment

by:Flora
ID: 41791326
thanks Martin. i have found the problem by putting breakpoints as you suggestion.
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 41791335
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question