OFF2000: Endless Loop When Macro Modifies Files in a Folder (254889)
The information in this article applies to:
- Microsoft Office 2000 Premium
- Microsoft Office 2000 Professional
- Microsoft Office 2000 Standard
- Microsoft Office 2000 Developer
- Microsoft Office 2000 Small Business
- Microsoft Excel 2000
- Microsoft Outlook 2000
- Microsoft PowerPoint 2000
- Microsoft Word 2000
- Microsoft Access 2000
- Microsoft FrontPage 2000
This article was previously published under Q254889 SYMPTOMS
The following symptom occurs with Microsoft Visual Basic for Applications code that relies on the Dir function to return an empty string as the termination condition of a file-processing loop: The code runs endlessly if the files are modified while the loop is being run.
CAUSE
The Dir function is not returning a zero-length string ("") when called repeatedly against a changing list of files. The list of files in a folder will change if your macro modifies any of the files in the folder.
WORKAROUNDMicrosoft provides programming examples for illustration only, without warranty either
expressed or implied, including, but not limited to, the implied warranties of
merchantability and/or fitness for a particular purpose. This article assumes
that you are familiar with the programming language being demonstrated and the
tools used to create and debug procedures. Microsoft support professionals can
help explain the functionality of a particular procedure, but they will not
modify these examples to provide added functionality or construct procedures to
meet your specific needs. If you have limited programming experience, you may
want to contact a Microsoft Certified Partner or the Microsoft fee-based
consulting line at (800) 936-5200. For more information about Microsoft Certified
Partners, please visit the following Microsoft Web site:
For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:
You can work around this behavior by avoiding the use of a zero-length string as the termination condition of the file-processing loop. As an example of this approach, follow these steps:
- If it does not already exist, create the folder C:\Temp\Testfiles.
- In that folder, create two Word documents, Temp1.doc and Temp2.doc.
- Start Microsoft Word and press ALT+F11 to start the Visual Basic Editor.
- On the Insert menu, click Module.
- In the new module, enter the following lines of code:
Sub WorkAround()
Dim i As Integer
Dim docDoc As Document
With Application.FileSearch
.LookIn = "c:\temp\testfiles\"
.FileName = "*.doc"
.Execute
For i = 1 To .FoundFiles.Count
msgbox "File #" & i & " is " & .FoundFiles(i)
'use the file here
Set docDoc = Documents.Open(FileName:=.FoundFiles(i))
'dirty the doc and force it to be saved
docDoc.Range.InsertAfter "test"
docDoc.Close (wdSaveChanges)
Next i
End With
End Sub
- Click anywhere in the following code line
Sub WorkAround()
and then press F8 repeatedly to step through the procedure.
Notice that the procedure terminates after processing the two files.
STATUSMicrosoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article. REFERENCESFor more information about the Visual Basic for Applications Dir function, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type dir in the Office Assistant or the Answer Wizard, and then click Search to view the topic.
Modification Type: | Minor | Last Reviewed: | 9/12/2006 |
---|
Keywords: | kbbug kbfix KB254889 |
---|
|