Excel 该程序优化了使用XML文件的过程

Excel 该程序优化了使用XML文件的过程,excel,xml,vba,edit,Excel,Xml,Vba,Edit,对不起,我的英语不好:(我有一项任务-我应该用Excel VBA编写一个程序,在文件夹和子文件夹中找到所有.xml文件,扫描它们,并在必要时进行更改。然后程序将所有更改的文件保存在名为“Todays date_changed”的文件夹中,所有未更改的文件只需传输到名为“Today date”的文件夹中即可。在最后,程序应显示关于有多少文件已更改和未更改的消息。我已经编写了代码,该代码在适当的条件下更改了.xml文件。如下所示: Sub EditXML() Dim doc As New DOMDo

对不起,我的英语不好:(我有一项任务-我应该用Excel VBA编写一个程序,在文件夹和子文件夹中找到所有.xml文件,扫描它们,并在必要时进行更改。然后程序将所有更改的文件保存在名为“Todays date_changed”的文件夹中,所有未更改的文件只需传输到名为“Today date”的文件夹中即可。在最后,程序应显示关于有多少文件已更改和未更改的消息。我已经编写了代码,该代码在适当的条件下更改了.xml文件。如下所示:

Sub EditXML()
Dim doc As New DOMDocument
    Const filePath As String = "D:\Test3.xml" 'path to the editing file
    Dim isLoaded As Boolean

    isLoaded = doc.Load(filePath)

    If isLoaded Then
        Dim oAttributes As MSXML2.IXMLDOMNodeList
        Set oAttributes = doc.getElementsByTagName("Operation")
        Dim attr As MSXML2.IXMLDOMAttribute
        Dim node As MSXML2.IXMLDOMElement
        Dim tdate As String
        tdate = Format(Now(), "yyyy-mm-dd")
        For Each node In oAttributes
        If (node.getAttributeNode("Client") Is Nothing) Then
        node.setAttribute "Client", "UL"
        End If
            For Each attr In node.Attributes
                If attr.Name = "Client" Then
                 If attr.Value <> "UL" Then
                    attr.Value = "UL"
                    End If
                ElseIf attr.Name = "Date" Then
                    If attr.Value <> "tdate" Then
                    attr.Value = tdate
                End If
                End If
            Next attr
        Next node
        doc.Save filePath
    End If
End Sub
Sub EditXML()

   Dim MyFolder As String
   Dim MyFile As String
   Dim oDoc As MSXML2.DOMDocument
   Dim doc As New DOMDocument
On Error Resume Next
Application.ScreenUpdating = False
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Choose a folder"
.Show
.AllowMultiSelect = False
   If .SelectedItems.Count = 0 Then
      Exit Sub
   End If
MyFolder = .SelectedItems(1) & "\"
End With
MyFile = Dir(MyFolder & "*.xml")
Do While MyFile <> ""
   oDoc.Load (MyFolder & MyFile) 
Dim oAttributes As MSXML2.IXMLDOMNodeList
        Set oAttributes = doc.getElementsByTagName("Operation")
        Dim attr As MSXML2.IXMLDOMAttribute
        Dim node As MSXML2.IXMLDOMElement
        Dim tdate As String
        tdate = Format(Now(), "yyyy-mm-dd")
        For Each node In oAttributes
        If (node.getAttributeNode("Client") Is Nothing) Then
        node.setAttribute "Client", "UL"
        End If
            For Each attr In node.Attributes
                If attr.Name = "Client" Then
                 If attr.Value <> "UL" Then
                    attr.Value = "UL"
                    End If
                ElseIf attr.Name = "Date" Then
                    If attr.Value <> "tdate" Then
                    attr.Value = tdate
                End If
                End If
            Next attr
        Next node
        doc.Save "D:\Test\Output\*.xml"
MyFile = Dir
Loop
Application.ScreenUpdating = True
End Sub
Sub-EditXML()
Dim文档作为新文档
Const filePath作为编辑文件的String=“D:\Test3.xml”路径
Dim被加载为布尔值
isLoaded=doc.Load(文件路径)
如果已加载,则
作为MSXML2.IXMLDOMNodeList的属性
Set oAttributes=doc.getElementsByTagName(“操作”)
Dim attr作为MSXML2.IXMLDOMAttribute
作为MSXML2.IXMLDOMELENT的Dim节点
作为字符串的Dim-tdate
tdate=格式(现在为(),“yyyy-mm-dd”)
对于属性中的每个节点
如果(node.getAttributeNode(“客户端”)为Nothing),则
node.setAttribute“客户端”、“UL”
如果结束
对于node.Attributes中的每个attr
如果attr.Name=“Client”,则
如果属性值为“UL”,则
属性值=“UL”
如果结束
ElseIf attr.Name=“Date”则
如果属性值为“tdate”,则
属性值=tdate
如果结束
如果结束
下一个属性
下一节点
保存文件路径
如果结束
端接头
我还编写了一段代码,理论上应该选择所选文件夹中的所有.xml文件,对其进行编辑,然后保存到特定文件夹,但它不执行任何编译操作,执行某些操作,但不保存任何内容。这里是:

Sub EditXML()
Dim doc As New DOMDocument
    Const filePath As String = "D:\Test3.xml" 'path to the editing file
    Dim isLoaded As Boolean

    isLoaded = doc.Load(filePath)

    If isLoaded Then
        Dim oAttributes As MSXML2.IXMLDOMNodeList
        Set oAttributes = doc.getElementsByTagName("Operation")
        Dim attr As MSXML2.IXMLDOMAttribute
        Dim node As MSXML2.IXMLDOMElement
        Dim tdate As String
        tdate = Format(Now(), "yyyy-mm-dd")
        For Each node In oAttributes
        If (node.getAttributeNode("Client") Is Nothing) Then
        node.setAttribute "Client", "UL"
        End If
            For Each attr In node.Attributes
                If attr.Name = "Client" Then
                 If attr.Value <> "UL" Then
                    attr.Value = "UL"
                    End If
                ElseIf attr.Name = "Date" Then
                    If attr.Value <> "tdate" Then
                    attr.Value = tdate
                End If
                End If
            Next attr
        Next node
        doc.Save filePath
    End If
End Sub
Sub EditXML()

   Dim MyFolder As String
   Dim MyFile As String
   Dim oDoc As MSXML2.DOMDocument
   Dim doc As New DOMDocument
On Error Resume Next
Application.ScreenUpdating = False
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Choose a folder"
.Show
.AllowMultiSelect = False
   If .SelectedItems.Count = 0 Then
      Exit Sub
   End If
MyFolder = .SelectedItems(1) & "\"
End With
MyFile = Dir(MyFolder & "*.xml")
Do While MyFile <> ""
   oDoc.Load (MyFolder & MyFile) 
Dim oAttributes As MSXML2.IXMLDOMNodeList
        Set oAttributes = doc.getElementsByTagName("Operation")
        Dim attr As MSXML2.IXMLDOMAttribute
        Dim node As MSXML2.IXMLDOMElement
        Dim tdate As String
        tdate = Format(Now(), "yyyy-mm-dd")
        For Each node In oAttributes
        If (node.getAttributeNode("Client") Is Nothing) Then
        node.setAttribute "Client", "UL"
        End If
            For Each attr In node.Attributes
                If attr.Name = "Client" Then
                 If attr.Value <> "UL" Then
                    attr.Value = "UL"
                    End If
                ElseIf attr.Name = "Date" Then
                    If attr.Value <> "tdate" Then
                    attr.Value = tdate
                End If
                End If
            Next attr
        Next node
        doc.Save "D:\Test\Output\*.xml"
MyFile = Dir
Loop
Application.ScreenUpdating = True
End Sub
Sub-EditXML()
将MyFolder设置为字符串
将MyFile设置为字符串
Dim oDoc作为MSXML2.DOM文档
Dim文档作为新文档
出错时继续下一步
Application.ScreenUpdating=False
使用Application.FileDialog(msoFileDialogFolderPicker)
.Title=“选择一个文件夹”
显示
.AllowMultiSelect=False
如果.SelectedItems.Count=0,则
出口接头
如果结束
MyFolder=.SelectedItems(1)和“\”
以
MyFile=Dir(MyFolder&“*.xml”)
当我的文件“”时执行此操作
加载(MyFolder和MyFile)
作为MSXML2.IXMLDOMNodeList的属性
Set oAttributes=doc.getElementsByTagName(“操作”)
Dim attr作为MSXML2.IXMLDOMAttribute
作为MSXML2.IXMLDOMELENT的Dim节点
作为字符串的Dim-tdate
tdate=格式(现在为(),“yyyy-mm-dd”)
对于属性中的每个节点
如果(node.getAttributeNode(“客户端”)为Nothing),则
node.setAttribute“客户端”、“UL”
如果结束
对于node.Attributes中的每个attr
如果attr.Name=“Client”,则
如果属性值为“UL”,则
属性值=“UL”
如果结束
ElseIf attr.Name=“Date”则
如果属性值为“tdate”,则
属性值=tdate
如果结束
如果结束
下一个属性
下一节点
doc.保存“D:\Test\Output\*.xml”
MyFile=Dir
环
Application.ScreenUpdating=True
端接头
最后,我请求帮助编写这个程序,因为这是我第一次尝试用VBA编写一些东西。我需要一些代码,这些代码将扫描文件夹和子文件夹中的xml,按照我在这里提到的编辑它们,并将文件保存到适当的文件夹(取决于它们是否被更改),正如我在关于工作的开始和消息中所描述的。以下是我使用的xml文件示例:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Document>
    <Operations>
        <Operation Date="2018-11-06" Client="UL"/>
        <Operation Date="2018-11-06" Client="UL"/>
        <Operation Date="2018-11-06"/>
    </Operations>
</Document>


非常感谢您的帮助:)

哇。你想在这里做很多事情。让我们从两个项目开始,确保您能够正常工作,然后逐步构建其他功能。对于初学者,可以用这种方式编辑文件夹中的所有XML文件

Sub ReplaceStringInFile()

    Const sSearchString As String = "c:\your_path_here\*.xml"

    Dim sBuf As String
    Dim sTemp As String
    Dim iFileNum As Integer
    Dim sFileName As String
    Dim sFilePath As String


    sFileName = Dir(sSearchString)

    Do While sFileName <> ""

        sFilePath = "c:\temp\" & sFileName  'Get full path to file
        iFileNum = FreeFile
        sTemp = ""  'Clear sTemp

        Open sFilePath For Input As iFileNum

            Do Until EOF(iFileNum)

                Line Input #iFileNum, sBuf
                sTemp = sTemp & sBuf & vbCrLf

            Loop

        Close iFileNum

        sTemp = Replace(sTemp, "THIS", "THAT")

        iFileNum = FreeFile

        Open sFilePath For Output As iFileNum
        Print #iFileNum, sTemp

        Close iFileNum

        sFileName = Dir() 'Get the next file
    Loop
End Sub
子替换字符串填充()
Const sSearchString As String=“c:\your\u path\u here\*.xml”
作为字符串的Dim sBuf
作为字符串的Dim sTemp
作为整数的Dim iFileNum
将sFileName设置为字符串
将sFilePath设置为字符串
sFileName=Dir(sSearchString)
当sFileName“”时执行此操作
sFilePath=“c:\temp\”&sFileName“获取文件的完整路径”
iFileNum=FreeFile
sTemp=“”清除sTemp
打开sFilePath以作为iFileNum输入
直到EOF为止(iFileNum)
行输入#iFileNum,sBuf
sTemp=sTemp&sBuf&vbCrLf
环
近缘
sTemp=替换(sTemp,“这个”,“那个”)
iFileNum=FreeFile
打开sFilePath以作为iFileNum输出
打印#iFileNum,sTemp
近缘
sFileName=Dir()'获取下一个文件
环
端接头
现在,它进入一个文件夹来查找XML文件,但是您说您要遍历目录中的所有文件夹和所有子文件夹,对吧,所以您有一个递归循环遍历这个文件夹“列表”。您可以使用下面的代码来实现这一点

Sub loopAllSubFolderSelectStartDirector()

'Another Macro must call LoopAllSubFolders Macro to start to procedure
Call LoopAllSubFolders("C:\your_path_here\")

End Sub

'List all files in sub folders
Sub LoopAllSubFolders(ByVal folderPath As String)

Dim fileName As String
Dim fullFilePath As String
Dim numFolders As Long
Dim folders() As String
Dim i As Long

If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
fileName = Dir(folderPath & "*.*", vbDirectory)

While Len(fileName) <> 0

    If Left(fileName, 1) <> "." Then

        fullFilePath = folderPath & fileName

        If (GetAttr(fullFilePath) And vbDirectory) = vbDirectory Then
            ReDim Preserve folders(0 To numFolders) As String
            folders(numFolders) = fullFilePath
            numFolders = numFolders + 1
        Else
            'Insert the actions to be performed on each file
            'This example will print the full file path to the immediate window
            Debug.Print folderPath & fileName
        End If

    End If

    fileName = Dir()

Wend

For i = 0 To numFolders - 1

    LoopAllSubFolders folders(i)

Next i

End Sub
子循环所有子文件夹selectStartDirectory()
'另一个宏必须调用LoopAllSubFolders宏才能启动该过程
调用LoopAllSubFolders(“C:\your\u path\u here\”)
端接头
'列出子文件夹中的所有文件
子循环所有子文件夹(ByVal folderPath作为字符串)
将文件名设置为字符串
将fullFilePath设置为字符串
暗淡的纽姆折叠像长的一样
将文件夹()设置为字符串
我想我会坚持多久
如果正确(folderPath,1)“\”则folderPath=folderPath&“\”
fileName=Dir(folderPath&“***”,vbDirectory)
而Len(文件名)0
如果左(文件名,1)”,则
fullFilePath=folderPath&fileName
如果(GetAttr(fullFilePath)和vbDirectory)=vbDirectory,则
重拨将文件夹(0到numFolders)保留为字符串
文件夹(numFolders)=