What is wrong with this sub-routine?

R

RHH

I very new to this VB coding game, so this may be fairly
obvious to everyone.

What's wrong with this?

~~~~~
Sub mcrFindandChangePassword()
' Basic macro for finding files.
Dim strFilePath As String
Dim strFileType As String

' The following are the defined variables
strFilePath = "H:\Excel"
strFileType = "*.xls"

'Find and open files associated with selected unit.
Set fs = Application.FileSearch
With fs

.LookIn = strFilePath
.FileName = strFileType
If .Execute > 0 Then
MsgBox "There were " & .FoundFiles.Count & _
" file(s) found."
For i = 1 To .FoundFiles.Count
MsgBox .FoundFiles(i)

'Open Documents in folder.
Documents.Open FileName:=.FoundFiles(i)

With ActiveDocument
.ReadOnlyRecommended = False
.EmbedTrueTypeFonts = False
.SaveFormsData = False
.SaveSubsetFonts = False
.Password = "tiger3"
.WritePassword = ""
End With
Application.DefaultSaveFormat = ""
ActiveDocument.SaveAs FileName:=.FoundFiles(i),
FileFormat:= _
wdFormatDocument, LockComments:=False,
Password:="tiger3", _
AddToRecentFiles:=True, WritePassword:="",
ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False,
SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False


Next i
Else
MsgBox "There were no files found."
End If
End With

End Sub
~~~~~~
I get a "Run-time error '5'" at .FileName = strFileType.

Any ideas?

Thanks in advance.

RHH
 
E

Ed

I'm not very experienced at all, but I just finished building one of these.
The only thing I see is the code might get confused with the "End With" for
fs all the way at the end, and another "With" sandwiched in the middle. I
would put it right after "MsgBox .FoundFiles(i)". Then again, refer to my
first comment.

Just out of curiosity, if that's the only place you use fs, why do that?
Why not just "With Application.FileSearch"? One less variable to process
when you're reading this 6 months from now.

Ed
 
H

Helmut Weber

Hi,
any ideas?
only a wild guess, as your code is a bit too messy.
I case you have set "option explicit",
you should dim filesearch, too. Like this:
Dim fs as filesearch
HTH
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
 

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