?
Solved

Application.FileSearch

Posted on 2016-09-30
5
Medium Priority
?
60 Views
Last Modified: 2016-09-30
I also have the same question presented on Experts Exchange 2-11-13 at:  https://www.experts-exchange.com/questions/28027899/Replace-Application-Filesearch.html.  

I am extremely new to VBA and our database uses the old Application.FileSearch.  Our database works in Access 2003, but we are being required now to use it in Access 2013.  The following code is supposed go out to a central directory and get an existing word document.  Since this was written in Access 2013, the code does not work in the Access 2013. I tried to follow LVL92 solution, but I do not know how to add it to my particular code.  I was told our code is way overwritten.  The code which needs updating is as follows:


 Public Function GetWordComplaint(strComplaintDoc, Response, GotIt, DocSource)
DoCmd.Hourglass True
'pubComplaintDocFolder is a constant located in the GenMods module
'This Function gets an already-existing document, if it is there and if the user wants to use it
'check to see if there is already a document by this name in the Complaint Document Folder
'if not, exit this function
'If so, tell them it exists and find out if they want to replace it with a new one?
'If they don't want to replace it, exit this function; otherwise, make a new document
'If they want to replace it with a new one, open a brand new WordDocument for them according to name criteria
Dim FolderSource As String
    If DocSource = "cmp" Then
        FolderSource = pubComplaintDocFolder
    Else
        FolderSource = pubInspDocFolder
    End If
With Application.FileSearch
    .NewSearch
    .LookIn = FolderSource
    .SearchSubFolders = False
    .FileName = strComplaintDoc
    .MatchTextExactly = True
    .FileType = msoFileTypeWordDocuments
    
    If .Execute() = 0 Then
        DoCmd.Hourglass False
        If Response = "edit" Then
            GotIt = 1
            MsgBox ("Complaint document " & strComplaintDoc & " does not exist in Word.  If you need to create one, go to the 'Merge' icon.")
        End If
        Exit Function
    Else
        'KeyCode = 0
        'ME: 7/11/03 - add code so it knows which control got you in here: new merge or edit
        If Response <> "edit" Then
            Response = MsgBox("The Document " & strComplaintDoc & " has previously been merged-Do You Want To MERGE Anyway?", vbYesNo)
            'ME: 7/11/03 - take out the following line and exit regardless of response
            '        we don't need to bring up the existing document from the "Create" button (consistency)
            'If response = vbYes Then GoTo Exit_Here
            GoTo Exit_Here
        End If
    End If
End With
strComplaintDoc = pubComplaintDocFolder & strComplaintDoc
Dim wApp As New Word.Application
Dim wDoc As Word.Document
' make new Word data file
On Error GoTo dMergeError
' open word merge document
Set wDoc = wApp.Documents.Open(strComplaintDoc)
wApp.Visible = True
wApp.WindowState = wdWindowStateMaximize
Exit_Here:
DoCmd.Hourglass False
Exit Function
dMergeError:
Select Case Err.Number
    Case 5174
        MsgBox ("Word doc named " & strComplaintDoc & " cannot be located.")
    Case Else
        MsgBox ("error # " & Err.Number & Err.Description)
End Select
Resume Exit_Here
End Function 

Open in new window



The general module which is referenced above presents the following additional related code:

Option Compare Database
Option Explicit
'following are various global fields for system
Public GoodUpdate As Integer
Public HoldBookMark As String
Public glbProjID As Long
Public glbFacChemID As Integer
Public HoldRunCnt As Long
Public pubCurrDBPath As String
Public LastPage As Integer
Public PageTop As Integer
Public CloseOverride As Integer

Public OK2CloseAsbFac01 As Integer
Public OK2CloseFacDetail01 As Integer
Public OK2CloseFacDetail02 As Integer


'Public Const pubComplaintDocFolder As String = "H:\users\wpdocs\airqual\air_compliance\Complaint\"

Open in new window

0
Comment
Question by:Tammy Allen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 7

Expert Comment

by:COACHMAN99
ID: 41823785
there are a few functions that should provide what you need:
if Dir("path and filename") <> "" then file exists
Kill("path and filename") will delete the old one
filecopy source, destination will write new one to destination
0
 

Author Comment

by:Tammy Allen
ID: 41823815
Does replace lines 16 - 22 in the code or does this leave hangin g commands?

if Dir("path and filename") <> "" then file exists

Open in new window

0
 
LVL 7

Expert Comment

by:COACHMAN99
ID: 41823828
I would think so. It looks as if the file type is irrelevant in this section.
The comments seem to simplify the objective

I will try to quickly code the function. stand by

Also, there is some use of the word 'merge' which doesn't make sense, and doesn't align with the comments at the head of the code?
0
 
LVL 7

Accepted Solution

by:
COACHMAN99 earned 2000 total points
ID: 41823869
Although un-tested, the following should work
There are 'hanging' decisions e.g. if '.Execute() = 0' and response isn't 'edit and vice-versa (may not be important)
There isn't any 'Kill' code so I'm assuming Word handles the 'replace' situation?
I assumed the path parameter to the function didn't have a "\" at the end?


Public Function GetWordComplaint(strComplaintDoc, Response, GotIt, DocSource)
DoCmd.Hourglass True
'pubComplaintDocFolder is a constant located in the GenMods module
'This Function gets an already-existing document, if it is there and if the user wants to use it
'check to see if there is already a document by this name in the Complaint Document Folder
'if not, exit this function
'If so, tell them it exists and find out if they want to replace it with a new one?
'If they don't want to replace it, exit this function; otherwise, make a new document
'If they want to replace it with a new one, open a brand new WordDocument for them according to name criteria
Dim FolderSource As String
  If DocSource = "cmp" Then
      FolderSource = pubComplaintDocFolder
  Else
      FolderSource = pubInspDocFolder
  End If
  If Dir(FolderSource & "\" & strComplaintDoc) = "" Then
    DoCmd.Hourglass False
    If Response = "edit" Then
        GotIt = 1
        MsgBox ("Complaint document " & strComplaintDoc & " does not exist in Word.  If you need to create one, go to the 'Merge' icon.")
    End If
    Exit Function
  Else
    'KeyCode = 0
    'ME: 7/11/03 - add code so it knows which control got you in here: new merge or edit
    If Response <> "edit" Then
        Response = MsgBox("The Document " & strComplaintDoc & " has previously been merged-Do You Want To MERGE Anyway?", vbYesNo)
        'ME: 7/11/03 - take out the following line and exit regardless of response
        '        we don't need to bring up the existing document from the "Create" button (consistency)
        'If response = vbYes Then GoTo Exit_Here
        GoTo Exit_Here
    End If
  End If


  strComplaintDoc = pubComplaintDocFolder & strComplaintDoc
  Dim wApp As New Word.Application
  Dim wDoc As Word.Document
  ' make new Word data file
  On Error GoTo dMergeError
  ' open word merge document
  Set wDoc = wApp.Documents.Open(strComplaintDoc)
  wApp.Visible = True
  wApp.WindowState = wdWindowStateMaximize
Exit_Here:
DoCmd.Hourglass False
Exit Function
dMergeError:
Select Case Err.Number
    Case 5174
        MsgBox ("Word doc named " & strComplaintDoc & " cannot be located.")
    Case Else
        MsgBox ("error # " & Err.Number & Err.Description)
End Select
Resume Exit_Here
End Function
0
 

Author Closing Comment

by:Tammy Allen
ID: 41823955
You were correct! No kill function.  the "Edit" button pulls up the existing word document.  Your code updates the Access 2003 code to Access 2013.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question