Loop through all Word Files in Directory

后端 未结 3 772
时光说笑
时光说笑 2021-01-01 01:08

I have the following code:

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

ActiveDocument.SaveAs2          


        
3条回答
  •  情深已故
    2021-01-01 01:47

    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
    

提交回复
热议问题