[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Application.FileSearch

Posted on 2016-09-30
5
Medium Priority
?
70 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
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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.
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

831 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