2014-09-03 147 views
0

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

“在这里我想补充的东西

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 

回答

0

问题就出在 ”短名称“(8.3名)您的.docx文件。它仍然有一个名字以.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$()调用
+1

或枚举所有文件到一个数组中,然后循环该数组。 – ths 2014-09-03 23:54:36

+0

我不认为这是8.3名称的问题。也许只是因为我正在处理文档,这就是为什么会创建隐藏的.doc文档。无论如何,我需要在最后使用.docx,所以你能否帮助我以某种方式修改我的宏,以便将结果存储到子文件夹中(对我来说这似乎是个好主意)? – matx 2014-09-04 21:41:59

相关问题