none
Create a macro RRS feed

  • Question

  • 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.

    Monday, July 30, 2012 10:44 AM

Answers

  • 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

    Monday, July 30, 2012 12:26 PM

All replies

  • 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

    Monday, July 30, 2012 12:26 PM
  • 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

    Tuesday, August 21, 2012 10:06 AM
  • 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

    Tuesday, August 21, 2012 1:19 PM
  • Hi Christiana,

    I have posted a similar entry in my blog:

    Link:http://repath.wordpress.com/2010/07/19/find-files-incl-subfolders/

    Place a command button on word doc,. copy and paste the code, should work well, else mail back.

    With Regards
    Repath.


    Love the Love that Loves the Love and Hate the Love that Loves the Hate.

    Wednesday, August 22, 2012 9:17 AM