Results 1 to 3 of 3
  1. #1
    New Lounger
    Join Date
    Feb 2001
    Location
    Gouda, Netherlands
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to open next file (word2000)

    I regularly need to open/edit a large number of consecutive files with different filenames. Is there a way to make a Word macro to open the next file in the directory I am working in, without specifying the file name? I searched with "open next file" both on this site and in google, but without success.
    Thanks for any suggestions, Marc

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: how to open next file (word2000)

    <P ID="edit" class=small>(Edited by jscher2000 on 28-May-06 11:56. )</P><table border=1 bordercolor=black bgcolor=yellow width=400 align=center><td><font color=black>Whoops, you might not be able to use this approach if you want to interact in a free form manner which each file. The code I posted would be for working with each document in an automated manner.</font color=black></td></table>
    It's a bit easier to work the other way around: set up your procedure to open each file in sequence, and then do stuff to them. This way, there is almost no chance of Word losing its place in the list. Unless you rename a file, in which case all bets are off. <img src=/S/wink.gif border=0 alt=wink width=15 height=15>

    The simplest and most Word 97 through 2003 friendly approach is to use the Dir command. Paste the following into a VBA module, select Dir and press F1 to read about how it works.

    <code>Sub LoopThroughFolderDemo()
    ' Starting parameters; not variable, so dim'd as constants
    Const strFolder = "c:testing"
    Const strFileSpec = "*.doc"
    ' Loop through strFolder trying to match strFileSpec
    ' First make sure you have a valid search string
    Dim strFileSearch As String, strFileFound As String
    If Right(strFolder, 1) = "" Then
    strFileSearch = strFolder & strFileSpec
    Else
    strFileSearch = strFolder & "" & strFileSpec
    End If
    ' Use Dir to locate the first file
    strFileFound = Dir(strFileSearch)
    ' strFileFound will be empty when we have exhausted the list
    Do While strFileFound <> ""
    ' Now do stuff to the file -- YOUR CODE GOES HERE

    ' Use Dir alone to locate the next file without changing the search spec
    strFileFound = Dir
    Loop
    End Sub</code>


    If you prefer, you can save the file names into an array or collection as a separate step, and then execute your other code against every file listed in the array or collection. This is useful if you want to display the list in a dialog and then work off of a list edited by the user.

    In searching around on the web (or here), you may find examples using the FileSystemObject, which is available in VBA if you add a reference to the Scripting Runtime library. The methods in the FileSystemObject expose much more information about files and may be a bit easier to use than the crusty and somewhat cryptic methods built in to VBA, but they are slower, so there's no reason to use them unless you really want that other information.

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: how to open next file (word2000)

    Here's something you could try if you save changes to the documents as you go. If you are just taking a peek, this might not work because it uses the "most recently used files" list (usually at the bottom of the File menu), and I think that list is limited to files you actually modified.

    <code>Sub OpenNextInSameFolder()
    ' This procedure checks the top file in the Most Recently Used list
    ' and iterates through the same folder trying to find the next file
    ' returned by Dir. No promises that it's reliable or proceeds in
    ' any logical order!
    With Application.RecentFiles
    If .Count = 0 Then
    MsgBox "No info on recently used files is available. Turn on " & _
    "the ''Recently used file list'' feature on the General " & _
    "tab of the Options dialog.", vbCritical + vbOKOnly
    Exit Sub
    End If
    ' Get last folder and file name
    Dim strLastFolder As String, strLastFileName As String
    strLastFolder = .Item(1).Path
    strLastFileName = .Item(1).Name
    End With
    ' Go looking for the next file, if any
    Dim strFileFound As String, blnGrabNext As Boolean, strExt As String
    ' Use Dir to locate the first file in the last-used folder
    strFileFound = Dir(strLastFolder & "*.*") 'EDIT EXTENSION as needed
    ' strFileFound will be empty when we have exhausted the list
    Do While strFileFound <> ""
    ' Is this the file we want?
    If blnGrabNext Then
    ' Check the file extension (if there is one!)
    If InStrRev(strFileFound, ".") > 0 Then
    strExt = "|" & Mid(strFileFound, InStrRev(strFileFound, ".") + 1) & "|"
    Else
    strExt = "NOEXT"
    End If
    ' If extension is Word-friendly, open the document and stop looking further
    If InStr(1, "|DOC|DOT|RTF|TXT|HTM|HTML|", strExt, vbTextCompare) > 0 Then
    Documents.Open strLastFolder & "" & strFileFound
    Exit Do
    End If
    ElseIf UCase(strFileFound) = UCase(strLastFileName) Then
    ' If name matches last opened file, grab on the next pass
    blnGrabNext = True
    End If
    ' Use Dir to locate the next file
    strFileFound = Dir
    Loop
    End Sub</code>

    Does it work for you?

Posting Permissions

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