Log in

View Full Version : I need help in a bad way



thebiped
01-13-2017, 02:31 PM
So the place I work for is using Word 2010 but alot of the code they use was office 2003 code. I am a network engineer so I can script routers and do VLSM subnets all day long but coding software and VBA code is out of my wheelhouse. I understand a little C++ that I took in college but I've never really messed with this kind of code before. I am not really understanding how to use the DIR in this code and ANY help would be greatly appreciated from the gurus here. I realize that there are other threads with this but I didn't know how you guys felt about raising old threads form the dead.

***EDIT*** I guess i should state the problem is with the Application.FileSearch portion of this code. It's no longer supported by 2010...not that you all didn't know that but I'm doing my best to be as helpful as possible to the folks helping me.



Sub MergeSetup()
'
' MergeSetup Macro
' This macro sets up the header source containt the field
' list and the data source file containg the source fields.
'
' Note this macro should be executed when the "Forms Gen" document
' is created. This sets the mail merege properties for the document and
' allows for the painting of the forms document.
'
' History
' 03/02/98 Rob Berringer Initial Creation
' 07/10/98 Rob Berringer Added Addtional Error Trapping.
'
' Check for required files.
Set FileSrch = Application.FileSearch

With FileSrch
.LookIn = "C:\windows"
.SearchSubFolders = False
.FileName = "tokens.tkn"
.MatchTextExactly = True
If .Execute = 0 Then
MsgBox "Tokens File Not Found, Required for Merge Setup", Title:="Court View 2000"
GoTo cleanExit ' Get out
End If

.FileName = "crth6005.dat"
If .Execute = 0 Then
MsgBox "Data File Not Found, Required for Merge Setup", Title:="Court View 2000"
GoTo cleanExit ' Get out
End If

End With

MsgString = "Setup Merge Files For The Document. This function should " & _
"only be peformed at the initial setup of the Document"

If MsgBox(MsgString, vbOKCancel, Title:="Court View 2000") = vbOK Then
' Set up the merge document as a Form Letter
On Error GoTo errorCatch
ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
' Set Header from Token file.
ActiveDocument.MailMerge.OpenHeaderSource Name:="C:\WINDOWS\TOKENS.TKN", _
ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
wdOpenFormatAuto
' Set Source file from Dat File.
ActiveDocument.MailMerge.OpenDataSource Name:="C:\WINDOWS\CRTH6005.DAT", _
ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
Format:=wdOpenFormatAuto, Connection:="", SQLStatement:="", SQLStatement1 _
:=""
' OK Start Building the Form (Start User Edit)
ActiveDocument.MailMerge.EditMainDocument
' Complete Message
StrMessage = "Merge File Setup is Complete. " & _
"Build Forms Generation Document"
MsgBox StrMessage, Title:="Court View 2000"
End If

' Exit, Jump out
GoTo cleanExit


errorCatch:
MsgString = "Setup Aborted, Merge File Setup is Not Complete:"
MsgBox MsgString, Title:="Court View 2000"

cleanExit:


End Sub

macropod
01-13-2017, 03:19 PM
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
You can download the class module from: http://dl.dropbox.com/u/35239054/FileSearch.cls

gmaxey
01-13-2017, 10:09 PM
As an alternative, you could just pass your required files (full path) to function that returns a Boolean True\False result:


Function fcnFileOrDirExists(PathName As String) As Boolean
'Macro Purpose: Function returns TRUE if the specified file or folder exists, false if not.
'PathName: Supports Windows mapped drives
'File usage: Provide full file path and extension
'Folder usage : Provide full folder path
Dim iTemp As Integer
'Ignore errors to allow for error evaluation
On Error Resume Next
iTemp = GetAttr(PathName)
'Check if error exists and set response appropriately
Select Case Err.Number
Case Is = 0: fcnFileOrDirExists = True
Case Else: fcnFileOrDirExists = False
End Select
'Resume error checking
On Error GoTo 0
lbl_Exit:
Exit Function
End Function