VBA(Excel):读取类内的XML文件

这是简单而基本的,但我还没有得到它。

我有几行代码从XML文件中读取节点属性。

当我在Module 1中运行代码时,它工作正常。

Sub ParseSingleTestResult(resFile As String, testRow As Integer) Set oXMLFile = CreateObject("Microsoft.XMLDOM") oXMLFile.Load (resFile) 'Find the <ResultsSummary> node and the outcome attribute: Set resultNode = oXMLFile.SelectNodes("/TestRun/ResultSummary") Set Node_Attribute = oXMLFile.SelectNodes("/TestRun/ResultSummary") Attrib = Node_Attribute(0).getAttribute("outcome") Cells(testRow, 10) = Attrib End Sub 

但是,类(正在处理工作簿超链接)中的完全相同的代码给出了这个错误:

 Run-time error '91': Object variable or With block variable not set 

下面是类模块中的相同代码 – > cHyperlinks:

 Private WithEvents pWS As Worksheet Dim resultsFile As String resultsFile = "D:\Source\Workspaces\QA\Opus3Automation\TestTests.playlist" Set oXMLFile = CreateObject("Microsoft.XMLDOM") oXMLFile.Load (resultsFile) Set resultNode = oXMLFile.SelectNodes("/TestRun/ResultSummary") Set Node_Attribute = oXMLFile.SelectNodes("/TestRun/ResultSummary") Attrib = Node_Attribute(0).getAttribute("outcome") 

给出错误的线是最后一个:

 Attrib = Node_Attribute(0).getAttribute("outcome") 

现在,我确定这个代码应该写的更好。 但我不明白为什么相同的代码在Module1 Sub内而不在Class Sub内部工作?

更新:类声明和使用:

 Public objCollection As Collection Sub CreateClasses() Dim ws As Worksheet Dim HyperlinksClass As cHyperlinks Set objCollection = New Collection For Each ws In Worksheets Set HyperlinksClass = New cHyperlinks Set HyperlinksClass.obj = ws objCollection.Add HyperlinksClass Next ws End Sub Sub Auto_Open() CreateClasses End Sub 

该类处理工作簿中的所有超链接点击。 目前,这是它处理的唯一链接:

 Set t = ActiveSheet.Range(Cells(testRow, 11), Cells(testRow, 11)) ActiveSheet.Hyperlinks.Add Anchor:=t, Address:="", TextToDisplay:="Details" 

更新2:

XML文件:

 <?xml version="1.0" encoding="UTF-8"?> <TestRun id="8ba795ec-8c5c-4095-a862-dad939f599c9" blablabla> <TestSettings name="Default Test Settings" id="a5ecdc26-0354-4011- b827-36c766b6fdb8"> <Deployment userDeploymentRoot="D:\qa\testresults\autotests \regressiontests\testtests" useDefaultDeploymentRoot="false" runDeploymentRoot="149999frho_149999FRHO3 2017-09-19 11_36_45" /> <Execution> <TestTypeSpecific /> <AgentRule name="Execution Agents"> </AgentRule> </Execution> <Properties /> </TestSettings> <Times creation="bablabla" /> <ResultSummary outcome="Aborted"> ...