Consulting

Results 1 to 8 of 8

Thread: Application.FileSearch

  1. #1
    VBAX Newbie
    Joined
    Sep 2014
    Posts
    3
    Location

    Application.FileSearch

    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

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Look up Dir or Application.FileDialog(msoFileDialogOpen) in VBA help, one of those should do what you want.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

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

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Sub M_snb()
      with activedocument
         c00=.name
         .close true
      end with
     
      name Options.DefaultFilePath(wdDocumentsPath) & "\" & c00 As "T:\COR\" & c00
    End Sub

  5. #5
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    For an Application.FileSearch replacement, see the discussion at:
    http://answers.microsoft.com/en-us/o...8-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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  6. #6
    VBAX Newbie
    Joined
    Sep 2014
    Posts
    3
    Location
    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.

  7. #7
    VBAX Newbie
    Joined
    Sep 2014
    Posts
    3
    Location
    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.

  8. #8
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    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

Posting Permissions

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