我一直在为自己的业务构建一个小机器人,将多个XML文件立即上传到Excel中以执行该机器人的其余部分至关重要。我已经能够使用循环成功上载多个XML,因为所有XML的格式都不相同,所以只有将所需的信息上载到Excel中是必要的。我查找了不同的来源,但看起来我做错了。到目前为止,我上载多个XML的相关VBA代码是
Application.ScreenUpdating = False
Application.DisplayAlerts = False
With Sheets("Working Notes")
lastrow = .Cells(.Rows.Count, "A").End(xlUp).row
For i = 1 To lastrow
strTargetFile = .Cells(i, "A")
Set Wb = Workbooks.OpenXML(FileName:=strTargetFile, LoadOption:=xlXmlLoadImportToList)
'wb.Sheets(1).UsedRange.Copy ThisWorkbook.Sheets("Working Notes").Cells(Rows.Count, "B").End(xlUp).Offset(1, 0)
Wb.Sheets(1).UsedRange.Copy ThisWorkbook.Sheets("Working Notes").Range("B" & i)
Wb.Close False
Next i
End With
我正在查看的XML页面是
<?xml version="1.0" encoding="utf-8"?>
<abcd_omn_gatca:abcd_omn xmlns:abcd="urn:lu:etat:acd:abcd_omn:v2.0" xmlns:ftc="urn:lu:etat:acd:gatca:v2.0" xmlns:sfa="urn:oecd:ties:stfgatcatypes:v2" xmlns:abcd_omn_gatca="urn:lu:etat:acd:abcd_gatca:v2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="urn:lu:etat:acd:abcd_gatca:v2.0 abcd_gatca_V2.0.xsd">
<abcd_omn_gatca:abcd_gatca>
<abcd_omn_gatca:abcd_RefId>XXXXX</abcd_omn_gatca:abcd_RefId>
<abcd_omn_gatca:abcd_Depositor>
<abcd:NameDepositor>XXXXX</abcd:NameDepositor>
<abcd:PersonalIdentificationNumberDepositor>XXXXX</abcd:PersonalIdentificationNumberDepositor>
<abcd:AddressDepositor>
<abcd:StreetPhysical>XXXXX</abcd:StreetPhysical>
<abcd:NumberPhysical>XXXXX</abcd:NumberPhysical>
<abcd:PostalCodePhysical>XXXXX</abcd:PostalCodePhysical>
<abcd:CityPhysical>XXXXX</abcd:CityPhysical>
<abcd:CountryPhysical>XXXXX</abcd:CountryPhysical>
</abcd:AddressDepositor>
<abcd:PersonDepositor>
<abcd:Name>XXXXX</abcd:Name>
<abcd:FirstName>XXXXX</abcd:FirstName>
<abcd:EmailPersonal>XXXXX</abcd:EmailPersonal>
<abcd:TelephoneDirect>XXXXX</abcd:TelephoneDirect>
</abcd:PersonDepositor>
</abcd_omn_gatca:abcd_Depositor>
<abcd_omn_gatca:abcd_Declarer>
<abcd:NameDeclarer>XXXXX</abcd:NameDeclarer>
<abcd:PersonalIdentificationNumberDeclarer>XXXXX</abcd:PersonalIdentificationNumberDeclarer>
<abcd:AddressDeclarer>
<abcd:StreetPhysical>XXXXX</abcd:StreetPhysical>
<abcd:NumberPhysical>XXXXX</abcd:NumberPhysical>
<abcd:PostalCodePhysical>XXXXX</abcd:PostalCodePhysical>
<abcd:CityPhysical>XXXXX</abcd:CityPhysical>
<abcd:CountryPhysical>XXXXX</abcd:CountryPhysical>
</abcd:AddressDeclarer>
<abcd:PersonDeclarer>
<abcd:Name>XXXXX</abcd:Name>
<abcd:FirstName>XXXXX</abcd:FirstName>
<abcd:EmailPersonal>XXXXX</abcd:EmailPersonal>
<abcd:EmailOrganisation>XXXXX</abcd:EmailOrganisation>
<abcd:TelephoneDirect>XXXXX</abcd:TelephoneDirect>
</abcd:PersonDeclarer>
</abcd_omn_gatca:abcd_Declarer>
<abcd_omn_gatca:abcd_ReportingPerson>
***<abcd:NameReportingPerson>RENIN III S.A R.L.</abcd:NameReportingPerson***>
<abcd:IdentificationNumber>XXXXX</abcd:IdentificationNumber>
<abcd:IdentificationNumberExtension>XXXXX</abcd:IdentificationNumberExtension>
<***abcd:AddressReportingPerson>
<abcd:StreetPhysical>Avenue M.F. Brady</abcd:StreetPhysical>
<abcd:NumberPhysical>45</abcd:NumberPhysical>
<abcd:PostalCodePhysical>1234</abcd:PostalCodePhysical>
<abcd:CityPhysical>Somethingburg</abcd:CityPhysical>
<abcd:CountryPhysical>SB</abcd:CountryPhysical>
</abcd:AddressReportingPerson>***
<abcd:PersonContactReportingPerson>
<abcd:Name>XXXXX</abcd:Name>
<abcd:FirstName>XXXXX</abcd:FirstName>
<abcd:EmailPersonal>XXXXX</abcd:EmailPersonal>
<abcd:EmailOrganisation>XXXXX</abcd:EmailOrganisation>
<abcd:TelephoneDirect>XXXXX</abcd:TelephoneDirect>
</abcd:PersonContactReportingPerson>
</abcd_omn_gatca:abcd_ReportingPerson>
<abcd_omn_gatca:RFI_Identifier>XXXXX</abcd_omn_gatca:RFI_Identifier>
<abcd_omn_gatca:ReportContent>
<abcd_omn_gatca:ReportingPeriod>XXXXX</abcd_omn_gatca:ReportingPeriod>
<abcd_omn_gatca:ZeroReporting>XXXXX</abcd_omn_gatca:ZeroReporting>
</abcd_omn_gatca:ReportContent>
</abcd_omn_gatca:abcd_gatca>
</abcd_omn_gatca:abcd_omn>
我唯一需要的信息是报告人的姓名“ RENIN III S.A R.L.”。以及Excel工作表上的报告人地址。原谅我对VBA的不成熟知识。我将非常有帮助。非常感谢!
答案 0 :(得分:0)
在删除"***"
之后,我从文件(test.xml
)加载了XML。前提是您包含对所需名称空间的引用,您可以按以下方式进行访问:
Option Explicit
Public Sub testing()
Dim xmlDoc As New MSXML2.DOMDocument60
xmlDoc.validateOnParse = True
xmlDoc.setProperty "SelectionNamespaces", "xmlns:abcd=""urn:lu:etat:acd:abcd_omn:v2.0"""
If xmlDoc.Load("C:\Users\User\Desktop\test.xml") Then
Dim node As Object
Debug.Print xmlDoc.SelectNodes("//abcd:AddressReportingPerson").Length
Debug.Print xmlDoc.SelectNodes("//abcd:NameReportingPerson").Length
For Each node In xmlDoc.SelectNodes("//abcd:AddressReportingPerson")
Debug.Print node.Text
Next node
For Each node In xmlDoc.SelectNodes("//abcd:NameReportingPerson")
Debug.Print node.Text
Next node
Else
Debug.Print "Houston, we have a problem!"
End If
End Sub