从.doc文档中批量删除高亮显示的Word宏(并将其另存为.docx)

时间:2014-09-03 16:06:20

标签: vba ms-word word-vba

我正在搜索批量打开.doc文档的宏并将它们保存为.docx。我已经找到了一个。现在我想在所有文档中删除任何突出显示(保留文本;以及执行更多清理操作)。当我在其中添加一行(到我能猜到的最佳位置)时,它会在最后一个文档之后连续运行而不会停止。知道在哪里以及如何修改它?

Sub batch_cleaner()

Dim strFilename As String
Dim strDocName As String
Dim strPath As String
Dim oDoc As Document
Dim fDialog As FileDialog
Dim intPos As Integer
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
    .Title = "Select folder and click OK"
    .AllowMultiSelect = False
    .InitialView = msoFileDialogViewList
    If .Show <> -1 Then
        MsgBox "Cancelled By User", , "List Folder Contents"
        Exit Sub
    End If
    strPath = fDialog.SelectedItems.Item(1)
    If Right(strPath, 1) <> "\" Then strPath = strPath + "\"
End With
If Documents.Count > 0 Then
    Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
If Left(strPath, 1) = Chr(34) Then
    strPath = Mid(strPath, 2, Len(strPath) - 2)
End If
strFilename = Dir$(strPath & "*.doc")
While Len(strFilename) <> 0
    Set oDoc = Documents.Open(strPath & strFilename)

&#39;在这里我试图添加东西

    strDocName = ActiveDocument.FullName
    intPos = InStrRev(strDocName, ".")
    strDocName = Left(strDocName, intPos - 1)
    strDocName = strDocName & ".docx"
    oDoc.SaveAs FileName:=strDocName, _
        FileFormat:=wdFormatDocumentDefault
    oDoc.Close SaveChanges:=wdDoNotSaveChanges
    strFilename = Dir$()
Wend

End Sub

这段代码总是破坏它:

 Selection.WholeStory
 Selection.Range.HighlightColorIndex = wdNoHighlight

1 个答案:

答案 0 :(得分:0)

问题出在&#34;短名称&#34;您的.docx文件的(8.3名称)。它的名称仍以.doc结尾,因此它也会在dir$()来电中找到您的新文件。

这可以在命令提示符下很容易地证明(假.doc文件只是为了显示名称问题):

C:\> echo x> 1.docx
C:\> if exist *.doc echo y
y
C:\> dir *.doc
09/03/2014  06:27 PM                 3 1.docx
C:\> dir /x *.doc
09/03/2014  06:27 PM                 3 100FD~1.DOC  1.docx

所以你看到短名称,虽然隐藏在普通视图之外,实际上是一个.DOC文件,所以它匹配。同样在你的剧本中它也会匹配。

随便想到几个选项:

  • 将文件命名为与.xyz不匹配的其他内容,然后再批量重命名
  • 使用子目录存储生成的文件,以便再次在dir$()调用中匹配
相关问题