Montag, 30. Juli 2012 10:44
How do I create a macro in Word 2003 to find all files containing a specific style.
I asked this question in the microsoft office community and was instructed to post the question at microsoft.public.word.vba.general but I have been unable to find this link.
I have very little working knowledge of macros but I am very familiar with Word.
Montag, 30. Juli 2012 12:26
Try the following as a starting point. In this example, the list of found documents is displayed in a message box, but you could use it in other ways (list the documents in a new document, ...)
Sub FindFiles() ' Replace "Caption" with the name of the style to find Const StyleName = "Caption" Dim strPath As String Dim strFile As String Dim doc As Document Dim strList As String With Application.FileDialog(4) ' 4=msoFileDialogFolderPicker If .Show Then strPath = .SelectedItems(1) Else MsgBox "No folder specified", vbCritical Exit Sub End If End With If Right(strPath, 1) <> "\" Then strPath = strPath & "\" End If strFile = Dir(strPath & "*.doc*") Application.ScreenUpdating = False Do While strFile <> "" Set doc = Documents.Open(FileName:=strPath & strFile, _ AddToRecentFiles:=False) With doc.Content.Find .ClearFormatting .Text = "" .Style = StyleName If .Execute Then strList = strList & vbCrLf & strFile End If End With doc.Close SaveChanges:=False strFile = Dir Loop Application.ScreenUpdating = True If strList = "" Then MsgBox "No documents found", vbInformation Else MsgBox "Documents:" & strList, vbInformation End If End Sub
Regards, Hans Vogelaar
Dienstag, 21. August 2012 10:06
Thank you for your reply Hans. I am a complete novice at this but am going to give it a try. I am assuming that I create it in a Word document.
Best wishes, Chrisana
Dienstag, 21. August 2012 13:19
Yes - you should create it in Word:
- Start Word.
- Press Alt+F11 to activate the Visual Basic Editor.
- Select Insert | Module to create a new module.
- Copy/paste the code from my previous reply into the module.
- With the insertion point anywhere in the code, press F5 or select Run | Run Macro to execute the macro.
Regards, Hans Vogelaar
Mittwoch, 22. August 2012 09:17
I have posted a similar entry in my blog:
Place a command button on word doc,. copy and paste the code, should work well, else mail back.
Love the Love that Loves the Love and Hate the Love that Loves the Hate.