Results 1 to 3 of 3
  1. #1
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cannot find file when opening files in a directory (Word 2007)

    I grabbed some code from another post, adjusted, and tried to run the procedure below, but when I get to the " Set docTemp = " statement, I get an error:

    Run-time error '5174':

    This file could not be found.

    ----------------------------------
    <pre>Sub OpenHTMFiles()
    Dim strFolder As String, strFile As String
    Dim docTemp As Document
    strFolder = "Cirectory*"
    strFile = Dir(strFolder)
    Do While strFile <> ""

    'THIS COMMENTED TEXT WORKS (WITHOUT THE REST, OF COURSE)
    'Documents.Open FileName:="Cirectoryfile.htm", _
    ' Format:=wdOpenFormatEncodedText, _
    ' Encoding:=msoEncodingUTF8
    MsgBox strFolder
    MsgBox strFile
    Set docTemp = Documents.Open(FileName:=strFile, _
    Format:=wdOpenFormatEncodedText, _
    Encoding:=msoEncodingUTF8)
    MsgBox strFile
    MsgBox strFile

    docTemp.Close
    strFile = Dir
    Loop
    End Sub</pre>

    -----------------
    Please help me figure out what I did wrong.

    Thanks!!
    Troy

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Cannot find file when opening files in a directory (Word 2007)

    Dir returns only the file name, not the complete path. I'd do it like this:
    <code>
    Sub OpenHTMFiles()
    Dim strFolder As String, strFile As String
    Dim docTemp As Document
    ' Folder name with trailing backslash
    strFolder = "Cirectory"
    ' Preferably specify the extension to prevent the loop from picking up too much
    strFile = Dir(strFolder & "*.htm")
    Do While strFile <> ""
    ' Specify path and filename
    Set docTemp = Documents.Open(FileName:=strFolder & strFile, _
    Format:=wdOpenFormatEncodedText, _
    Encoding:=msoEncodingUTF8)
    ...
    docTemp.Close
    strFile = Dir
    Loop
    End Sub</code>

  3. #3
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Cannot find file when opening files in a directory (Word 2007)

    That did the trick!! Thanks for the quick response!!

    Troy

Posting Permissions

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