View Single Post
 
Old 01-12-2022, 11:35 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

This code is an entire module that you should put into your Excel workbook. You will need to add References to:
Microsoft Scripting Runtime
Microsoft Word x.x Object Library

Then run the Test macro after changing the filepath to point at the folder where your individual Word documents are.

NOTE: The vast majority of this code came from a post on StackOverflow by Ryan
Code:
Option Explicit

Private mobjWordApp As Word.Application

Sub Test()
  ProcessDirectory "C:\temp\"
End Sub

Property Get WordApp() As Word.Application
  If mobjWordApp Is Nothing Then
    Set mobjWordApp = CreateObject("Word.Application")
    mobjWordApp.Visible = True
  End If
  Set WordApp = mobjWordApp
End Property

Sub CloseWordApp()
  If Not (mobjWordApp Is Nothing) Then
    On Error Resume Next
    mobjWordApp.Quit
    Set mobjWordApp = Nothing
  End If
End Sub

Function GetWordDocument(FileName As String) As Word.Document
  On Error Resume Next
  Set GetWordDocument = WordApp.Documents.Open(FileName)
  If Err.Number = &H80010105 Then
    CloseWordApp
    On Error GoTo 0
    Set GetWordDocument = WordApp.Documents.Open(FileName)
  End If
End Function

Sub ProcessDirectory(PathName As String)
  Dim fso As New FileSystemObject, objFile As File
  Dim objFolder As Folder, objWordDoc As Object, aSheet As Worksheet
  Const aSheetName As String = "DocTitles"
  
  On Error Resume Next
    Set aSheet = ActiveWorkbook.Sheets(aSheetName)
  On Error GoTo Err_Handler
  If aSheet Is Nothing Then
    Set aSheet = ActiveWorkbook.Sheets.Add
    aSheet.Name = aSheetName
  End If
  aSheet.Range("A1").Value = "Title"
  aSheet.Range("B1").Value = "Filename"

  Set objFolder = fso.GetFolder(PathName)
  For Each objFile In objFolder.Files
    If objFile.Name Like "*.doc*" Then
      Set objWordDoc = GetWordDocument(objFile.Path)
      ProcessDocument objWordDoc, aSheet
      objWordDoc.Close 0, 1
      Set objWordDoc = Nothing
    End If
  Next

Exit_Handler:
  CloseWordApp
  Exit Sub

Err_Handler:
  MsgBox "Error " & Err.Number & ": " & Err.Description
  Resume Exit_Handler
  'Resume Next ' or as above
End Sub

Sub ProcessDocument(objWordDoc As Document, aSheet As Worksheet)
  Dim aRng As Word.Range, sFound As String, iRow As Integer
  Set aRng = objWordDoc.Content
  With aRng.Find
    .ClearFormatting
    .Style = "Title"
    .Text = ""
    If .Execute = True Then
      sFound = aRng.Text
      iRow = aSheet.UsedRange.Rows.Count + 1
      sFound = Trim(Replace(sFound, vbCr, " ")) 'replace paragraph marks with a space
      aSheet.Cells(iRow, 1).Value = sFound
      aSheet.Cells(iRow, 2).Value = objWordDoc.Name
    End If
  End With
End Sub
I don't think this code actually handles the multiple title paragraphs correctly but your sample doc didn't have any so I didn't bother fixing this. If we loop through your template there are other Title-styled paragraphs which certainly aren't titles.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia

Last edited by Guessed; 01-13-2022 at 03:50 PM.
Reply With Quote