How to create a macro which can identify the Microsoft doc/docx file for Comments .Basically could sort the commented files from non -commented one.

I have multiple Microsoft word document i,,e around 5000 docs. And want to identify the documents containing comments and want to move those commented files to "Comment Folder".And the non-commented Files to "Non-commented Folder".And it should run in a batch(For multiple files).

I want to only specify the path of a particular directory containing these documents and want a macro should run and  sort the commented files with non-commented one and create a log after the completion of the batch.

And the macro should have the Exception handling also,as if  there would be any eroneous files that will not allow a macro to identify the comments or could not open that file ,those file name/or if possible those files should come under "Eroneous Folder" or should create a log for those eroneous files .

Kindly help me with  this issue.

Thanks & Regards

Gagan Khurana

Answer
Answer

The procedure would be similar to that you requested in your other message, but with a different set of actions relating to the open document. The macro will create the comment and non comment folders if not present. as sub-folders of the chosen folder. e.g.

Option Explicit

Sub BatchProcessMoveFiles()
Dim strFilename As String
Dim strPath As String
Const strCommentsPath As String = "Comment Folder\"
Const strNoCommentsPath As String = "Non-commented Folder\"
Dim strFiles As String
Dim oDoc As Document
Dim Log As Document
Dim fDialog As FileDialog
    Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
    With fDialog
        .Title = "Select folder and click OK"
        .AllowMultiSelect = False
        .InitialView = msoFileDialogViewList
        If .Show <> -1 Then
            MsgBox "Cancelled By User", , _
                   "List Folder Contents"
            Exit Sub
        End If
        strPath = fDialog.SelectedItems.Item(1) & "\"
        If Not FolderExists(strPath & strCommentsPath) Then MkDir strPath & strCommentsPath
        If Not FolderExists(strPath & strNoCommentsPath) Then MkDir strPath & strNoCommentsPath
    End With
    strFiles = ""
    strFilename = Dir$(strPath & "*.doc")        'should open doc, docx, and docm formats
    While Len(strFilename) <> 0
        WordBasic.DisableAutoMacros 1
        Set oDoc = Documents.Open(strPath & strFilename)
        strFiles = strFiles & Date & "-" & Time & Chr(32) & oDoc.FullName & " moved to "
        'do what you want with the open document
        If oDoc.Content.Comments.Count > 0 Then
            oDoc.Close SaveChanges:=wdDoNotSaveChanges
            Name strPath & strFilename As strPath & strCommentsPath & strFilename
            strFiles = strFiles & strCommentsPath & strFilename & vbCr
        Else
            oDoc.Close SaveChanges:=wdDoNotSaveChanges
            Name strPath & strFilename As strPath & strNoCommentsPath & strFilename
            strFiles = strFiles & strNoCommentsPath & strFilename & vbCr
        End If
        strFilename = Dir$()
        WordBasic.DisableAutoMacros 0
    Wend
    Set oDoc = Nothing
    Set Log = Documents.Add
    Log.Range.Text = strFiles
End Sub

Public Function FolderExists(ByVal PathName As String) As Boolean
Dim lngAttr As Long
    On Error GoTo NoFolder
    lngAttr = GetAttr(PathName)
    If (lngAttr And vbDirectory) = vbDirectory Then
        FolderExists = True
    End If
NoFolder:
End Function

Graham Mayor (Microsoft Word MVP 2002-2019)
For more Word tips and downloads visit my web site
https://www.gmayor.com/Word_pages.htm

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 174 Applies to: