R
RPMitchal
Word 2007
Hello Gurus:
It seems as though the below macro from an earlier posting was put together
for a version of Word that is older than 2007. In testing it, I found that
among other things, “application.filesearch†is no longer a valid application
within Word 2007.
Unfortunately, I am not yet well versed enough with VBA to be able to work
around it or come up with a correctly working substitute.
Is there any chance at all that one of you fine folks may be able to “tweakâ€
this code so that it will indeed work with 2007? I can think of several
current projects for which I could make use of this macro.
If so, I would be so greatly appreciative.
Thanks – Rod
______________________________________________
Subject: execute macro for all documents in the folder
10/31/2005 9:41 AM PST
By: (e-mail address removed)
I'm really leary about posting my code up, but i'm going to cause i think my
macro is better than what they have. I give anyone permission to alter the
code i provide. I'm also embarressed if there are errors in it, as i know
there are a few things that are beyond my programming expertise, as i'm am
self-taught and don't have any VB experience and am learning this on my own.
If you have any suggestions, please give them, i'm very willing and ready to
learn everything i can about how to be a better programmer with this language.
What the program does-
1. Asks you if you want to save the files in FilteredHTML before you run a
macro on them.
2. Asks how many folders you want to run a macro on.
3. Allows input of the paths of all the folders to run the macro on.
4. Asks how many macros you want to run on each folder you input.
5. Asks for the name(s) of the macro(s) you want to run.
6. Asks if you want to save them in FilteredHTML when the macro(s) are done.
And now the code-
Sub MacroRunner()
'this macro will allow for the
'running of a macro on a whole folder
'or, many folders. If you want you can
'save the initial file as FilteredHTML
'so when the macro is actually run
'it will run on a filtered HTML file
Application.ScreenUpdating = False
Dim counter, y, z As Integer
Dim fPath, fiType, path, fLog, mLog, f, d, tLog, flist, _
convert, MtoRun, MtoRunName, fFilename, mFilename, _
nmacros, nfolders As String
Dim doc As Document
Set f = CreateObject("Scripting.FileSystemObject")
fFilename = Environ$("APPDATA") & "\~folders" & ".txt"
mFilename = Environ$("APPDATA") & "\~macros" & ".txt"
Set tLog = f.Createtextfile(fFilename, True)
Set mLog = f.Createtextfile(mFilename, True)
convert = MsgBox(prompt:="Do you want convert everything to Filtered
HTML?", _
buttons:=vbYesNo)
Application.ScreenRefresh
fiType = MsgBox(prompt:="Do you want to save completed files in Filtered
HTML?", _
buttons:=vbYesNo)
Application.ScreenRefresh
nfolders = InputBox(prompt:="How many folders do you want to run macro
on?")
If Len(nfolders) = 0 Then
MsgBox ("You hit cancel or didn't put anything in.")
Exit Sub
End If
Application.ScreenRefresh
For y = 1 To nfolders
If nfolders = 1 Then
path = InputBox(prompt:="Input the folder's path.")
ElseIf y = nfolders Then
path = InputBox(prompt:="Input the last folder's path.")
ElseIf y = 1 Then
path = InputBox(prompt:="Input the 1st folder's path.")
ElseIf y = 2 Then
path = InputBox(prompt:="Input the 2nd folder's path.")
ElseIf y = 3 Then
path = InputBox(prompt:="Input the 3rd folder's path.")
Else
path = InputBox(prompt:="Input the " & y & "th folder's path.")
End If
If Len(path) = 0 Then
MsgBox ("You hit cancel or didn't put anything in.")
Exit Sub
End If
tLog.writeline (path)
Application.ScreenRefresh
Next y
nmacros = InputBox(prompt:="How many macros do you want to run?")
If Len(nfolders) = 0 Then
MsgBox ("You hit cancel or didn't put anything in.")
Exit Sub
End If
Application.ScreenRefresh
For y = 1 To nmacros
If nmacros = 1 Then
MtoRun = InputBox(prompt:="Input the macro's name you want to
run on all of the files.")
ElseIf y = nmacros Then
MtoRun = InputBox(prompt:="Input the last macro's name you want
to run on all of the files.")
ElseIf y = 1 Then
MtoRun = InputBox(prompt:="Input the 1st macro's name you want
to run on all of the files.")
ElseIf y = 2 Then
MtoRun = InputBox(prompt:="Input the 2nd macro's name you want
to run on all of the files.")
ElseIf y = 3 Then
MtoRun = InputBox(prompt:="Input the 3rd macro's name you want
to run on all of the files.")
Else
MtoRun = InputBox(prompt:="Input the " & y & "th macro's name
you want to run on all of the files.")
End If
If Len(MtoRun) = 0 Then
MsgBox ("You hit cancel or didn't put anything in.")
Exit Sub
End If
mLog.writeline (MtoRun)
Application.ScreenRefresh
Next y
tLog = (fFilename)
mLog = (mFilename)
Open tLog For Input As #1
Do While Not EOF(1)
Line Input #1, fPath
If convert = vbYes Then
With Application.FileSearch
.NewSearch
.LookIn = fPath
.SearchSubFolders = False
.FileType = msoFileTypeAllFiles
If Not .Execute() = 0 Then
For counter = 1 To .FoundFiles.Count
Set doc = Documents.Open(.FoundFiles(counter))
ActiveDocument.SaveAs
FileFormat:=wdFormatFilteredHTML
ActiveDocument.Close
Set doc = Nothing
Next counter
Else
MsgBox "No Word Documents in specified folder(s)"
End If
End With
End If
With Application.FileSearch
.NewSearch
.LookIn = fPath
.SearchSubFolders = False
.FileType = msoFileTypeAllFiles
If Not .Execute() = 0 Then
For counter = 1 To .FoundFiles.Count
Open mLog For Input As #2
Set doc = Documents.Open(.FoundFiles(counter))
Do While Not EOF(2)
Line Input #2, MtoRunName
Application.Run MtoRunName
Loop
If fiType = vbYes Then
ActiveDocument.SaveAs
FileFormat:=wdFormatFilteredHTML
Else
ActiveDocument.Save
End If
ActiveDocument.Close
Set doc = Nothing
Close #2
Next counter
Else
MsgBox "No files in specified folder(s)"
End If
End With
Loop
Close #1
f.deletefile mFilename
f.deletefile fFilename
Application.ScreenUpdating = True
MsgBox "Finally... Done"
End Sub
Hello Gurus:
It seems as though the below macro from an earlier posting was put together
for a version of Word that is older than 2007. In testing it, I found that
among other things, “application.filesearch†is no longer a valid application
within Word 2007.
Unfortunately, I am not yet well versed enough with VBA to be able to work
around it or come up with a correctly working substitute.
Is there any chance at all that one of you fine folks may be able to “tweakâ€
this code so that it will indeed work with 2007? I can think of several
current projects for which I could make use of this macro.
If so, I would be so greatly appreciative.
Thanks – Rod
______________________________________________
Subject: execute macro for all documents in the folder
10/31/2005 9:41 AM PST
By: (e-mail address removed)
I'm really leary about posting my code up, but i'm going to cause i think my
macro is better than what they have. I give anyone permission to alter the
code i provide. I'm also embarressed if there are errors in it, as i know
there are a few things that are beyond my programming expertise, as i'm am
self-taught and don't have any VB experience and am learning this on my own.
If you have any suggestions, please give them, i'm very willing and ready to
learn everything i can about how to be a better programmer with this language.
What the program does-
1. Asks you if you want to save the files in FilteredHTML before you run a
macro on them.
2. Asks how many folders you want to run a macro on.
3. Allows input of the paths of all the folders to run the macro on.
4. Asks how many macros you want to run on each folder you input.
5. Asks for the name(s) of the macro(s) you want to run.
6. Asks if you want to save them in FilteredHTML when the macro(s) are done.
And now the code-
Sub MacroRunner()
'this macro will allow for the
'running of a macro on a whole folder
'or, many folders. If you want you can
'save the initial file as FilteredHTML
'so when the macro is actually run
'it will run on a filtered HTML file
Application.ScreenUpdating = False
Dim counter, y, z As Integer
Dim fPath, fiType, path, fLog, mLog, f, d, tLog, flist, _
convert, MtoRun, MtoRunName, fFilename, mFilename, _
nmacros, nfolders As String
Dim doc As Document
Set f = CreateObject("Scripting.FileSystemObject")
fFilename = Environ$("APPDATA") & "\~folders" & ".txt"
mFilename = Environ$("APPDATA") & "\~macros" & ".txt"
Set tLog = f.Createtextfile(fFilename, True)
Set mLog = f.Createtextfile(mFilename, True)
convert = MsgBox(prompt:="Do you want convert everything to Filtered
HTML?", _
buttons:=vbYesNo)
Application.ScreenRefresh
fiType = MsgBox(prompt:="Do you want to save completed files in Filtered
HTML?", _
buttons:=vbYesNo)
Application.ScreenRefresh
nfolders = InputBox(prompt:="How many folders do you want to run macro
on?")
If Len(nfolders) = 0 Then
MsgBox ("You hit cancel or didn't put anything in.")
Exit Sub
End If
Application.ScreenRefresh
For y = 1 To nfolders
If nfolders = 1 Then
path = InputBox(prompt:="Input the folder's path.")
ElseIf y = nfolders Then
path = InputBox(prompt:="Input the last folder's path.")
ElseIf y = 1 Then
path = InputBox(prompt:="Input the 1st folder's path.")
ElseIf y = 2 Then
path = InputBox(prompt:="Input the 2nd folder's path.")
ElseIf y = 3 Then
path = InputBox(prompt:="Input the 3rd folder's path.")
Else
path = InputBox(prompt:="Input the " & y & "th folder's path.")
End If
If Len(path) = 0 Then
MsgBox ("You hit cancel or didn't put anything in.")
Exit Sub
End If
tLog.writeline (path)
Application.ScreenRefresh
Next y
nmacros = InputBox(prompt:="How many macros do you want to run?")
If Len(nfolders) = 0 Then
MsgBox ("You hit cancel or didn't put anything in.")
Exit Sub
End If
Application.ScreenRefresh
For y = 1 To nmacros
If nmacros = 1 Then
MtoRun = InputBox(prompt:="Input the macro's name you want to
run on all of the files.")
ElseIf y = nmacros Then
MtoRun = InputBox(prompt:="Input the last macro's name you want
to run on all of the files.")
ElseIf y = 1 Then
MtoRun = InputBox(prompt:="Input the 1st macro's name you want
to run on all of the files.")
ElseIf y = 2 Then
MtoRun = InputBox(prompt:="Input the 2nd macro's name you want
to run on all of the files.")
ElseIf y = 3 Then
MtoRun = InputBox(prompt:="Input the 3rd macro's name you want
to run on all of the files.")
Else
MtoRun = InputBox(prompt:="Input the " & y & "th macro's name
you want to run on all of the files.")
End If
If Len(MtoRun) = 0 Then
MsgBox ("You hit cancel or didn't put anything in.")
Exit Sub
End If
mLog.writeline (MtoRun)
Application.ScreenRefresh
Next y
tLog = (fFilename)
mLog = (mFilename)
Open tLog For Input As #1
Do While Not EOF(1)
Line Input #1, fPath
If convert = vbYes Then
With Application.FileSearch
.NewSearch
.LookIn = fPath
.SearchSubFolders = False
.FileType = msoFileTypeAllFiles
If Not .Execute() = 0 Then
For counter = 1 To .FoundFiles.Count
Set doc = Documents.Open(.FoundFiles(counter))
ActiveDocument.SaveAs
FileFormat:=wdFormatFilteredHTML
ActiveDocument.Close
Set doc = Nothing
Next counter
Else
MsgBox "No Word Documents in specified folder(s)"
End If
End With
End If
With Application.FileSearch
.NewSearch
.LookIn = fPath
.SearchSubFolders = False
.FileType = msoFileTypeAllFiles
If Not .Execute() = 0 Then
For counter = 1 To .FoundFiles.Count
Open mLog For Input As #2
Set doc = Documents.Open(.FoundFiles(counter))
Do While Not EOF(2)
Line Input #2, MtoRunName
Application.Run MtoRunName
Loop
If fiType = vbYes Then
ActiveDocument.SaveAs
FileFormat:=wdFormatFilteredHTML
Else
ActiveDocument.Save
End If
ActiveDocument.Close
Set doc = Nothing
Close #2
Next counter
Else
MsgBox "No files in specified folder(s)"
End If
End With
Loop
Close #1
f.deletefile mFilename
f.deletefile fFilename
Application.ScreenUpdating = True
MsgBox "Finally... Done"
End Sub