PDA

View Full Version : Code is falling through and looping - why?



presence76
03-01-2006, 06:35 PM
Sorry I could not come up with a better description.

I am reading a directory and creating 2 new ones, populating the new ones with the files from the first directory. When I get to the end of the files in the directory, it reads some files that I cannot see and begins to put them in the directories, producing a loop. The files have bizzarre names like "~$04370107B8B87B7C" and the text in them looks like

"
user1user1|Nn


\???d???????????????????????????
"



Code is:


Sub checkpages()

Dim fso As FileSystemObject
Dim fldr As Folder
Dim f As File
Dim myDoc As Document
Dim totpages As Integer
Dim onepagedir As String
Dim twopagedir As String

onepagedir = "P:\Clients\Vanguard\Finance\testing\onepagedocs\"
twopagedir = "P:\Clients\Vanguard\Finance\testing\twopagedocs\"

If Dir(onepagedir) <> "" Then
Kill (onepagedir)
End If

If Dir(twopagedir) <> "" Then
Kill (twopagedir)
End If

MkDir (onepagedir)
MkDir (twopagedir)


Const TARGET_FOLDER As String = "P:\Clients\Vanguard\Finance\testing\"

Set fso = New FileSystemObject
Set fldr = fso.GetFolder(TARGET_FOLDER)
For Each f In fldr.Files
If Right(f.Name, 4) = ".doc" Then
Set myDoc = Documents.Open(TARGET_FOLDER & f.Name)
totpages = Selection.Information(wdNumberOfPagesInDocument)
If totpages = 1 Then
myDoc.SaveAs FileName:=onepagedir & f.Name
myDoc.Close False
Else
If totpages = 2 Then
myDoc.SaveAs FileName:=twopagedir & f.Name
myDoc.Close False
End If
End If
End If
Next f

End Sub


It has to do with the For/Next loop. If anyone has any ideas, it would be greatly appreciated.

Thanks.

XLGibbs
03-01-2006, 06:57 PM
do a manual search of those folders for all hidden files and see what might be there. Those annotations seem to indicate certain temporary file space allocations that might be created during file operations, and ceased early

YOu might also want to add


Kill MyDoc

to be sure the object is cleared from memory.

This might be of use as well..
http://vbaexpress.com/kb/getarticle.php?kb_id=828

presence76
03-02-2006, 06:27 AM
Just to reply - I have a temporary fix anyway.

What I did was to put a line of code at the top of the loop that checks for the bogus files. Code is now like this:


Set fso = New FileSystemObject
Set fldr = fso.GetFolder(TARGET_FOLDER)
For Each f In fldr.Files
If Left(f.Name, 1) = "~" Then
onepagedir = onepagedir
Else
If Right(f.Name, 4) = ".doc" Then
Set myDoc = Documents.Open(TARGET_FOLDER & f.Name)
totpages = Selection.Information(wdNumberOfPagesInDocument)
If totpages = 1 Then
myDoc.SaveAs FileName:=onepagedir & f.Name
myDoc.Close False
Else
If totpages = 2 Then
myDoc.SaveAs FileName:=twopagedir & f.Name
myDoc.Close False
End If
End If
End If
End If
Next f



I check for a "~" at the beginning of the filename - then I know that is a temporary file and I execute a dummy line to skip the other if's.

Not the best solution, but will have to do for today.

Thanks for the help.

fumei
03-03-2006, 05:05 PM
I would strongly agree with XLGibbs.

1. Explicitly destroy all objects from memory as you go.

2. Time to do a serious cleanup of files!!! As far as I am concerned ANY file (unless I explicitly know it to be correct) that has a tilde (~) as an initial character AND has a creation OR modification date that is not today, needs to be deleted. It is the mark of a Microsoft/Windows temporary file. In my mind, a "temporary" file should be just that. Unfortunately our current most popular operating system is not....quite...up to paying proper attention.

Of course, if you have not booted for weeks, you may want to be a bit carefully...it could (possibly) be a needed temporary file - even if the creation date is weeks old.

Booted for weeks.....what WAS I thinking??!!!! OK. OK. Sure, yes things have got better, and we do not need to boot....oh, hourly, anymore.

Anyway...clean things out. You probably got a whack of those suckers.