使用VBA将RTF转换为DOCX

使用VBA将RTF转换为DOCX,vba,ms-word,Vba,Ms Word,我是这方面的新手,但我使用下面的代码将整个文件夹中的.RTF文件转换为.DOCX文件 Sub BatchConvertToDocx() Application.ScreenUpdating = False Dim strFolder As String, strFile As String, wdDoc As Document strFolder = GetFolder If strFolder = "" Then Exit Sub strFile = D

我是这方面的新手,但我使用下面的代码将整个文件夹中的
.RTF
文件转换为
.DOCX
文件

Sub BatchConvertToDocx()
    Application.ScreenUpdating = False
    Dim strFolder As String, strFile As String, wdDoc As Document
    strFolder = GetFolder
    If strFolder = "" Then Exit Sub
    strFile = Dir(strFolder & "\*.rtf", vbNormal)
    While strFile <> ""
      Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
      With wdDoc
        .SaveAs2 FileName:=Left(.FullName, InStrRev(.FullName, ".")) & "docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
        .Close wdDoNotSaveChanges
      End With
      strFile = Dir()
    Wend
    Set wdDoc = Nothing
    App

    lication.ScreenUpdating = True
End Sub

Function GetFolder() As String
    Dim oFolder As Object
    GetFolder = ""
    Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
    If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
    Set oFolder = Nothing
End Function

另外,在将
.docx
文件转换回
.rtf
文件时,文本应重新出现在输出
.rtf
文件中。

我看到了两种不同的方法

  • 使用“查找/替换”硬删除/删除文本。在这里,我看不出有任何可能以任何方式将该文本带回来。走了就走了

  • 通过将文本格式化为隐藏文本来隐藏打印文本

  • 硬删除

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "\[*\]"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    
    从打印中隐藏

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Font.Hidden = True
    With Selection.Find
        .Text = "\[*\]"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    
    'if this line is not used the text might be visible on the screen but not on print.
    ActiveWindow.ActivePane.View.ShowAll = False
    
    从打印中取消隐藏文本
    当转换回
    .rtf

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Font.Hidden = True
    Selection.Find.Replacement.Font.Hidden = False
    With Selection.Find
        .Text = "\[*\]"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    

    谢谢Peh,我应该在哪里将其添加到父代码中?正如我所说,我是一个新手:)对不起,但是StackOverflow不是一个免费的代码编写服务,也不是一个教你编写VBA代码的网站(为此,还有其他网站)。我只能展示可能性或技巧。如果你需要一个完整的工作代码,那么这是错误的地方。我上面的回答只是一个“如何实现”的演示案例,而不是一个现成的解决方案。
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Font.Hidden = True
    Selection.Find.Replacement.Font.Hidden = False
    With Selection.Find
        .Text = "\[*\]"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll