FileSearch

J

jaysfive

How would I change this to search for files begining with a set letter

With Application.FileSearch
..FileName = "*.doc"
..LookIn = "G:\a_a_e"
..MatchTextExactly = True
..SearchSubFolders = False
.Execute
 
J

Jean-Guy Marcil

jaysfive was telling us:
jaysfive nous racontait que :
How would I change this to search for files begining with a set letter

With Application.FileSearch
.FileName = "*.doc"
.LookIn = "G:\a_a_e"
.MatchTextExactly = True
.SearchSubFolders = False
.Execute

.FileName = "*.doc"
should be
.FileName = "S*.doc"
if the set letter is "S".


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
A

Andi Mayer

No, this finds all files with an "S" anywhere in the filename

You are sure????? *S* finds all S, but S* finds only the beginning, at
least on my Computers

If you expect an answer to a personal mail, add the word "manfred" to the first 10 lines in the message
MW
 
H

Helmut Weber

Hi,
yes, you are right,
here and now, according to my specifications,
First point, filesearch returns the fullname,
like "c:\test\test.mydoc.doc"

Second point, it returns fullnames,
that contain an "S" or "s", unexpectedly,
and I don't know about case sensitivity here,
it is a mystery.

So, try this one:

Sub Makro2()
Dim l As Long
Dim s As String
With Application.FileSearch
.FileName = "S*.doc"
.LookIn = "c:\"
.SearchSubFolders = True
.Execute
For l = 1 To .FoundFiles.Count
s = StrReverse(.FoundFiles(l))
s = Left(s, InStr(s, "\"))
s = Left(s, Len(s) - 1)
s = StrReverse(s)
If Left(s, 1) = "s" Or Left(s, 1) = "S" Then
Selection.TypeText s & vbCr
End If
Next
End With
End Sub

If you need help for putting the result into an array,
ask again.

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
J

jaysfive

Andi said:
You are sure????? *S* finds all S, but S* finds only the beginning, at
least on my Computers

If you expect an answer to a personal mail, add the word "manfred" to
the first 10 lines in the message

Hope you don't object Manfred but here is the full macro:-
Sub ChangeTemplateToNormal()

Dim killFile As String
Dim myCopy As Document
Dim docName As String
Dim oForm As Object
Dim i As Integer
Dim varFile As Variant
With Application.FileSearch
..NewSearch
..FileName = "d*.doc"
..LookIn = "G:\a_a_e"
..SearchSubFolders = False
..Execute

For Each varFile In .FoundFiles
With Documents.Open(FileName:=varFile,
ConfirmConversions:=False)
On Error GoTo z
..UpdateStylesOnOpen = False
On Error GoTo z
..AttachedTemplate = "normal"
On Error GoTo z
..Save
..Close savechanges:=wdSaveChanges
Application.StatusBar = varFile
Close
End With
u:

Next varFile
End With
GoTo x
'Application.StatusBar = varFile
z:

'+++++++++++++++++++++++++++++++++++++++++++++++++++++
MsgBox "Bad document,click OK, see next message box for document
name,move it somewhere else and run macro again"
MsgBox varFile
Application.StatusBar = varFile
x:

Call macro_finished
Application.Quit savechanges:=wdPromptToSaveChanges
End Sub

This finds "abdner.doc
 
J

jaysfive

Andi said:
You are sure????? *S* finds all S, but S* finds only the beginning, at
least on my Computers

If you expect an answer to a personal mail, add the word "manfred" to
the first 10 lines in the message

Hope you don't object Manfred but here is the full macro:-
Sub ChangeTemplateToNormal()

Dim killFile As String
Dim myCopy As Document
Dim docName As String
Dim oForm As Object
Dim i As Integer
Dim varFile As Variant
With Application.FileSearch
..NewSearch
..FileName = "d*.doc"
..LookIn = "G:\a_a_e"
..SearchSubFolders = False
..Execute

For Each varFile In .FoundFiles
With Documents.Open(FileName:=varFile,
ConfirmConversions:=False)
On Error GoTo z
..UpdateStylesOnOpen = False
On Error GoTo z
..AttachedTemplate = "normal"
On Error GoTo z
..Save
..Close savechanges:=wdSaveChanges
Application.StatusBar = varFile
Close
End With
u:

Next varFile
End With
GoTo x
'Application.StatusBar = varFile
z:

'+++++++++++++++++++++++++++++++++++++++++++++++++++++
MsgBox "Bad document,click OK, see next message box for document
name,move it somewhere else and run macro again"
MsgBox varFile
Application.StatusBar = varFile
x:

Call macro_finished
Application.Quit savechanges:=wdPromptToSaveChanges
End Sub

This finds "abdner.doc
 
J

jaysfive

Andi said:
You are sure????? *S* finds all S, but S* finds only the beginning, at
least on my Computers

If you expect an answer to a personal mail, add the word "manfred" to
the first 10 lines in the message

Hope you don't object Manfred but here is the full macro:-
Sub ChangeTemplateToNormal()

Dim killFile As String
Dim myCopy As Document
Dim docName As String
Dim oForm As Object
Dim i As Integer
Dim varFile As Variant
With Application.FileSearch
..NewSearch
..FileName = "d*.doc"
..LookIn = "G:\a_a_e"
..SearchSubFolders = False
..Execute

For Each varFile In .FoundFiles
With Documents.Open(FileName:=varFile,
ConfirmConversions:=False)
On Error GoTo z
..UpdateStylesOnOpen = False
On Error GoTo z
..AttachedTemplate = "normal"
On Error GoTo z
..Save
..Close savechanges:=wdSaveChanges
Application.StatusBar = varFile
Close
End With
u:

Next varFile
End With
GoTo x
'Application.StatusBar = varFile
z:

'+++++++++++++++++++++++++++++++++++++++++++++++++++++
MsgBox "Bad document,click OK, see next message box for document
name,move it somewhere else and run macro again"
MsgBox varFile
Application.StatusBar = varFile
x:

Call macro_finished
Application.Quit savechanges:=wdPromptToSaveChanges
End Sub

This finds "abdner.doc
 
J

Jean-Guy Marcil

jaysfive was telling us:
jaysfive nous racontait que :
No, this finds all files with an "S" anywhere in the filename

There is something wrong with your version or there is a bug in the version
you are using.

Here with Office 2003 SP1 on WinXP Pro SP2, the following
'_______________________________________
With Application.FileSearch
.FileName = "*.*"
.LookIn = "C:\Windows"
.MatchTextExactly = True
.SearchSubFolders = False
.Execute
For i = 1 To .FoundFiles.Count
MsgBox .FoundFiles(i)
Next
End With
'_______________________________________
returns 107 files.


On the other hand,
'_______________________________________
With Application.FileSearch
.FileName = "c*.*"
.LookIn = "C:\Windows"
.MatchTextExactly = True
.SearchSubFolders = False
.Execute
For i = 1 To .FoundFiles.Count
MsgBox .FoundFiles(i)
Next
End With
'_______________________________________
returns 8 files, the only 8 in the folder that start with "c".
And capitalization is not an issue,
.FileName = "C*.*"
returns the same 8 files.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
A

Andi Mayer

Hope you don't object Manfred but here is the full macro:-

I would use a total different aproach:

Sub Manfred_ChangeTemplateToNormal()
Dim FileName as string
FileName=Dir("G:\a_a_e\d*.doc")
Do While FileName<>""
'Do your Document stuff here
FileName=Dir
loop


If you expect an answer to a personal mail, add the word "manfred" to the first 10 lines in the message
MW
 
J

jaysfive

Helmut said:
Hi,
yes, you are right,
here and now, according to my specifications,
First point, filesearch returns the fullname,
like "c:\test\test.mydoc.doc"

Second point, it returns fullnames,
that contain an "S" or "s", unexpectedly,
and I don't know about case sensitivity here,
it is a mystery.

So, try this one:

Sub Makro2()
Dim l As Long
Dim s As String
With Application.FileSearch
.FileName = "S*.doc"
.LookIn = "c:\"
.SearchSubFolders = True
.Execute
For l = 1 To .FoundFiles.Count
s = StrReverse(.FoundFiles(l))
s = Left(s, InStr(s, "\"))
s = Left(s, Len(s) - 1)
s = StrReverse(s)
If Left(s, 1) = "s" Or Left(s, 1) = "S" Then
Selection.TypeText s & vbCr
End If
Next
End With
End Sub

If you need help for putting the result into an array,
ask again.

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98

Many thanks Helmut, since I'm a 67 year old pensioner without a lot of
knowledge of vba and trying to help my daughter with WORD may I push my
luck and ask you how I would insert your sub into:--


Sub ChangeTemplateToNormal()
Call macro_running
'Application.DisplayAlerts = False
'AnimateScreenMovements = False
'ScreenUpdating = False
'Application.Visible = False
Dim oForm As Object
Dim i As Integer
' Dim i As Long
Dim varFile As Variant
'UserForm1.Show
With Application.FileSearch
'*** select files ********************************

..FileName = "*.doc"
..LookIn = "\\server2\cvs\L_R"
..SearchSubFolders = False
..Execute

For Each varFile In .FoundFiles
With Documents.Open(FileName:=varFile,
ConfirmConversions:=False)
On Error GoTo z
..UpdateStylesOnOpen = False
..AttachedTemplate = "normal"
..Save
..Close SaveChanges:=wdSaveChanges
'Application.StatusBar = varFile
Close
End With
qq:
Next varFile
End With
GoTo x
'Application.StatusBar = varFile

ScreenUpdating = True
AnimateScreenMovements = False
Application.Visible = True
z:
MsgBox "Bad document,click OK, see next message box for document
name,move it somewhere else and run macro again"
MsgBox varFile

GoTo qq
Application.StatusBar = varFile
x:
'Unload UserForm1
Call macro_finished
Application.Quit SaveChanges:=wdPromptToSaveChanges

End Sub
 
H

Helmut Weber

Hi Jean-Guy,

I had omitted .MatchTextExactly = True
With it, everything works as expected.
Wasn't my day, it seems.

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
H

Helmut Weber

Hi Jean-Guy,
still one of these days...
Have a very close look at:

Sub NoGood()
Dim i As Integer
With Application.FileSearch
.FileName = "s*.doc"
.LookIn = "c:\"
.MatchTextExactly = True
.SearchSubFolders = True
.Execute
For i = 1 To .FoundFiles.Count
Selection.TypeText .FoundFiles(i) & vbCr
Next
End With
End Sub

and

Sub Alright()
Dim i As Integer
With Application.FileSearch
.FileName = "s*.*"
.LookIn = "c:\"
.MatchTextExactly = True
.SearchSubFolders = True
.Execute
For i = 1 To .FoundFiles.Count
Selection.TypeText .FoundFiles(i) & vbCr
Next
End With
End Sub

Did you try it? It's amazing!

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
J

Jean-Guy Marcil

Helmut Weber was telling us:
Helmut Weber nous racontait que :
Hi Jean-Guy,
still one of these days...
Have a very close look at:

Sub NoGood()
Dim i As Integer
With Application.FileSearch
.FileName = "s*.doc"
.LookIn = "c:\"
.MatchTextExactly = True
.SearchSubFolders = True
.Execute
For i = 1 To .FoundFiles.Count
Selection.TypeText .FoundFiles(i) & vbCr
Next
End With
End Sub

and

Sub Alright()
Dim i As Integer
With Application.FileSearch
.FileName = "s*.*"
.LookIn = "c:\"
.MatchTextExactly = True
.SearchSubFolders = True
.Execute
For i = 1 To .FoundFiles.Count
Selection.TypeText .FoundFiles(i) & vbCr
Next
End With
End Sub

Did you try it? It's amazing!

Amazing is not the word... more like "depressing"...

So there is a bug when we use
SearchSubFolders = True
and that we want specific file extensions...
(If you set SearchSubFolders to false, then it works as expected in both
cases, this means that MatchTextExactly has nothing to do with it).

Thanks, good to know... This means we need to implement some kind of
workaround...At least, by now, we are used to this!

Cheers!

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
H

Helmut Weber

Hi Jaysfive,
if you have read the other postings,
you know, that this was thrilling:
Didn't know, whether I was wrong or right,
but hopefully, I got it. It's like this:

Sub Gotcha()
Dim i As Integer
Dim s As String
With Application.FileSearch
.FileName = "s*.doc"
.LookIn = "c:\"
.MatchTextExactly = True
.SearchSubFolders = True
.Execute
For i = 1 To .FoundFiles.Count
s = StrReverse(.FoundFiles(i))
' seems to be case sensitive after all
If Mid(s, InStr(s, "\") - 1, 1) = "s" Or _
Mid(s, InStr(s, "\") - 1, 1) = "S" Then
' your code goes into here, like
' with
' Documents.Open(FileName:=.FoundFiles(i),
' ConfirmConversions:=False) etc. etc.
' next line is for testing only
Selection.TypeText .FoundFiles(i) & vbCr
End If
Next
End With
End Sub

If you need some comment on StrReverse or the mid-thing,
don't hesitate to ask again.

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
J

jaysfive

Not sure if I should be pleased or not at raising the issue, but as
long as you guys enjoyed yourselves, I can't complain. I certainly
appreciate your help. Many thanks Helmut and Jean-Guy. A word of
warning - if I need more help, I shall return

jeff
 
J

jaysfive

It works - YIPEE.
You've just saved me having to change templates on 27,000 CVs
Salut! & Vielen Dank!
regards
non foreign language speaking Scotsman
 
J

jaysfive

It works - YIPEE.
You've just saved me having to change templates on 27,000 CVs
Salut! & Vielen Dank!
regards
non foreign language speaking Scotsman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top