Solved

Application.FileSearch

Posted on 2016-09-30
5
35 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 500 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

706 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now