Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

run Macro on all open Word docs

Status
Not open for further replies.

richiwatts

Technical User
Jun 21, 2002
180
GB
How do I get a macro to run on all open Word files?

Rich
 
A starting point:
For Each objDoc In Application.Documents
MsgBox objDoc.Name " is open"
Next

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
OK, where would I put that then++

Sub ActionSearch()
'Search for word
On Error Resume Next
Dim lCol As Long
Dim lRow As Long
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(msoFileDialogFilePicker)
dlgOpen.AllowMultiSelect = False
dlgOpen.Filters.Add "*.*", 1
dlgOpen.Title = "Select Dictionary"
dlgOpen.Show

Dim strFileName As String

Err.Clear
strFileName = dlgOpen.SelectedItems(1)
If Err.Number <> 0 Then Exit Sub

If Dir(strFileName) = "" Then Exit Sub
If UCase(Trim(Right(strFileName, 3))) <> UCase("xls") Then Exit Sub

Err.Clear
Set appObject = GetObject("Excel.Application")
If Err.Number <> 0 Then
Set appObject = CreateObject("Excel.Application")
End If
Set wbObject = appObject.Workbooks.Open(strFileName)

Set wsObject = wbObject.sheets("sheet1.")
Dim strTemp1 As String
Dim strTemp2 As String
lRow = 1

Do
strTemp1 = wsObject.Cells(lRow, 2)
If Trim(strTemp1) = "" Then Exit Do
Call FindWord(strTemp1)
lRow = lRow + 1
Loop
MsgBox "Completed"

End Sub

Sub FindWord(ByVal strFindWord As String)

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting

Options.DefaultHighlightColorIndex = wdYellow

Application.ScreenUpdating = False

With Selection.Find
.Text = strFindWord

.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Highlight = False
.Replacement.Highlight = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 
It may help if you walked the logic through step-by-step. I am not sure, it looks like you are starting IN Word. I am basing this on the fact you are making an Excel instance. So.

You are in Word.
You have a file dialog.
You are getting an xls file.
You are declaring strTemp1, AND strTemp2 as strings...but never use strTemp2.
You use strTemp1 as a parameter in FindWord.
FindWord will action on the current active document.

So what is the question again? WHAT open documents? You never open any new ones.

As PH states (although not explicitly) open documents are in the Documents collection.
Code:
Dim objDoc As Word.Document
For Each objDoc In Application.Documents
  ' call the subs to do whatever it is
Next
will loop through all open documents. But I can't see where you have any, other than the current active one. But then...maybe you do have them already open. if so, that may be not all that great an idea. How many are you planning to process?


Gerry
 
I was going to open 15 files and then run the Macro on all open files
 
Hmmm, you get resource issues having that many files open. Why are you doing that? Is there a real reason for it? Why not process the files one by one (which you have to anyway) but opening them one by one. I really think having 15 files open at the same time is not a good idea, nor it is needed to do so.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top