+ Reply to Thread
Results 1 to 2 of 2

Dir stuck in (nearly) infinite loop

Hybrid View

rsc2a Dir stuck in (nearly)... 08-13-2014, 11:13 AM
Norie Re: Dir stuck in (nearly)... 08-13-2014, 11:22 AM
  1. #1
    Registered User
    Join Date
    08-07-2014
    Location
    TN
    MS-Off Ver
    2010
    Posts
    1

    Dir stuck in (nearly) infinite loop

    Hi guys -

    I have the following code that is supposed to swap out text for every file in a folder (as part of a larger bit of code), but when it is finished swapping the text, it starts back through the directory again replacing the text, and again, and again.....

    Set WordApp = CreateObject("Word.Application")
    WordApp.Visible = False
    
        strFile = Dir$(strFolder & "*")
        Do Until strFile = ""
        
        ' Check if file is open
                
    AfterClose:
     
        On Error Resume Next
            f = FreeFile()
            Open strFolder & strFile For Input Lock Read As #f
            Close #f
    
            If Err.Number <> 0 Then
                MsgBox ("Please close " & strFolder & strFile & " so the program can continue.")
                GoTo AfterClose
            End If
        
        On Error GoTo 0
        
    ' Replacing the text...
    
                Set WordDoc = WordApp.Documents.Open(strFolder & strFile)
                                            
                For Each myStoryRange In WordDoc.StoryRanges
                    With myStoryRange.Find
                        .Text = badText
                        .Replacement.Text = goodText
                        .MatchCase = True
                        .MatchWholeWord = True
                        .Wrap = wdFindContinue
                        .Execute Replace:=wdReplaceAll
                    End With
                Next myStoryRange
                
    ' Saves the file
                WordDoc.Save
                WordDoc.Close
            strFile = Dir$()
        Loop
    After cycling through 20 or so times, it eventually jumps out. I've isolated it to this bit of code, but I'm stuck. Is there something obvious I'm missing here?

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Dir stuck in (nearly) infinite loop

    Try using FSO instead of Dir to get a list of the files in the directory, then loop through that list.
    If posting code please use code tags, see here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Why is my loop infinite, and how can I fix it?
    By Mrowe in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-13-2012, 02:48 PM
  2. [SOLVED] VBA infinite loop
    By kamelkid2 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-13-2012, 12:20 PM
  3. Stuck in an infinite loop!
    By Dragon1473 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-09-2010, 03:31 PM
  4. Infinite Loop
    By phong919 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-22-2010, 04:56 PM
  5. [SOLVED] For Next Infinite Loop
    By Naji in forum Excel General
    Replies: 5
    Last Post: 01-13-2006, 03:00 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1