2014-09-03 149 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

相關問題