View Single Post
 
Old 10-19-2019, 09:11 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The following will loop through the documents in a selected folder and run my earlier code

Code:
Sub BatchProcess()
Dim strFile As String
Dim strName As String
Dim strPath As String
Dim oDoc 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)
        Do Until Right(strPath, 1) = ""
            strPath = strPath & ""
        Loop
    End With
    strFile = Dir$(strPath & "*.doc")
    ' If the files are .txt format then change .doc in the line above to .txt
    While strFile <> ""
        Set oDoc = Documents.Open(strPath & strFile)
        'do stuff with the document e.g. call the macro I posted earlier
        AddLinks oDoc
        ' If the files are .txt format then add the following 2 lines
        'strName = Left(oDoc.Name, InStrRev(oDoc.Name, ".")) & "docx"
        'oDoc.SaveAs2 FileName:=strPath & strName, FileFormat:=12, AddToRecentFiles:=False
        oDoc.Close SaveChanges:=wdSaveChanges
        strFile = Dir$()
    Wend
    MsgBox "Processing complete"
lbl_Exit:
    Set oDoc = Nothing
    Exit Sub
End Sub
to test the original macro, open a document and run the following
Code:
Sub Test()
AddLinks ActiveDocument
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote