PDA

View Full Version : Application.FileSearch



bullet
09-09-2014, 11:58 PM
I hope that someone can help me.

I have a code with use Application.FileSearch that works fine in WORD2003, but my company is going to use WORD2010 and while I was testing this code doesn't work any more because of the Application.Filesearch.

I read on internet that you must use something else then Application.Filesearch. But I don't know how to chance my code. Can someone please help me.


Sub opslaan()
Dim strDir As String
Dim strFileName As String
Dim FSO As New Scripting.FileSystemObject

strDir = Options.DefaultFilePath(wdDocumentsPath) 'Bestandslocatie documenten
strFileName = ActiveDocument.Name

ActiveDocument.Save
ChangeFileOpenDirectory "t:\cor\"

With Application.FileSearch
.LookIn = CurDir
.FileName = strFileName
If .Execute > 0 Then
With Dialogs(wdDialogFileSaveAs)
.Name = "t:\cor\" & strFileName
.Show
End With
Else
ActiveDocument.SaveAs strFileName
End If
End With

ActiveDocument.Close

ChangeFileOpenDirectory strDir

With Application.FileSearch
.LookIn = CurDir
.FileName = strFileName
If .Execute > 0 Then
FSO.DeleteFile strFileName, True
Else
End If
End With

Dialogs(wdDialogFileOpen).Show

End Sub

Bob Phillips
09-10-2014, 12:53 AM
Look up Dir or Application.FileDialog(msoFileDialogOpen) in VBA help, one of those should do what you want.

gmayor
09-10-2014, 03:21 AM
The macro appears to save the document at its current location, then save it with the same name in the folder "t:\cor\" using the save dialog then close and delete the file from the default document folder if it is there? Is that a fair summary? If so the following should do that without all that user interference.



Sub Macro1()
Dim strName As String
Dim strPath As String
Dim strAsk As String
Const strNewPath As String = "t:\cor\"
If Documents.Count = 0 Then Exit Sub 'no document so quit
ActiveDocument.Save 'Save any changes
strName = ActiveDocument.name 'Note the name
strPath = ActiveDocument.Path & Chr(92) 'and the original path
ActiveDocument.Close 0 'close the document
If FileExists(strNewPath & strName) Then 'check if the file exists at the target location
'and if it does confirm you wish to replace it
strAsk = MsgBox("The file:" & vbCr & strNewPath & strName & vbCr _
& "exists. Do you wish to replace it?", vbYesNo)
If strAsk = vbYes Then 'Confirmed
Kill strNewPath & strName 'delete the target file
Name strPath & strName As strNewPath & strName ' and replace it with the new version
Documents.Open strNewPath & strName 'Open the new version from its new location
Else
Documents.Open strPath & strName 'Re-open the document from its original location
End If
Else 'The file doesn't exist in the target folder
'so move the file to the target folder
Name strPath & strName As strNewPath & strName
'and open it.
Documents.Open strNewPath & strName
End If
End Sub

Public Function FileExists(ByVal Filename As String) As Boolean
Dim lngAttr As Long
On Error GoTo NoFile
lngAttr = GetAttr(Filename)
If (lngAttr And vbDirectory) <> vbDirectory Then
FileExists = True
End If
NoFile:
Exit Function
End Function

snb
09-10-2014, 03:59 AM
Sub M_snb()
with activedocument
c00=.name
.close true
end with

name Options.DefaultFilePath(wdDocumentsPath) & "\" & c00 As "T:\COR\" & c00
End Sub

macropod
09-10-2014, 09:56 AM
For an Application.FileSearch replacement, see the discussion at:
http://answers.microsoft.com/en-us/office/forum/office_2007-customize/dir-vs-filesearch/9021a162-7ec5-4c16-bff8-84f28300dba4

Basically:
Download the class file from: http://dl.dropbox.com/u/35239054/FileSearch.cls
Open the VBA editor
Import the file (press CTRL-M)
Ensure a reference to the Microsoft Office Object Library in the VBE's Tools|References.
Create a variable of type FileSearch and use it the same way as you might have used Application.FileSearch in any office version before 2007.

bullet
09-11-2014, 02:50 AM
gmayer
That is correct, it safes the file at the current location then saves it in the folder "t:\cor\", than delete it in the first location. And opens the dialogsscreen documents open in the first location.

bullet
09-11-2014, 02:56 AM
SNB
Your code is very simple and works.

But If the same name already exist in the target folder, I would like to have the SaveAs screen from WORD, so that we can give it a different name.

snb
09-13-2014, 03:19 AM
Sub M_snb()
With activedocument
c00=.name
.close True
End With

If Dir("T:\COR\" & c00)="" Then
name Options.DefaultFilePath(wdDocumentsPath) & "\" & c00 As "T:\COR\" & c00
else
With Application.FileDialog(2)
.InitialFileName = "T:\COR\"
If .Show = 0 Then Exit Sub
.Execute
End With
end if
End Sub