vba 循环遍历目录中的所有 Word 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11526577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 13:34:17  来源:igfitidea点击:

Loop through all Word Files in Directory

vbams-word

提问by user1440061

I have the following code:

我有以下代码:

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

I want to loop this sub through all of the word (.doc) files in a directory. I have the following code:

我想在目录中的所有单词 (.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

But it is not working. How do I get this to work either by altering the current code or using new code?

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

回答by Siddharth Rout

You don't actually need the WordtoTxtwLB Macro. You can combine both the codes. see this example

您实际上并不需要 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

BTW, are you sure you want to use the *.*wildcard? What if there are Autocad files in the folder? Also ActiveDocument.Namewill give you the file name with the Extension.

顺便说一句,您确定要使用*.*通配符吗?如果文件夹中有 Autocad 文件怎么办?同时ActiveDocument.Name会给你的扩展名的文件名。

回答by mycowan

To edit all the word documents in a directory I built this simple subroutine.

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

The subRoutine loops through the directory and opens each *.doc file it finds. Then on the open document file it calls the second subRoutine. After the second subRoutine is finished the document is saved and then closed.

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

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

~~~~~~

~~~~~~

回答by William Tong

Here's my solution. I think it's easy to understand and straight forward for newbies like me that I will post my code here. Because I searched around and the codes I saw were kind of complicated. Let's go.

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

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