Word VBA >> Macro stuck in infinite Dir loop

by RGF2aWQgVHVybmVy » Tue, 31 Mar 2009 16:08:01 GMT

I've tried to cobble together some code based on snippets given by the
experts here to run one or more macros on all the files in a folder.
Unfortunately, it keeps opening and and closing each file in an infinite Dir
loop. Can anyone see what's going wrong? I've tried all sorts of things to no
avail.
Any help much appreciated.
David Turner

Sub BatchRun()

Application.ScreenUpdating = False

Dim strFileName As String
Dim strPath As String
Dim sOrgPath As String
Dim oDoc As Document
Dim fDialog As FileDialog
Dim macroList() As String
Dim macroName As String
Dim k As Long
Dim FileArray()
Dim f As Long

ReDim macroList(0)

sOrgPath = Options.DefaultFilePath(wdDocumentsPath)

Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
.InitialFileName = sOrgPath
.Title = "Select folder and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User", , "BatchRun"
Exit Sub
End If
strPath = fDialog.SelectedItems.Item(1)
If Right(strPath, 1) <> "\" Then strPath = strPath + "\"
End With

Do
macroName = InputBox("Enter the name of the macro that you want to
run.", "Macro Name", "Macro1")
If Len(macroName) = 0 Then
MsgBox ("Nothing entered. Exiting routine.")
Exit Sub
Else
macroList(UBound(macroList)) = macroName
ReDim Preserve macroList(UBound(macroList) + 1)
End If
Loop While MsgBox("Do you want to run an additional macro?", vbYesNo +
vbQuestion, _
"More macros?") = vbYes

f = 0
strFileName = Dir$(strPath & "*.doc")
Do While (Len(strFileName) > 0)
Set oDoc = Documents.Open(strPath & strFileName)
For k = 0 To UBound(macroList) - 1
Application.Run macroList(k)
Next k
oDoc.Save
oDoc.Close
Set oDoc = Nothing
f = f + 1
ReDim Preserve FileArray(1 To f)
FileArray(f) = strFileName
strFileName = Dir$()
Loop

Application.ScreenUpdating = True
MsgBox "All Done"

End Sub


Word VBA >> Macro stuck in infinite Dir loop

by Doug Robbins - Word MVP on news.microsoft.com » Tue, 31 Mar 2009 16:27:01 GMT

The usual way of using a Dir loop is

While strFileName<>""
Set oDoc = Documents.Open(strPath & strFileName)
For k = 0 To UBound(macroList) - 1
Application.Run macroList(k)
Next k
oDoc.Save
oDoc.Close
Set oDoc = Nothing
f = f + 1
ReDim Preserve FileArray(1 To f)
FileArray(f) = strFileName
strFileName = Dir$()
Loop

Of course, there is always the chance that the macros that you are running
is causing the problem.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com







Word VBA >> Macro stuck in infinite Dir loop

by David Horowitz » Wed, 01 Apr 2009 09:37:10 GMT

David,
Standard debugging tips I would recommend for you:
I always use "Option Explicit" in all my modules. (Tools > Options > Require
variable declarations)
What I would do is set a breakpoint on the Application.Run line of code by
hitting F9 (Debug > Toggle Breakpoint) then hit F5 to run the program. Then
when your breakpoint gets hit, I'd hit Shift-F8 (Debug > Step Over) and see
which macro hangs you up.
Once you discover which macro sends you off into never-never land, you can
start all over again (maybe using Run > Reset), using Shift-F8 to Step Over
all the macros leading up to the one that's the problem, and then using F8
(Step Into) to go line-by-line through the offending macro.
HTH.
Dave
--
David Horowitz
Lead Technologist
Soundside Inc.
www.soundside.biz







Word VBA >> Macro stuck in infinite Dir loop

by Karl E. Peterson » Wed, 01 Apr 2009 09:46:41 GMT




Do the "problem" macros call Dir() by any chance? That'd definitely be one of those
"foul the waters" red flag kinda things. The other thing I'll point out is that
it's considered "Best Form" to build an array of filenames in your Dir() loop, then
enter another loop to process each element of the array. This not only avoids the
previous problem with nested Dir calls, but also avoids issues related to changing
the directory content during the loop.
--
.NET: It's About Trust!
http://vfred.mvps.org




Word VBA >> Macro stuck in infinite Dir loop

by RGF2aWQgVHVybmVy » Wed, 01 Apr 2009 16:51:01 GMT






Thanks for your reply Dave. Yes, that's definitely good practice and I
should do that all the time. I'll give it a try.


It's not so much a case of a macro hanging up. The offending macro runs just
fine on its own but when run from batch macro, the whole thing goes into an
endless loop, with each file being opened, processed and closed in turn and
then beginning over again. This happens when I'm stepping through the batch
macro for testing purposes from within the VBA interface of the template
concerned. If I just attach the template and run the macro with all the files
and the template closed, the files are processed normally.

I'll give that a try. Many thanks for your tips.

Dave



Word VBA >> Macro stuck in infinite Dir loop

by RGF2aWQgVHVybmVy » Wed, 01 Apr 2009 17:06:17 GMT







Thanks for your reply Karl. No they don't.

The other thing I'll point out is that

Yes, I think you've probably hit the nail on the head. It should "know"
which files are to be processed "before" running the macro concerned, rather
than trying to run it on each file from within the Dir loop and then going
back to look for the next file to be processed and getting confused over
which is the active document no doubt.
Thanks for the tip. Much appreciated.

BR,
Dave



Similar Threads

1. Help on .NET Word Object Library Range Find.Execute infinite Loop - CSharp/C#

2. Infinite loop

Hi. In every one of my office programs, when I hit the help icon (or from the 
menu), the program tries to access the internet, but goes into an infinte 
loop. the only way out is to reboot. Happens in Word, Excel, publisher, etc. 
Any advice?

3. Infinite loop in Groove 3.1 - Microsoft Office

4. Infinite loop when using track changes

I have been unable to find this problem with Word 2003 documented anywhere.

I have written a VBA macro to iterate thorugh the complete set of tracked 
changes in a Revisions collection of a document.  There are 405 changes in 
this document.  At some point while stepping through the collection via a 
For..Next loop, a deleted Revision object is encountered.  Then the previous 
2 revisions plus the "deleted" revision are repeated (apparently) infinitely 
in the Revisions collection.  The indexes of the 3 repeated changes iterate 
(63, 64, 65) even though they apparenlty occupy successive positions in the 
collection.  Obviously, pointers are going awry somewhere within the 
Revisions collection.

If you step through the tracked changes using the Word toolbar, the same 
thing happens.  When you get to the area where the "deleted" Revision/change 
object is located, Word appears to hang at the same change forever (the 2 
changes prior to the deleted object are in the same table cell).

Anybody have ideas or can anybody point me to potential bug fixes?

5. Infinite Loop Problem - Word Document management

6. infinite loop in winword.exe

Some months ago, I started getting into an endless loop while
simply typing in text in a word 97 document. I did ctrl-alt-del
and winword.exe was shown as taking up 100% of the CPU. (I'm
using Windows XP.)

I stop the process, restart WORD, reload the document, etc., and
it works for awhile, and then happens again.

I've reloaded winword.exe from my cd and that doesn't seem to
fix the problem.

Looking for others who have had this problem, I ran across a
message on some board that indicated it might be due to a
problem with add-ins. So I deleted all my add-ins, of which
there were two. The problem seemed to go away.

This week it came back.

I need help!!  Thanks for any you can give me.

Ray Stone
 XXXX@XXXXX.COM 

7. StrReverse() function - infinite loop - MS Word&VBA

8. replace header text: get section data in header to halt infinite loop VBA

VBA macro, which replaces header text when recorded, does
NOT replace header text when called.

I then forced macro into header and it replaces fine, but 
the ActiveWindow.ActivePane.View.NextHeaderFooter goes into
an infinite loop.  

Tried getting section number using old WordBasic command
(SelInfo(2)), as well as page number (SelInfo(3))
NewPg = WordBasic.SelInfo(3)
then compairing "NewPg" with prior page variable "OldPg".

Variables don't seem to fill.

Any suggestions appreciated.  THANK YOU.