Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run time error 5174 I don't know why

Status
Not open for further replies.

PleaseGiveHelp

Programmer
Oct 29, 2002
131
US
Run time error 5174: This file could not be found. Try one or more of the following:
* Check the spelling of the name of the document
* Try a different name.

This error occurs when the macro finds a file that has the same 9 digit code sequence...

I don't understand why my macro is doing this. the file exists!

The first few files are:
000001_ML_strong.doc
000001_VG_hall.doc
000001_VG_nichels.doc
000001_VG_soper.doc

When it gets to the 000001_VG_nichels.doc file it bombs out. Why?
 
Ok here is the code. I realized I was getting the error because my code didn't know which directory to find the files. I am no longer getting the error. But for some reason my looping doesn't work. It doesn't get to all of the files...


Sub Merge()
'
' Merge Macro
' Macro created 10/22/2003 by ssga
'
Dim strcurrentfilename As Variant
Dim Wrd As Word.Application
Dim newcurrentfilename As String
Dim firstnine As String
Dim originalfilename As String
Dim lastfile As String
Dim files As String
Dim i As Integer
Dim currentpathname, currentfilename As String

Set Wrd = CreateObject("Word.Application")
ChDrive "F:"
ChDir "F:\HNW-IT\Team Folders\Ursula\ConsentTesting\Files\"
currentpathname = "F:\HNW-IT\Team Folders\Ursula\ConsentTesting\Files\"
currentfilename = Dir(currentpathname, vbDirectory) 'Retrieves the first entry in the folder
lastfile = ""
Do While currentfilename <> &quot;&quot;
If currentfilename <> &quot;.&quot; And currentfilename <> &quot;..&quot; Then
If (GetAttr(currentpathname & currentfilename) And vbDirectory) <> vbDirectory Then 'makes sure the object is a file
If Right(currentfilename, 3) = &quot;doc&quot; Then
'If first 9 chars of current file name ARE NOT the same as the last one
If Left(currentfilename, 9) <> lastfile Then
'save and close any open file
Word.ActiveDocument.SaveAs Left(currentfilename, 9), wdFormatDocument, False, &quot;&quot;, True, &quot;&quot;, False, _
False, False, False
Application.Documents.Close True
'open the next document
Application.Documents.Open currentpathname & currentfilename
'documents has same 9 digit prefix (lastfile),

Else
'If first 9 chars of current file name ARE the same as the last one
Selection.EndKey wdStory
Selection.InsertBreak wdSectionBreakNextPage
'Application.Documents.Open currentpathname & currentfilename
Word.ActiveDocument.SaveAs Left(currentfilename, 9), wdFormatDocument, False, &quot;&quot;, True, &quot;&quot;, False, _
False, False, False
Selection.InsertFile (currentpathname & currentfilename), &quot;&quot;, False, False, False
End If
'loop to open next document with diff 9 digit sequence
End If
End If
End If
lastfile = Left(currentfilename, 9)
currentfilename = Dir 'this gets the next object in the folder
Loop
'this closes the final document
Application.Documents.Close True

End Sub

 
I haven't tried to decipher exactly what you're trying to do, but if the files you are saving are going into the same folder as the one you are getting the directory of, then that may be your problem. The dir command may be getting confused by the emergence of new files.


Rob
[flowerface]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top