在Excel(2007)VBA中读取和更改多个XML文件 - 附加

时间:2012-08-25 20:52:40

标签: xml parsing vba multiple-files

我曾尝试使用代码(OlleSjögren先生于8月15日13:08回答)在文件夹d:\ eBobo

中的多个xml文件中用两行替换一行代码

例如,我想更改一行(在该文件夹中的所有xml文件中都相同):

AAAAAAAAAA

到行(也在所有xml文件中):

bbbbbbbbbb

CCCCCCCCCC

这是我的尝试:

Sub ReplaceStringInFile()

    Const sSearchString As String = "d:\eBobo\*.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 = "d:\eBobo\" & 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, "aaaaaaaaaa", "bbbbbbbbbb" <br /> "cccccccccc")

        iFileNum = FreeFile

        Open sFilePath For Output As iFileNum
        Print #iFileNum, sTemp

        Close iFileNum

        sFileName = Dir() 'Get the next file
    Loop
End Sub

我收到一条消息语法错误。任何帮助将不胜感激。

感谢您的回答!

1 个答案:

答案 0 :(得分:0)

尝试删除多余的引号:

sTemp = Replace(sTemp, "aaaaaaaaaa", "bbbbbbbbbb <br /> cccccccccc")

如果您确实需要xml代码中的两行,请尝试以下方法:

sTemp = Replace(sTemp, "aaaaaaaaaa", "bbbbbbbbbb" & vbCRLF & "cccccccccc")