find last modified file in .file.search

M

muyBN

In the following code, I need to open the file which has been modified last
but don't quite know how to write it. Your assistance in this will be
appreciated.


strPath = "C:\Data\misc\0PackIt\"
strFirstFile = ActiveDocument.name
With Selection
With Application.FileSearch
.NewSearch
.LookIn = strPath
.SearchSubFolders = False
.FileName = "*.csv"
.Execute (msoSortByLastModified)
intFiles = .FoundFiles.count
If .Execute(msoSortOrderAscending) > 0 Then
strFile '= last modifed file
End If
End With
End With
Documents.Open (strPath & "\" & strFile), AddToRecentFiles:=True,
Format:=11
 
P

Perry

Don't know what the With Selection/End With block is doing in yr code.
Remove this for the time being.

As to the searchresults:

Expand below code example to match yr situation.
Note: the results can be found in the .FoundFiles() array.
The first member in this array will be the full filename of targetfile you
requested against the .Execute() parameters you used.

Use that one to open your file...

With Application.FileSearch
.NewSearch
.LookIn = strPath
.SearchSubFolders = False
.FileName = "*.csv"
.Execute (msoSortByLastModified, msoSortOrderAscending)
If .found then
Msgbox .FoundFiles(1)
end if
End With

--
Krgrds,
Perry

System:
Vista/Office Ultimate
VS2005/VSTO2005 SE
 
M

muyBN

Thanks, Perry. Actually, the criterion of descending worked best for me since
this put the most recent date on top.

Another question: I tried opening the file with the following code which
worked OK in another macro I wrote, but not in this one. Notice any problems
with it?

Documents.Open (strPath & "\" & strFile), AddToRecentFiles:=True, Format:=11
 
M

muyBN

One more question: When I open the .csv file in Word, it throws up an error
box with the title "Show Repairs." Within the box, it qualifies with: "Errors
were detected in this file, but Word was able to open the file by making the
repairs listed below. Save the file to make the repairs permanent." Within
the box, it has "Recovered Text Only 1." There are two button options: "Go
To" (which doesn't do anything) and "Close."

Is there any setting I can put in the code so that this box doesn't come up?

Code as presently constituted:
Documents.Open (strFile), AddToRecentFiles:=True, Format:=11
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top