循环遍历目录中的所有 Word 文件 [英] Loop through all Word Files in Directory

查看:42
本文介绍了循环遍历目录中的所有 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) 文件中循环这个子文件.我有以下代码:

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

Sub LoopDirectory()

vDirectory = "C:programs2	est"

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?

推荐答案

您实际上并不需要 WordtoTxtwLB 宏.您可以组合这两个代码.看这个例子

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

(未经测试)

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

    vDirectory = "C:programs2	est"

    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 会给你带扩展名的文件名.

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

这篇关于循环遍历目录中的所有 Word 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆