替换文本文件中的文本
Replacing text in Text Files
我创建了一个包含不同部分的文本文件。稍后需要拉取资料
我拆分了 txt 文件的每一行以包含每个变量。
示例:
[Title]The Maps[TitleEnd]
[Name]Smith,John[NameEnd]
[Subject]Mythical Creatures[SubjectEnd]
[Text]Today i learned about this information and blah blah blah[TextEnd]
etc.
我需要打开 txt 文件,找到正确的括号,即 [Text] 到 [TextEnd],然后替换它。
示例:
[Text]Today i learned about this information and blah blah blah[TextEnd]
到
[Text]Today i learned about this information and i learned more things and blah blah blah[TextEnd]
我看了几个例子。一个让我接近但不是替换 1 行,而是将所有内容放在 1 行。
Private Sub SavePro_Click()
IRBNum = ThisDocument.IRBNetID
FilePath = "Abstract.txt"
TextFile = FreeFile
Open FilePath For Input As #1
StrFinal = "[Text]*"
While EOF(1) = False
Line Input #1, strline
StrFinal = StrFinal + ModifyText(strline)
Wend
StrFinal = Left(StrFinal, Len(StrFinal) - 2)
Close 1
Open FilePath For Output As #1
Print #1, StrFinal
Close #1
End Sub
Function ModifyText(ByVal strInput As String) As String
Dim arrString() As String
Dim strOutput As String
arrString = Split(strInput, " ")
strOutput = arrString(0) + " " + SubText.Text
ModifyText = strOutput
End Function
如果您使用 XML 格式
<xml>
<Title>The Maps</Title>
<Name>Smith,John</Name>
<Subject>Mythical Creatures</Subject>
<Text>Today i learned about this information and blah blah blah</Text>
</xml>
大部分代码已经为您编写。
Option Explicit
Sub update()
Dim xDoc, oNode
Set xDoc = CreateObject("MSXML2.DOMDocument.6.0")
xDoc.Load Path & "\Abstract.xml"
Set oNode = xDoc.SelectSingleNode("xml/Text")
MsgBox "Text: " & oNode.nodeTypedValue
oNode.nodeTypedValue = "Today i learned about this information " & vbCrLf & _
"and i learned more things and blah blah blah"
xDoc.Save Path & "\Abstract.xml"
End Sub
我创建了一个包含不同部分的文本文件。稍后需要拉取资料
我拆分了 txt 文件的每一行以包含每个变量。
示例:
[Title]The Maps[TitleEnd]
[Name]Smith,John[NameEnd]
[Subject]Mythical Creatures[SubjectEnd]
[Text]Today i learned about this information and blah blah blah[TextEnd]
etc.
我需要打开 txt 文件,找到正确的括号,即 [Text] 到 [TextEnd],然后替换它。
示例:
[Text]Today i learned about this information and blah blah blah[TextEnd]
到
[Text]Today i learned about this information and i learned more things and blah blah blah[TextEnd]
我看了几个例子。一个让我接近但不是替换 1 行,而是将所有内容放在 1 行。
Private Sub SavePro_Click()
IRBNum = ThisDocument.IRBNetID
FilePath = "Abstract.txt"
TextFile = FreeFile
Open FilePath For Input As #1
StrFinal = "[Text]*"
While EOF(1) = False
Line Input #1, strline
StrFinal = StrFinal + ModifyText(strline)
Wend
StrFinal = Left(StrFinal, Len(StrFinal) - 2)
Close 1
Open FilePath For Output As #1
Print #1, StrFinal
Close #1
End Sub
Function ModifyText(ByVal strInput As String) As String
Dim arrString() As String
Dim strOutput As String
arrString = Split(strInput, " ")
strOutput = arrString(0) + " " + SubText.Text
ModifyText = strOutput
End Function
如果您使用 XML 格式
<xml>
<Title>The Maps</Title>
<Name>Smith,John</Name>
<Subject>Mythical Creatures</Subject>
<Text>Today i learned about this information and blah blah blah</Text>
</xml>
大部分代码已经为您编写。
Option Explicit
Sub update()
Dim xDoc, oNode
Set xDoc = CreateObject("MSXML2.DOMDocument.6.0")
xDoc.Load Path & "\Abstract.xml"
Set oNode = xDoc.SelectSingleNode("xml/Text")
MsgBox "Text: " & oNode.nodeTypedValue
oNode.nodeTypedValue = "Today i learned about this information " & vbCrLf & _
"and i learned more things and blah blah blah"
xDoc.Save Path & "\Abstract.xml"
End Sub