Consulting

Results 1 to 3 of 3

Thread: Run-time error '5174' - File not found.

  1. #1
    VBAX Regular
    Joined
    Jul 2014
    Posts
    79
    Location

    Run-time error '5174' - File not found.

    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.

  2. #2
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Jul 2014
    Posts
    79
    Location
    Thanks, that's great. Appreciate it.

    Joe

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •