2017-03-01 125 views
0

我是一个新手,但是我使用下面的代码将整个文件夹中的.RTF文件转换为.DOCX文件。使用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

我看到2种不同的方式来做到这一点。

  1. 用查找/替换删除/删除文本。在这里,我认为不可能以任何方式重新提供该文本。消失了。

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

硬盘从印刷

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 

转换回当从印刷
这应该取消隐藏文本取消隐藏文本删除

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.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 
+0

谢谢Peh,我在哪里添加到父代码?正如我所说我是一个新手:) – Allan

+0

对不起,但StackOverflow不是一个免费的代码写作服务,也没有一个网站教你编码VBA(为此目的还有其他网站)。我只能显示可能性或技巧。如果你需要一个完整的工作代码,那么这是错误的地方。我上面的回答仅仅是一个展示案例,“如何实现”不是一个随时可用的解决方案。 –