使用VBA OpenTextFile写入替换文本的代码会导致文件损坏

使用VBA OpenTextFile写入替换文本的代码会导致文件损坏,vba,excel,Vba,Excel,我希望从Excel文件中的宏打开.docx文件,查找该文件中某个字符串的所有实例,并用另一个字符串替换该字符串 我可以找到并打开该文件。我不知道字符串是否已被替换,因为下面的代码破坏了文件。无法再使用Word打开该文件 以下是宏的代码: Dim objFSO Dim objTS 'define a TextStream object Const ForReading = 1 Const ForWriting = 2 Const TristateUseDefault = -2 Dim strCo

我希望从Excel文件中的宏打开.docx文件,查找该文件中某个字符串的所有实例,并用另一个字符串替换该字符串

我可以找到并打开该文件。我不知道字符串是否已被替换,因为下面的代码破坏了文件。无法再使用Word打开该文件

以下是宏的代码:

Dim objFSO
Dim objTS 'define a TextStream object
Const ForReading = 1
Const ForWriting = 2
Const TristateUseDefault = -2

Dim strContents As String
Dim fileSpec As String

Sub SearchAndReplaceTextInFile()
    fileSpec = "C:\some\path\to\file\location\file.docx"

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTS = objFSO.OpenTextFile(fileSpec, ForReading, TristateUseDefault)

    strContents = objTS.ReadAll
    strContents = Replace(strContents, "adress", "address")

    Set objTS = objFSO.OpenTextFile(fileSpec, ForWriting, TristateUseDefault)

    objTS.Write strContents
    objTS.Close
End Sub

OpenTextFile
仅支持两种
iomode
模式,
用于读取
用于显示
。要替换文本文件中的文本,请使用
CreateTextFile
并将
覆盖设置为
True

在文本文件中查找和替换 在Word文档中查找并替换 评论
不能使用
FileSystemObject
修改Office文档。Office文档实际上是Zip文件。如果将
docx
xlsx
的文件扩展名更改为zip并打开,您将看到多个文件夹。这些文件夹中包含清单、元数据和xml文件。

OpenTextFile
仅支持两种
iomode
模式,
用于读取
用于显示
。要替换文本文件中的文本,请使用
CreateTextFile
并将
覆盖设置为
True

在文本文件中查找和替换 在Word文档中查找并替换 评论
不能使用
FileSystemObject
修改Office文档。Office文档实际上是Zip文件。如果将
docx
xlsx
的文件扩展名更改为zip并打开,您将看到多个文件夹。这些文件夹中包含清单、元数据和xml文件。

这适用于txt文件。但是有了docx文件,它们就被破坏了。很抱歉。您应该使用
CreateObject(“Word.Application”)
@mycowan打开doc文件。我希望我注意到文件扩展名。我以为你正在用word打开一个文本文件。谢谢你接受我的回答。快乐编码!!这适用于txt文件。但是有了docx文件,它们就被破坏了。很抱歉。您应该使用
CreateObject(“Word.Application”)
@mycowan打开doc文件。我希望我注意到文件扩展名。我以为你正在用word打开一个文本文件。谢谢你接受我的回答。快乐编码!!
Dim objFSO
Dim objTS                                             'define a TextStream object
Const ForReading = 1
Const ForWriting = 2
Const TristateUseDefault = -2

Dim strContents As String
Dim fileSpec As String

Sub SearchAndReplaceTextInFile()
    fileSpec = "C:\Users\best buy\Documents\EBirdRegionalRequest.txt"    '"C:\some\path\to\file\location\file.docx"

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTS = objFSO.OpenTextFile(Filename:=fileSpec, iomode:=ForReading, Format:=TristateUseDefault)

    strContents = objTS.ReadAll
    strContents = Replace(strContents, "adress", "address")

    Set objTS = objFSO.CreateTextFile(Filename:=fileSpec, overwrite:=True)

    objTS.Write strContents
    objTS.Close

    Debug.Print objFSO.OpenTextFile(Filename:=fileSpec, iomode:=ForReading, Format:=TristateUseDefault).ReadAll
End Sub
Sub SearchAndReplaceTextWordDoc(fileSpec As String, Find As String, Replace As String)
    Const wdReplaceAll As Long = 2
    Const wdFindContinue = 1
    Dim wdRange As Variant
    Dim doc As Object, wdApp As Object
    Set wdApp = CreateObject("Word.Application")
    Set doc = wdApp.Documents.Open(Filename:=fileSpec, ReadOnly:=False)

    For Each wdRange In doc.StoryRanges
        With wdRange.Find
            .Text = Find
            .Replacement.Text = Replace
            .Wrap = wdFindContinue
            .ClearFormatting
            .Replacement.ClearFormatting
            .Replacement.Highlight = False
            .Execute Replace:=wdReplaceAll
        End With
    Next
    wdApp.Visible = True

    doc.Close SaveChanges:=True
    wdApp.Quit
End Sub