我的第一篇文章,哇!在过去的 6 个月里,我一直在潜伏,因为我一直在学习 VBA,并且取得了很大的进步。但现在我被困住了,我需要你的帮助!
我有一组 xml 文档,我需要用另一个声明替换顶部的声明,但我不知道该怎么做。我已经阅读了许多关于在 VBA 中使用 DOMDocument 来修改 xml 文档的文章,并且我已经弄清楚了如何在 microsoft API 的帮助下修改 xml 文件中的不同节点。但是我似乎无法访问声明。
这可能吗?
如果是这样,我在正确的轨道上吗?
如果是这样,我需要调用哪些(如果存在)属性或方法来访问 xml 文件中的声明,以便我可以修改它们?
如果没有,我该怎么做?
我真的很想通过手动复制和粘贴来避免这样做,有成千上万的记录需要修改。
这是我的 VBA 来提取和修改字段:
Sub XMLtest()
Dim strXML As String
Dim objXML As MSXML2.DOMDocument
Dim point As IXMLDOMNode
Dim origVal As String
Dim newVal As String
Set objXML = New MSXML2.DOMDocument
strXML = "Q:\TIS\!Safety(Beth)\Tim's Projects\Safety Inspections\xml\2011-12-07T10_32_31(good tag).xml"
objXML.Load (strXML)
Set point = objXML.DocumentElement.ChildNodes.Item(0)
Debug.Print point.XML
origVal = objXML.DocumentElement.ChildNodes.Item(0).Text
MsgBox origVal
'this is a section that will change the value of the first item
objXML.DocumentElement.ChildNodes.Item(0).Text = "TimTest1"
MsgBox objXML.DocumentElement.ChildNodes.Item(0).Text
objXML.Save (strXML)
End Sub
我的目标是删除 xml 文件的声明部分并将其替换为另一组声明,以允许 infopath 再次读取它(当前声明将不会被 infopath 表单读取)。
我要删除的声明:
<?xml version="1.0" encoding="UTF-8"?>
<?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:Office-Inspection:-myXSD-2011-10-05T14-49-56" PIVersion="1.0.0.0" href="http://a.octer.com/dept/TIS/TISSafety/Office%20Inspection/Forms/template.xsn" solutionVersion="1.0.0.31" productVersion="14.0.0" ?>
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.2"?>
<my:myFields xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:tns="http://microsoft.com/webservices/SharePointPortalServer/UserProfileService" xmlns:s1="http://microsoft.com/wsdl/types/" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-10-05T14:49:56" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-us">
这是我正在尝试修改的 xml 文档:
<?xml version="1.0" encoding="UTF-8"?>
<?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:Office-Inspection:-myXSD- 2011-10-05T14-49-56" PIVersion="1.0.0.0" href="http://a.octer.com/dept/TIS/TISSafety/Office%20Inspection/Forms/template.xsn" solutionVersion="1.0.0.31" productVersion="14.0.0" ?>
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.2"?>
<my:myFields xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:tns="http://microsoft.com/webservices/SharePointPortalServer/UserProfileService" xmlns:s1="http://microsoft.com/wsdl/types/" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-10-05T14:49:56" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-us">
<my:Manager>MikeTest2</my:Manager>
<my:Date>2011-12-07</my:Date>
<my:Team>Marketing</my:Team>
<my:Inspector>HermanTest3</my:Inspector>
<my:Aisles>Y</my:Aisles>
<my:SecondaryAisles>Y</my:SecondaryAisles>
<my:CircutOverload>Y</my:CircutOverload>
<my:OutletCovers>Y</my:OutletCovers>
<my:PanelsClosed>Y</my:PanelsClosed>
<my:FireExt>Y</my:FireExt>
<my:ExtTag>Y</my:ExtTag>
<my:CeilingTiles>Y</my:CeilingTiles>
<my:Sprinklers>Y</my:Sprinklers>
<my:Evacuation>Y</my:Evacuation>
<my:a44444>Y</my:a44444>
<my:Comments>email sent to team encouraging a general cleanup of floor space in cubicles.</my:Comments>
</my:myFields>