使用VBA将RTF转换为DOCX

时间:2017-03-01 07:53:43

标签: ms-word word-vba

我是新手,但我使用以下代码将整个文件夹中的.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文件中方括号之间的文字吗?

例如

Hi [This is the text that should be hidden] there.

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

1 个答案:

答案 0 :(得分:1)

我看到了两种不同的方法。

  1. 使用find / replace删除/删除文本。在这里,我认为不可能以任何方式恢复该文本。已经不见了。

  2. 隐藏打印文本并将其格式化为隐藏文字。

  3. 硬删除

    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