Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Seach Find txt file or close workbook in workbook_open()

After I click the excel file, I want to search a specific text file in
a specific folder. If the text file is not in that folder, I want
workbook to close. I tried below, but it did not work.

Private Sub Workbook_Open()

............

With Application.FileSearch
.NewSearch
.LookIn = Filename
.SearchSubFolders = False
.TextOrProperty = "tftougpq"
.FileType = msoFileTypeAllFiles
If .Execute 0 Then
ThisWorkbook.Close
End If
End With
......
......

End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Seach Find txt file or close workbook in workbook_open()

So you want to search the same folder for a text file with a particular name.

If that's true:

Option Explicit
Private Sub Workbook_Open()

Dim TestStr As String
Dim myFileName As String

myFileName = "yourspecifictextfilename.txt"

TestStr = ""
On Error Resume Next
TestStr = Dir(Me.Path & "\" & myFileName)
On Error GoTo 0

If TestStr = "" Then
'not found
Me.Close savechanges:=False
End If

End Sub

Remember if macros are disabled (or events are not allowed to run), then this
won't do what you want.



Mahmut wrote:

After I click the excel file, I want to search a specific text file in
a specific folder. If the text file is not in that folder, I want
workbook to close. I tried below, but it did not work.

Private Sub Workbook_Open()

...........

With Application.FileSearch
.NewSearch
.LookIn = Filename
.SearchSubFolders = False
.TextOrProperty = "tftougpq"
.FileType = msoFileTypeAllFiles
If .Execute 0 Then
ThisWorkbook.Close
End If
End With
.....
.....

End Sub


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Moved workbook-File name, minimize, maximize and close are not vis KELC-F/A Excel Worksheet Functions 2 July 1st 09 12:46 AM
Close workbook without saving file [email protected] Excel Programming 7 March 6th 08 03:20 PM
function to seach directory for file location Kevin Excel Worksheet Functions 1 August 28th 06 04:34 PM
File name seach within open filetype Andrew[_27_] Excel Programming 0 January 21st 04 06:20 PM
File|Close w/Save fires Workbook_Open twice Mike Preston Excel Programming 6 August 28th 03 10:54 PM


All times are GMT +1. The time now is 07:59 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"