遍历目录中的所有Word文件

时间:2012-07-17 16:11:16

标签: vba ms-word

我有以下代码:

Sub WordtoTxtwLB()
'
' WordtoTxtwLB Macro
'
'
Dim fileName As String
myFileName = ActiveDocument.Name

ActiveDocument.SaveAs2 fileName:= _
"\\FILE\" & myFileName & ".txt", FileFormat:= _
wdFormatText, LockComments:=False, Password:="", AddToRecentFiles:=True, _
WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
False, Encoding:=1252, InsertLineBreaks:=True, AllowSubstitutions:=False, _
LineEnding:=wdCRLF, CompatibilityMode:=0


End Sub

我想通过目录中的所有单词(.doc)文件循环此子。我有以下代码:

Sub LoopDirectory()

vDirectory = "C:\programs2\test"

vFile = Dir(vDirectory & "\" & "*.*")

Do While vFile <> ""

Documents.Open fileName:=vDirectory & "\" & vFile

ActiveDocument.WordtoTxtwLB

vFile = Dir
Loop

End Sub

但它不起作用。如何通过更改当前代码或使用新代码来使其工作?

3 个答案:

答案 0 :(得分:11)

您实际上并不需要WordtoTxtwLB宏。您可以将两个代码组合在一起。看这个例子

(的 UNTESTED

Sub LoopDirectory()
    Dim vDirectory As String
    Dim oDoc As Document

    vDirectory = "C:\programs2\test\"

    vFile = Dir(vDirectory & "*.*")

    Do While vFile <> ""
        Set oDoc = Documents.Open(fileName:=vDirectory & vFile)

        ActiveDocument.SaveAs2 fileName:="\\FILE\" & oDoc.Name & ".txt", _
                               FileFormat:=wdFormatText, _
                               LockComments:=False, _
                               Password:="", _
                               AddToRecentFiles:=True, _
                               WritePassword:="", _
                               ReadOnlyRecommended:=False, _
                               EmbedTrueTypeFonts:=False, _
                               SaveNativePictureFormat:=False, _
                               SaveFormsData:=False, _
                               SaveAsAOCELetter:=False, _
                               Encoding:=1252, _
                               InsertLineBreaks:=True, _
                               AllowSubstitutions:=False, _
                               LineEnding:=wdCRLF, _
                               CompatibilityMode:=0

        oDoc.Close SaveChanges:=False
        vFile = Dir
    Loop
End Sub
顺便说一下,您确定要使用*.*通配符吗?如果文件夹中有Autocad文件怎么办?此外,ActiveDocument.Name会为您提供扩展名的文件名。

答案 1 :(得分:2)

要编辑目录中的所有单词文档,我构建了这个简单的子程序。

subRoutine循环遍历目录和 打开它找到的每个* .doc文件。然后在它调用的打开文档文件上 第二个子例程。在第二个subRoutine完成文档后 保存然后关闭。

Sub DoVBRoutineNow()
Dim file
Dim path As String


path = "C:\Documents and Settings\userName\My Documents\myWorkFolder\"

file = Dir(path & "*.doc")
Do While file <> ""
Documents.Open FileName:=path & file

Call secondSubRoutine

ActiveDocument.Save
ActiveDocument.Close

file = Dir()
Loop
End Sub

~~~~~~

答案 2 :(得分:0)

这是我的解决方案。我认为对于像我这样的新手来说,这很容易理解和直截了当,我会在这里发布我的代码。因为我四处搜寻,所以看到的代码有点复杂。走吧。

Sub loopDocxs()
Dim wApp As Word.Application 
Dim wDoc As Word.Document 
Dim mySource As Object
Set obj = CreateObject("Scripting.FileSystemObject")
Set mySource = obj.GetFolder("D:\docxs\")

For Each file In mySource.Files 'loop through the directory
  If Len(file.Name) > 0 And InStr(1, file.Name, "$") = 0 Then '$ is temp file mask

    Set wApp = CreateObject("Word.Application")
    wApp.Visible = True
    'Word.Application doesn't recognize file here event if it's a word file.
    'fortunately we have the file name which we can use.
    Set wDoc = wApp.Documents.Open(mySource & "\" & file.Name, , ReadOnly)

    'Do your things here which will be a lot of code

    wApp.Quit
    Set wApp = Nothing


  End If
Next file