PDA

View Full Version : [SOLVED:] Run-time error '5174' - File not found.



Jfp87
09-19-2014, 11:29 PM
Another one guys, I have tried to solve it but I'm not getting anywhere anymore. Here is some of the code I was helped out with in a previous post (Word 2010):

varFile = Dir("F:\Joe\Test Folder\Test Destination\*.docx")
Do While varFile <> ""
Set oDoc = Documents.Open(varFile)
With oDoc
.Bookmarks("Discip").Range.Text = strDiscip
.Bookmarks("ProjectNumber").Range.Text = lngProjNum
.Bookmarks("Rev").Range.Text = "A1"
.ContentControls(1).Range.Text = Me.cboClient.Value
.ContentControls(2).Range.Text = cboAsset.Value
.ContentControls(3).Range.Text = txtWpkDocNo.Value
.ContentControls(4).Range.Text = Me.cboDiscip.Value
.ContentControls(5).Range.Text = txtWpkTitle.Value
End With
Debug.Print oDoc.FullName
oDoc.Close wdSaveChanges
Set oDoc = Nothing
varFile = Dir
Loop

When I run the above code for example, I get this exact error message '5174' - File Not Found ("C:\users\U1830\Documents\PL.docx").

As you can see, the path in the error message is different to the path in my code. Not only that, but the path in the error message - as far as I can tell - doesn't exist. My understanding of paths is that if you follow it from one folder to the next, you end up at the final file\folder in the path name. I can't do that with the above path name - there is no "Documents" folder within "U1830" - the final file is correct though.

I researched the error and found there can be problems when both the filename extension is not included as part of the FileName argument, and when the filename extension is something other than ".doc" - which it is. Line 3 was also highlighted by the debugger.

Quite frustrating.

gmayor
09-20-2014, 12:08 AM
Set the path separately e.g. as follows. This assumes that strPath exists and that the bookmarks and content controls exist in the documents. If not you will have to introduce some error correction.


Sub Test()
Dim oDoc As Document
Dim strFilename As String
Const strPath As String = "F:\Joe\Test Folder\Test Destination\"
strFilename = Dir$(strPath & "*.docx")
While Len(strFilename) <> 0
Set oDoc = Documents.Open(strPath & strFilename)
With oDoc
.Bookmarks("Discip").Range.Text = strDiscip
.Bookmarks("ProjectNumber").Range.Text = lngProjNum
.Bookmarks("Rev").Range.Text = "A1"
.ContentControls(1).Range.Text = Me.cboClient.Value
.ContentControls(2).Range.Text = cboAsset.Value
.ContentControls(3).Range.Text = txtWpkDocNo.Value
.ContentControls(4).Range.Text = Me.cboDiscip.Value
.ContentControls(5).Range.Text = txtWpkTitle.Value
End With
Debug.Print oDoc.FullName
oDoc.Close wdSaveChanges
Set oDoc = Nothing
strFilename = Dir$()
Wend
End Sub

Jfp87
09-22-2014, 12:13 AM
Thanks, that's great. Appreciate it.

Joe